我正在尝试创建一个脚本,它将常规 Word 文档格式转换为我公司内部内容系统的 wikiformatting。例如:
这是粗体=> "__这是粗体__"
我一直在尝试通过 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
试试这个
我首先录制了一个宏来搜索“粗体格式”(没有搜索文本...格式...字体...粗体)
然后在程序在“ Selection.Find.Execute ”行之后暂停时使用“观察窗口”检查“ Selection ”对象
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句