JeroenEijkhof
JeroenEijkhof

Reputation: 2222

Can I font format the output of a word macro?

I have a document with comments on a long interview transcript. I found a Macro on SO that let's me export those comments with the highlighted text. This is awesome but the output is terribly dull (plain text).

I need to know if and how to apply bold, italic and insert newlines. I have looked for like an hours now and because my VBA is terrible I have no reference for where to look other than keyword searches on "marco output formatting"

Does someone know how to take the below script and font changes to parts of the text?

Sub ExportComments()
    Dim s As String
    Dim cmt As Word.Comment
    Dim doc As Word.Document

    For Each cmt In ActiveDocument.Comments
        s = s & "Text: " & cmt.Scope.FormattedText & " -> "
        s = s & "Comments: " & cmt.Initial & cmt.Index & ":" & cmt.Range.Text & vbCr
    Next

    Set doc = Documents.Add
    doc.Range.Text = s
End Sub

Maybe I can do it with HTML interpreted by Word?

Upvotes: 1

Views: 734

Answers (1)

Christina
Christina

Reputation: 1379

I'm assuming that the formatting you want included is already within the comment text, and that you are just looking for a way to get that into your final document. Here is a modified version of your script that will do that (with one caveat, listed below):

Sub ExportComments()
Dim cmt As Comment
Dim newdoc As Document
Dim currDoc As Document

Set currDoc = ActiveDocument
Set newdoc = Documents.Add
currDoc.Activate
For Each cmt In currDoc.Comments
    With newdoc.Content
        cmt.Scope.Copy
        .InsertAfter "Text: "
        .Collapse wdCollapseEnd
        .Paste
        .InsertAfter " - > "
        cmt.Range.Copy
        .InsertAfter "Comments: " & cmt.Initial & cmt.Index & ":"
        .Collapse wdCollapseEnd
        .Paste
        .InsertParagraphAfter
    End With
Next

End Sub

The difference here is that I'm using Copy and Paste rather than generating text strings.

Caveat: As the macro is written right now, any character formatting from the Scope (the text that appears next to Text in your file) will be applied to the arrow and the initials as well. This is pretty easy to fix with a search and replace, so I didn't incorporate it into the script.

Upvotes: 1

Related Questions