此问题的答案(Exporting a sheet to a word table gives a weird outcome)中建议的代码不会使表格适合边距,导致表格的一部分被隐藏
如何强制桌子适合边距?
我尝试了objDoc.Tables.AutoFitBehavior (wdAutofitWindow)
但它没有用。
答案 0 :(得分:1)
Sub Export_Click()
Dim objWord As Word.Application
Dim myDoc As Word.Document
Dim myTable As Word.Table
Dim myRange As Excel.Range
Dim lastRow As Long
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set myDoc = objWord.Documents.Add
lastRow = Sheets("export").Range("$G$1").Value 'number of lines to export
Set myRange = Range("A1:F" & lastRow)
myRange.Copy
myDoc.Paragraphs(1).Range.PasteExcelTable _
LinkedToExcel:=False, _
WordFormatting:=False, _
RTF:=False
Set WordTable = myDoc.Tables(1)
WordTable.AutoFitBehavior (wdAutoFitWindow)
Application.CutCopyMode = False 'clear the clipboard
End Sub
此代码基于您的last question。
要运行上述代码,您必须添加 Microsoft Word 12.0对象库的引用。您可以在VBE
中执行此操作。在Tools
菜单中点击References
,然后选择Microsoft Word 12.0 Object Library