Freigeben über

November 11 Update breaks VBA Selection.Extend

Mamue 0 Zuverlässigkeitspunkte
2025-11-14T17:11:52.7333333+00:00

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

mqa

Microsoft 365 und Office | Entwicklung | JavaScript-API für Office
0 Kommentare Keine Kommentare
{count} Stimmen

Vom Moderator empfohlene Antwort
  1. Mamue 0 Zuverlässigkeitspunkte
    2025-11-17T16:41:46.8933333+00:00

    We have found a solution for the problem by changing our code to mitigate the damage the patch has caused.

    If Selection.Find.text = "#" Then
                    Selection.Extend ("#")
                    If InStr(1, Selection.text, "#") <> 1 Then
                        ' Set highlight of variable# back to #variable#
                        ' In some cases the highlight happens correctly
                        ' Checking with InStr if the first element is a #
                        ' If that is the case Selection.Start will not be lowered                    
    
                        Selection.start = Selection.start - 1
                    End If
                    
                    S_Select_Anweisungen             
                End If
    '....
    
    0 Kommentare Keine Kommentare

5 zusätzliche Antworten

Sortieren nach: Am hilfreichsten
  1. Thomas L 141.4K Zuverlässigkeitspunkte Freiwilliger Moderator
    2025-11-18T07:04:32.09+00:00

    Hallo,

    vielen Dank für deine Nachricht.

    Bei weiteren Fragen stehen wir zur Verfügung.

    0 Kommentare Keine Kommentare

Ihre Antwort

Antworten können von Fragestellenden als „Angenommen“ und von Moderierenden als „Empfohlen“ gekennzeichnet werden, wodurch Benutzende wissen, dass diese Antwort das Problem des Fragestellenden gelöst hat.