Link to home
Create AccountLog in
Avatar of TLewis
TLewisFlag for United States of America

asked on

Excel VBA How to format Word document

I am creating a document from an Excel spreadsheet.  All of that works fine.  However, I need to format different lines differently.  Whenever I run the code, it changes the format of everything in the document to whatever I set the current font to be.  I have pasted a test example below.  What am I missing?
Thanks for any help!


Sub CreateNewWordDoc()
    Dim wrdApp As Word.Application
    Dim wrdDoc As Word.Document
    Dim DirectoryName As String
    DirectoryName = "C:\Temp\"

    Set wrdApp = New Word.Application
    wrdApp.Visible = True
   
    Set wrdDoc = wrdApp.Documents.Add
    With wrdDoc
         
          .Content.InsertAfter ("Font Should Be 18")
          .Content.ParagraphFormat.Alignment = wdAlignParagraphCenter
          .Content.Font.Size = 18
          .Content.Font.Bold = wdToggle
          .Content.InsertParagraphAfter
                   
          .Content.InsertAfter ("Font Should Be 11")
          .Content.ParagraphFormat.Alignment = wdAlignParagraphCenter
          .Content.Font.Size = 11
          .Content.Font.Bold = wdToggle
          .Content.InsertParagraphAfter
 
          .SaveAs (DirectoryName & "Test.doc")
          .Close ' close the document
    End With

    wrdApp.Quit ' close the Word application
    Set wrdDoc = Nothing
    Set wrdApp = Nothing
End Sub
ASKER CERTIFIED SOLUTION
Avatar of andrewssd3
andrewssd3
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of TLewis

ASKER

Excellent!  The only thing that I had to change was the. SaveAs and .Close to .ActiveDocument.SaveAs and .ActiveDocument.Close

Thanks!