简体   繁体   中英

Excel VBA Button Copy WorkSheet to New Word Doc

The code below copies a table I have in a work sheet and pastes it in a new Microsoft Word Document. The only issue is it cuts off half the table when pasted in the word document. Any suggestions? Thanks.

Sub btnExport()
Dim objWord As Word.Application
Range("C2:D60").Copy

Set objWord = CreateObject("Word.Application.14")
With objWord
    .Documents.Add
    .Visible = True
    .Selection.Paste
End With
End Sub

Sheet I'm using

You can use the Word AutoFit command to make the table fit into the document. I've included code to swap the orientation to Landscape as well but this may not be necessary.

Sub btnExport()
    Dim objWord As Word.Application
    Range("C2:D60").Copy ' you should name the worksheet as well here really

    Set objWord = CreateObject("Word.Application.14")
    With objWord
        .Documents.Add
        .ActiveDocument.PageSetup.Orientation = 1 'wdOrientLandscape
        .Visible = True
        .Selection.Paste
        .ActiveDocument.Tables(1).AutoFitBehavior 2 'wdAutoFitWindow
    End With
End Sub

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