Reputation: 81
I am trying to change text in a paragraph using vba word. The following code causes the Next to not go to the next element in the collection.
Sub ReadPara()
Dim myString$
Dim DocPara As Paragraph
For Each DocPara In ActiveDocument.Paragraphs
'Debug.Print DocPara.Range.ParagraphStyle '; " - "; DocPara.Range.Text
If Left(DocPara.Range.ParagraphStyle, Len("Heading")) = "Heading" Then
Debug.Print DocPara.Range.ListFormat.ListString
End If
'This section does not go to the next element in the collection
If InStr(DocPara.Range.Text, "HW") > 1 Then
Debug.Print DocPar; qa.Range.Text
myString$ = DocPara.Range.Text
DocPara.Range.Text = myString$ & "Changed"
' Debug.Print DocPara.Range.Text
End If
Next DocPara
End Sub
Upvotes: 1
Views: 24537
Reputation: 2683
Instead of using
For Each DocPara In ActiveDocument.Paragraphs
'Rest of your code it here
Next DocPara
Use
For p = 1 to ActiveDocument.Paragraphs.Count
'Rest of your code it here
'use the script below when refering the the specific paragraph
ActiveDocument.Paragraphs(p).
Next p
Upvotes: 0
Reputation: 81
Now the code works except I still don't go to the next paragraph. I seem to stay on the same paragraph. The next DocPara isn't working like I would expect.
Option Explicit
Sub ReadPara()
Dim myString$
Dim myHeading$
Dim DocPara As Paragraph
For Each DocPara In ActiveDocument.Paragraphs
If Left(DocPara.Range.ParagraphStyle, Len("Heading")) = "Heading" Then
myHeading$ = DocPara.Range.ListFormat.ListString
ElseIf InStr(DocPara.Range.Text, "HW") > 1 Then
myString$ = DocPara.Range.Text
With DocPara.Range
myString$ = Replace(myString, "HW", "HW-" & myHeading$)
.Text = myString$
.Style = ActiveDocument.Styles("Normal")
End With
End If
Next DocPara
End Sub
Upvotes: 2
Reputation: 5385
Second problem:
ParagraphStyle
is read-only, use Style
instead. Both are of type Variant
, so you don't use Set
.
Try this:
DocPara.Range.Style = ActiveDocument.Styles("Normal")
Upvotes: 3
Reputation: 81
I have overcome my first problem and no it wasn't the typo. The typo was just in the message and not in my code. Now I just can't seem to change the style of the newly modified paragraph.
Option Explicit
Sub ReadPara()
Dim myString$
Dim myHeading$
Dim DocPara As Paragraph
For Each DocPara In ActiveDocument.Paragraphs
If Left(DocPara.Range.ParagraphStyle, Len("Heading")) = "Heading" Then
myHeading$ = DocPara.Range.ListFormat.ListString
ElseIf InStr(DocPara.Range.Text, "HW") > 1 Then
myString$ = DocPara.Range.Text
myString$ = Replace(myString, "HW", "HW-" & myHeading$)
DocPara.Range.Text = myString$
'The line below doesn't work at all
Set DocPara.Range.ParagraphStyle = ActiveDocument.Styles("Normal")
End If
Next DocPara
End Sub
Upvotes: 2
Reputation: 5385
The following line will cause an error and (depending on your error handling) probably causes execution to break out of the loop:
Debug.Print DocPar; qa.Range.Text
If you enter Option Explicit
in the top of each code module (in order to force every variable to be explicitly declared), this kind of error will probably be found earlier. :)
Upvotes: 1