Excel VBA按钮复制工作表到新的Word文档

时间:2017-03-15 18:22:14

标签: excel vba excel-vba ms-word worksheet

下面的代码会复制工作表中的表格并将其粘贴到新的Microsoft Word文档中。唯一的问题是,当粘贴在word文档中时,它会切断一半的表。有什么建议?感谢。

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

1 个答案:

答案 0 :(得分:1)

您可以使用Word AutoFit命令使表格适合文档。我已经包含了将方向交换为Landscape的代码,但这可能没有必要。

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