使用 vba word 更改段落中的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11996956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing text in a paragraph using vba word
提问by engineer Mike
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.
我正在尝试使用 vba word 更改段落中的文本。以下代码导致 Next 不转到集合中的下一个元素。
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
回答by Olle Sj?gren
Second problem:
第二个问题:
ParagraphStyle
is read-only, use Style
instead. Both are of type Variant
, so you don't use Set
.
ParagraphStyle
是只读的,请Style
改用。两者都是 type Variant
,所以你不要使用Set
.
Try this:
尝试这个:
DocPara.Range.Style = ActiveDocument.Styles("Normal")
回答by engineer Mike
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.
现在代码可以工作了,但我仍然没有进入下一段。我似乎停留在同一段。下一个 DocPara 没有像我期望的那样工作。
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
回答by engineer Mike
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
回答by Olle Sj?gren
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. :)
如果Option Explicit
在每个代码模块的顶部输入(为了强制每个变量都显式声明),这种错误可能会更早发现。:)
回答by Jean-Pierre Oosthuizen
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