用位于文本框中的文档变量替换字符串 [英] Replacing strings with docvariables located within textboxes

查看:47
本文介绍了用位于文本框中的文档变量替换字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 DocVariable 字段自动替换文本字符串.唯一的问题是当这些字符串(以后我将其称为 TAGS)时,是当 TAG 位于TextBox"内时.控制.

I am replacing strings of text with automation with DocVariable fields. The only issues is when these strings, going forward I will referrer to as TAGS, is when the TAG is located within "TextBox" Controls.

我使用两个模块用 DOCVARIABLES 替换标签

I use two modules to replace tags with DOCVARIABLES

***TAG 示例:DEPARTMENT_NAME" 字符串文本

***TAG Example: "DEPARTMENT_NAME" string text

DOCVARIABLE 示例:{DOCVARIABLE "Department_Name";* 合并格式}***

DOCVARIABLE EXAMPLE: {DOCVARIABLE "Department_Name" * MERGEFORMAT}***

模块 1

Public Function PopulateDocuments() As Boolean

Dim Department_Name As String
Department_Name = "Department_Name"
ReplaceWithDocVar "DEPARTMENT_NAME", "Department_Name"
End Function


模块 2

Public Function ReplaceWithDocVar(strFind As String, strVarName As String) As Boolean
Dim oStory As Range
Dim TextColor As Range
Dim strDocName As String
Dim orgDocName As String
Dim orgPath As String
Dim intPos As Integer
Dim docpath As String
Dim docname As String
Application.DisplayAlerts = False
    For Each oStory In ActiveDocument.StoryRanges
        With oStory.Find

            Do While .Execute(FindText:=strFind, MatchCase:=True, MatchWholeWord:=True)
                
                oStory.Text = ""
                'oStory.Text = wdBlue

                oStory.Fields.Add Range:=oStory, _
                                  Type:=wdFieldDocVariable, _
                                  Text:=Chr(34) & strVarName & Chr(34), _
                                  PreserveFormatting:=True

                oStory.Collapse 0

            Loop
        End With
        If oStory.StoryType <> wdMainTextStory Then
            While Not (oStory.NextStoryRange Is Nothing)
                Set oStory = oStory.NextStoryRange
                With oStory.Find

                    Do While .Execute(FindText:=strFind, MatchCase:=True, MatchWholeWord:=True)

                        oStory.Text = ""
                        'oStory.Text = wdBlue
                        oStory.Fields.Add Range:=oStory, _
                                          Type:=wdFieldDocVariable, _
                                          Text:=Chr(34) & strVarName & Chr(34), _
                                          PreserveFormatting:=True
                        'oStory.Font.ColorIndex = wdBlue
                        oStory.Collapse 0

                    Loop
                End With
            Wend
        End If
    Next oStory
    Set oStory = Nothing
lbl_Exit:
    Exit Function
End Function 

注意:我不是这段代码的作者,但它运行得非常好.

Note: I am not the author of this code but it runs very well all of the same.

在替换我可以找到的那些 TAG 时,我没有看到任何错误消息.除文本框中的标签之外的所有标签都正确替换为 DOCVARIABLES.

I do not see any error messages when replacing those TAGs that I can locate. All TAGS excluding those in textboxes are correctly replaced with DOCVARIABLES.

推荐答案

文本框是文档中的形状.您必须在文档中搜索 Shapes,然后确定 Shape 是否是带有文本内容的 TextBox.无论如何,对文本框的搜索以及对它们的文本内容的检查都必须与通过当前代码的文档故事范围进行的其他搜索分开.

TextBoxes are Shapes within the document. You have to search for Shapes in the document and then determine if the Shape is a TextBox with text content. In any event, the search for TextBoxes and then the examination of their text content will have to be separate from your other searches through the document story ranges of your current code.

这是一些用于搜索文本框的示例代码.根据您的项目需要对其进行调整.

Here is some sample code for searching TextBoxes. Adapt it as required for your project.

Dim shp As Word.Shape
Dim rng As Word.Range
For Each shp In ActiveDocument.Shapes
    If shp.Type = msoTextBox Then
        If shp.TextFrame.HasText Then
            Set rng = shp.TextFrame.TextRange
            With rng.Find
                'add your find and replace criteria here
            End With
        End If
    End If
Next

这篇关于用位于文本框中的文档变量替换字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆