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
This code is based on your last question.
For above code to run you'll have to add reference of Microsoft Word 12.0 Object Library. You can do this in VBE
. In Tools
menu click References
and select Microsoft Word 12.0 Object Library
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…