简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM