Excel VBAbutton将工作表复制到新的Word文档
下面的代码复制工作表中的表格,并将其粘贴到新的Microsoft Word文档中。 唯一的问题是,当粘贴在word文档中时,它会切断一半的表格。 有什么build议么? 谢谢。
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
我正在使用的工作表
您可以使用Word AutoFit
命令使表格适合文档。 我已经包含代码交换方向到景观,但这可能不是必要的。
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