2017-07-14 3 views
-1

私は会社の内部コンテンツシステムのための標準的なWordのdoc形式をwikiformattingに変換するスクリプトを作成しようとしています。例えば:これは太字で表示されWord VBA:単語に書式が設定されているかどうかを確認し、それがあれば修正できますか?

=>

「__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 

答えて

1

が、これは

iが「太字の書式を検索するためのマクロを記録することで開始してみてください「(なし検索テキスト...フォーマット...フォント...太字)

その後、使用されている 『Selection.Find「プログラムは直後に一時停止されたときに、オブジェクト」選択を』検討する」ウィンドウを見ます.Execute "行

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 
+0

これは機能します。ありがとう! – DukeSilver

関連する問題