1
votes

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
1
Record your steps in Word macro recorder and read the code Word makes. - tony bd
Don't see any issues with your code.. It works fine for me - Dmitry Pavliv
it works, but everything is styled as heading-1. - Mustapha George
I've Heading 1 style for first row and Normal style for second: i.stack.imgur.com/TvFZL.png - Dmitry Pavliv
simco - true, I simplified example and it works now. - Mustapha George

1 Answers

0
votes

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