I am relatively new to Word VBA and have a problem with my logic. I want to prompt a user for two numbers, then search the text for those two numbers enclosed within square brackets and highlighted (in fact I have just realised that the condition "highlighted" is sufficient but that is not relevant to my problem.
I have tried to construct a Do..While loop to run the Find and Replace until Low Number = High Number. I have a number of problems - the loop is endless at the moment and sometimes I get a mismatch Error 13 caused I think by the type of variable i have chosen to store lownum and hinum (i've been round this so many times, I am now confused). Any help would be VERY gratefully accepted. I copy the code below.
Sub Step4_1()
' Step4_1 Macro
Dim Message, Title, Default, lowNum As String, hiNum As String, check
check = True: lowNum = 0: hiNum = 0 'Initialise variables
Message = "Enter the lower section number to remove - DO NOT ENTER THE BRACKETS" 'Set prompt
Title = "User Input Low Section Number" ' Set title.
Default = "0" ' Set default.
' Display message, title, and default value.
lowNum = InputBox(Message, Title, Default)
Message = "Enter the upper section number to remove - DO NOT ENTER THE BRACKETS" 'Set prompt
Title = "User Input High Section Number" ' Set title.
Default = "0" ' Set default.
' Display message, title, and default value.
highNum = InputBox(Message, Title, Default)
Do ' Outer loop
' We now have user-input values for lownum and hinum. The idea now is to increment lownum until it matches hinum and for as
'long as this loop lasts, to perform a find and replace, feeding the current value of lownum into the findtext.
With Selection.Find
Do While check = True ' Inner loop.
.Text = "[" + lowNum + "],"
.Highlight = True
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
Selection.Find.Execute Replace:=wdReplaceAll
'Exit Do ' Exit inner loop.
If lowNum = hiNum Then
check = False ' Set value of flag to false.
End If
lowNum = lowNum + 1 'Increment lowNum counter.
Loop
End With
Loop Until check = False ' Exit Outer loop immediately
End Sub
I have tried to construct a Do..While loop to run the Find and Replace until Low Number = High Number. I have a number of problems - the loop is endless at the moment and sometimes I get a mismatch Error 13 caused I think by the type of variable i have chosen to store lownum and hinum (i've been round this so many times, I am now confused). Any help would be VERY gratefully accepted. I copy the code below.
Sub Step4_1()
' Step4_1 Macro
Dim Message, Title, Default, lowNum As String, hiNum As String, check
check = True: lowNum = 0: hiNum = 0 'Initialise variables
Message = "Enter the lower section number to remove - DO NOT ENTER THE BRACKETS" 'Set prompt
Title = "User Input Low Section Number" ' Set title.
Default = "0" ' Set default.
' Display message, title, and default value.
lowNum = InputBox(Message, Title, Default)
Message = "Enter the upper section number to remove - DO NOT ENTER THE BRACKETS" 'Set prompt
Title = "User Input High Section Number" ' Set title.
Default = "0" ' Set default.
' Display message, title, and default value.
highNum = InputBox(Message, Title, Default)
Do ' Outer loop
' We now have user-input values for lownum and hinum. The idea now is to increment lownum until it matches hinum and for as
'long as this loop lasts, to perform a find and replace, feeding the current value of lownum into the findtext.
With Selection.Find
Do While check = True ' Inner loop.
.Text = "[" + lowNum + "],"
.Highlight = True
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
Selection.Find.Execute Replace:=wdReplaceAll
'Exit Do ' Exit inner loop.
If lowNum = hiNum Then
check = False ' Set value of flag to false.
End If
lowNum = lowNum + 1 'Increment lowNum counter.
Loop
End With
Loop Until check = False ' Exit Outer loop immediately
End Sub