【问题标题】:Replacing strings with docvariables located within textboxes用文本框中的文档变量替换字符串
【发布时间】:2021-02-02 04:54:23
【问题描述】:

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

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

***标签示例: “DEPARTMENT_NAME” 字符串文本

文档变量示例: {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 

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

在替换那些我能找到的 TAG 时,我没有看到任何错误消息。除文本框中的标签外,所有标签均已正确替换为 DOCVARIABLES。

【问题讨论】:

  • 这里有问题吗?
  • 代码的作者是谁?来源是什么?

标签: ms-word textbox docvariable


【解决方案1】:

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

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

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

【讨论】:

  • 我收到“If shp.TextFrame2.HasText Then”运行时错误'-2147467263 (80004001) 上的指定值超出范围错误
  • 从 TextFrame2 更改为 TextFrame,看看是否可行。
  • 文本框停止错误。问题是需要一个更好的名称完整 Word 等效于文本框中 DocVariable 的 URL 路径。 wrd1 是文档
  • find 值与您在代码模块 2 中使用的“strFind”值不一样吗?我不清楚您所说的“wrd1 是文档”。
猜你喜欢
  • 2014-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-07
相关资源
最近更新 更多