TLewis
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.A lignment = wdAlignParagraphCenter
.Content.Font.Size = 18
.Content.Font.Bold = wdToggle
.Content.InsertParagraphAf ter
.Content.InsertAfter ("Font Should Be 11")
.Content.ParagraphFormat.A lignment = wdAlignParagraphCenter
.Content.Font.Size = 11
.Content.Font.Bold = wdToggle
.Content.InsertParagraphAf ter
.SaveAs (DirectoryName & "Test.doc")
.Close ' close the document
End With
wrdApp.Quit ' close the Word application
Set wrdDoc = Nothing
Set wrdApp = Nothing
End Sub
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.A
.Content.Font.Size = 18
.Content.Font.Bold = wdToggle
.Content.InsertParagraphAf
.Content.InsertAfter ("Font Should Be 11")
.Content.ParagraphFormat.A
.Content.Font.Size = 11
.Content.Font.Bold = wdToggle
.Content.InsertParagraphAf
.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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thanks!