2021-01-30

Replacing strings with docvariables located within textboxes

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.

I use two modules to replace tags with DOCVARIABLES

***TAG Example: "DEPARTMENT_NAME" string text

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


Module 1

Public Function PopulateDocuments() As Boolean

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

Module 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.

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.



from Recent Questions - Stack Overflow https://ift.tt/3oAToHv
https://ift.tt/eA8V8J

No comments:

Post a Comment