Word VBA:如何判断单词是否有格式,如果有,则对其进行修改?



我正在尝试创建一个脚本,该脚本将常规的Word文档格式转换为我公司内部内容系统的wiki格式。例如:

这是粗体=> "__This是bolded__">

我一直在尝试通过Word VBA来做到这一点,但我正在努力取得任何进展。(就其价值而言,我对Excel VBA非常熟悉。这是我到目前为止所拥有的:

Sub convertFormatting()
For Each sentence In ActiveDocument.StoryRanges
For Each w In sentence.Words
If w.Font.Bold = True Then
newWord = "__" & w
Debug.Print "Great, I figured out that it's bold. Now what?"
End If
Next
Next
End Sub

试试这个

我首先录制一个宏来搜索"粗体格式"(没有搜索文本......格式。。。字体。。。粗体(

然后使用"监视窗口"检查"选择">对象,当程序在">选择.查找.执行"行之后暂停时

Sub convertBold2wiki()
With Selection.Find             ' this sets up the "find" parameters
.ClearFormatting
.Font.Bold = True
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
End With
Dim txt As String
Dim found As Boolean
Do While True
found = Selection.Find.Execute    ' returns false on "not found"
If Not found Then Exit Do
txt = Selection.Range.Text        ' this is the found text
Debug.Print txt
Debug.Print Selection.Start
Debug.Print Selection.End

Selection.Font.Bold = False       ' un-bold the found text, otherwise ... well you know
If Len(Selection.Text) > 0 Then   ' make sure that you are not inserting text at cursor
Selection.Range.Text = "__" & txt & "__"
End If
Loop
End Sub

最新更新