找到文本的更好方法,选择段落开始,然后重新格式化
我正在写一个宏来找到一个开放的括号,从该字符开始到段落的开始,然后将所选文本重新格式化为小帽子。公平的警告,我对VBA一无所知,我从网上找到的示例中将这些宏删除在一起。
宏的最后一部分将光标移至下一段的开始,以便我可以再次运行。我想循环,但是我还没有想出如何为循环设置停止点。
以下宏有效,但是必须有一种更好的方法来做到这一点。此方法会产生大量的屏幕闪烁,并永远需要运行。
Sub References()
'
' References Macro
'this part selects text until it finds an open parenthesis
Dim flag As Boolean
flag = True
While flag = True
Selection.MoveRight Unit:=wdCharacter, Count:=1, _
Extend:=wdExtend
'checks the last character to see if its a open parenthesis
If Strings.Right(Selection.Range.Text, 1) = "(" Then
'if it was an open parenthesis the loop flag will end
flag = False
End If
Wend
'this part reformats the text
With Selection.Font
.Name = "Times New Roman"
.Size = 12
.Bold = False
.Italic = False
.Underline = wdUnderlineNone
.UnderlineColor = wdColorAutomatic
.StrikeThrough = False
.DoubleStrikeThrough = False
.Outline = False
.SmallCaps = True
.AllCaps = False
.Color = wdColorAutomatic
.Spacing = 0
.Scaling = 100
.Position = 0
.Kerning = 1
End With
'this part moves the cursor to the beginning of the next paragraph
Selection.HomeKey Unit:=wdLine
Selection.MoveDown Unit:=wdParagraph, Count:=1
End Sub
I'm writing a macro in Word to find an open parenthesis, select from that character to the start of the paragraph, and then reformat the selected text into small caps. Fair warning, I know nothing about VBA and I hack these macros together from examples I find online.
The last part of the macro moves the cursor to the start of the next paragraph so that I can run it again. I'd like to loop it, but I haven't figured out how to set a stopping point for the loop yet.
The following macro works, but there has to be a better way to do it. This method results in a lot of screen flicker and takes forever to run.
Sub References()
'
' References Macro
'this part selects text until it finds an open parenthesis
Dim flag As Boolean
flag = True
While flag = True
Selection.MoveRight Unit:=wdCharacter, Count:=1, _
Extend:=wdExtend
'checks the last character to see if its a open parenthesis
If Strings.Right(Selection.Range.Text, 1) = "(" Then
'if it was an open parenthesis the loop flag will end
flag = False
End If
Wend
'this part reformats the text
With Selection.Font
.Name = "Times New Roman"
.Size = 12
.Bold = False
.Italic = False
.Underline = wdUnderlineNone
.UnderlineColor = wdColorAutomatic
.StrikeThrough = False
.DoubleStrikeThrough = False
.Outline = False
.SmallCaps = True
.AllCaps = False
.Color = wdColorAutomatic
.Spacing = 0
.Scaling = 100
.Position = 0
.Kerning = 1
End With
'this part moves the cursor to the beginning of the next paragraph
Selection.HomeKey Unit:=wdLine
Selection.MoveDown Unit:=wdParagraph, Count:=1
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
例如,使用查找(比测试每个字符更快):
For example, using Find (which is way faster than testing every character):