ridoco
09-16-2022, 03:10 PM
Hello, I'm really new at this, and I'm trying to build a macro that can do a repetitive activity that I do at work for me.
It's supposed to take a table that we have and add a row to it with specific minor changes, and I've got it to work mostly.
I haven't included all the code below, but the relevant bits. So the code checks each paragraph to see if it's a certain kind of paragraph, and if it is then it executes a given action. DiscPara, for example, checks to see if the paragraph contains {{Disc}}, and if it does, it moves the selection until it finds the "!", then moves down from that and copies and pastes the value.
The problem is that {{Disc}} appears 26 times in my document, but because of the way I have it coded right now, it begins the selection at the first instance of {{Disc}} instead of the paragraph that the loop is currently on. It looks in the string of allWords for the first instance {{Disc}} and then performs the desired function on the first instance instead of on the second, third, etc. The same goes for lines containing ! WMS & i.
I've been thinking to possibly separate it out of the loop, so it's something that happens after everything else in the document. In either case, though, I'm not sure how to get it to perform the desired function at the paragraph I want it to instead of at the first instance of the paragraph.
Sub UPdate()
ActiveDocument.PageSetup.LineNumbering.Active = False
allWords = ActiveDocument.Content.Text
i = "11.1"
j = "11.2"
myText = myPara.Range.Text
For Each myPara In ActiveDocument.Paragraphs
DiscPara:
If InStr(myText, "{{Disc}}") > 0 Then
With Selection
.Start = Selection.Start
.End = Selection.Start
.Collapse wdCollapseStart
Do While InStr("!", Left(Selection.Text, 1)) = 0
.MoveStart , -1
Loop
.Collapse wdCollapseStart
.MoveStart wdParagraph, 1
.Expand wdParagraph
.Copy
.MoveUp wdParagraph, 1
.Paste
End With
DoEvents
GoTo StartUpdate
Else: GoTo HeadPara
End If
HeadPara:
If InStr(myText, "! ABC " & i) > 0 Then
With Selection
.Start = Selection.Start
.End = Selection.Start
.Collapse wdCollapseStart
.Expand wdParagraph
.Copy
.MoveUp wdParagraph, 1
.Paste
.Start = InStr(allWords, myText) + 2
.End = InStr(allWords, myText) + 3
.Expand wdParagraph
End With
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = i
.Wrap = wdFindStop
.Forward = True
.Replacement.Text = j
.MatchWildcards = False
.Execute Replace:=wdReplaceAll
End With
DoEvents
GoTo StartUpdate
End If
StartUpdate:
Next myPara
End Sub
It's supposed to take a table that we have and add a row to it with specific minor changes, and I've got it to work mostly.
I haven't included all the code below, but the relevant bits. So the code checks each paragraph to see if it's a certain kind of paragraph, and if it is then it executes a given action. DiscPara, for example, checks to see if the paragraph contains {{Disc}}, and if it does, it moves the selection until it finds the "!", then moves down from that and copies and pastes the value.
The problem is that {{Disc}} appears 26 times in my document, but because of the way I have it coded right now, it begins the selection at the first instance of {{Disc}} instead of the paragraph that the loop is currently on. It looks in the string of allWords for the first instance {{Disc}} and then performs the desired function on the first instance instead of on the second, third, etc. The same goes for lines containing ! WMS & i.
I've been thinking to possibly separate it out of the loop, so it's something that happens after everything else in the document. In either case, though, I'm not sure how to get it to perform the desired function at the paragraph I want it to instead of at the first instance of the paragraph.
Sub UPdate()
ActiveDocument.PageSetup.LineNumbering.Active = False
allWords = ActiveDocument.Content.Text
i = "11.1"
j = "11.2"
myText = myPara.Range.Text
For Each myPara In ActiveDocument.Paragraphs
DiscPara:
If InStr(myText, "{{Disc}}") > 0 Then
With Selection
.Start = Selection.Start
.End = Selection.Start
.Collapse wdCollapseStart
Do While InStr("!", Left(Selection.Text, 1)) = 0
.MoveStart , -1
Loop
.Collapse wdCollapseStart
.MoveStart wdParagraph, 1
.Expand wdParagraph
.Copy
.MoveUp wdParagraph, 1
.Paste
End With
DoEvents
GoTo StartUpdate
Else: GoTo HeadPara
End If
HeadPara:
If InStr(myText, "! ABC " & i) > 0 Then
With Selection
.Start = Selection.Start
.End = Selection.Start
.Collapse wdCollapseStart
.Expand wdParagraph
.Copy
.MoveUp wdParagraph, 1
.Paste
.Start = InStr(allWords, myText) + 2
.End = InStr(allWords, myText) + 3
.Expand wdParagraph
End With
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = i
.Wrap = wdFindStop
.Forward = True
.Replacement.Text = j
.MatchWildcards = False
.Execute Replace:=wdReplaceAll
End With
DoEvents
GoTo StartUpdate
End If
StartUpdate:
Next myPara
End Sub