Hello everyone,
we have a macro project in a template file for generating official documents cleanly and uniformly. It has served us well for many many years. One part of the project is the replacement of custom variables. The project uses multiple defined custom variables as well as allows users to create their own. These variables are prefixed and postfixed with a hash/pound sign # to identify and replace (.e.g with values read from an ini file, e.g. data retreived from an LDAP source like ActiveDirectory.
Since the november 11 update, the Selection.Extend breaks on second and every subsequent run. Variables are in the form of e.g. #VTITLE#, which when resolved would replace it with Dr. if applicable. (mqa_setup_1, mqa_success_1).
We use the following code to find the next installment of a variable and replace it (:
Sub s_eingaben_erfragen()
On Error GoTo Hell
Dim i As Integer
Dim StartPos As Long
StartPos = 0
Selection.HomeKey unit:=wdStory
Do
noch_etwas_da = False
Selection.start = StartPos
With Selection.Find
.ClearFormatting
.text = "#"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.Execute Forward:=True
End With
If Selection.Find.Found = True Then
StartPos = Selection.start
If StartPos > 2 Then
StartPos = StartPos - 2 ' performance performance gains
End If
noch_etwas_da = True
If Selection.Find.text = "#" Then ' set breakpoint here
Selection.Extend ("#")
S_Select_Anweisungen ' Set true or false in p_anweisung, if valid custom command
End If
If P_anweisung = False Then ' if not resolvable, thus no custom commando
Selection.Collapse direction:=wdCollapseStart
Selection.Expand unit:=wdCharacter
Selection.TypeText "~*~" ' # will be replaced with ^~^
End If
End If
Loop While noch_etwas_da = True
Selection.HomeKey unit:=wdStory '~*~ wieder zurücksetzen
Dim ReplErg As Boolean
ReplErg = True
Do While ReplErg
ReplErg = Selection.Find.Execute("~*~", , , , , , , , , "#")
Selection.Collapse direction:=wdCollapseEnd
Loop
Exit Sub
Hell: PS_Fehlerbehandlung ErrNr:=Err.Number, ErrSource:=Err.Source, ErrDesc:=Err.Description
End Sub
Now in detail:
With Selection.Find
.ClearFormatting
.text = "#"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.Execute Forward:=True
End With
will go and select the next hash, identifiable by halting at the mentioned breakpoint (mqa_1). If we go past
Selection.Extend ("#")
then the entire variable will be selected, including the hashes => #VBA# (mqa_2). The replacement action happens smoothly. When we enter the loop a second time for the second variable the block above still identifies correctly the hash sign (mqa_3), but when we enter into the Selection.Extend it shifts one character away and now only targets BSGCO# (mqa_5) instead of #VBSGO#, which breaks our entire application, since it fails to resolve and runs into an infinite loop of adding ~*~ into the variable position (mqa_fail_1).
Does anybody have tips how to resolve this issue? This absolutely breaks our agencies workflows top to bottom. It definitely is the november patch, once uninstalled everything works smoothly again. We are helpful for any advice you can give us. Thank you and kind regards
Mamue
