Mustapha George
Mustapha George

Reputation: 2527

Write to Word Document from Excel VBA

I am able to write to Word Document from a VBA macro, but I am having trouble with syntax to generate headers, paragraphs, tables etc...

This example writes two lines to a Word Document, but both come out as header style. I would like one line header and one line in paragraph or "normal" style...

   Dim wdApp As Word.Application
   Set wdApp = New Word.Application

   With wdApp
    .Visible = True
    .Activate
    .Documents.Add

    With .Selection        
        .Style = "Heading 1"
        .TypeText ("My Heading")
        .TypeParagraph

        .Style = "Normal"
        .TypeText ("Some regular paragraph text")

    End With

Upvotes: 1

Views: 2353

Answers (1)

Mustapha George
Mustapha George

Reputation: 2527

This does what I want.

With .Selection
    .Style = "Heading 1"
    .TypeText "Header 1"
    .TypeParagraph

    .Style = "Heading 2"
    .TypeText "Header 1.1"
    .TypeParagraph

    .Style = "No Spacing"
    .TypeText "some text"
    .TypeParagraph

    .Style = "Heading 1"
    .TypeText "Header 2"
    .TypeParagraph
End With

Upvotes: 0

Related Questions