I wrote this macro to export an Excel sheet into a word table:
我编写这个宏将Excel表导出到word表中:
Private Sub Export_Click() 'export to word table
Dim objWord As Object
Dim objDoc As Object
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Sheets("export").Visible = True 'make hidden sheet visible
Set objDoc = objWord.documents.Add()
LastRow = Sheets("export").Range("$G$1").Value 'number of lines to export
For i = 1 To LastRow
Sheets("export").Rows(i).EntireRow.Copy
objWord.Selection.Paste
Next i
Application.CutCopyMode = False 'clear the clipboard
Sheets("export").Visible = 2 'hide the sheet
End Sub
The result is a weird formatted table with cells getting wider and wider toward the bottom, while the original sheet has the same formatting on all the rows and columns.
结果是一个奇怪的格式化表,单元格向底部越来越宽,而原始的表对所有的行和列都有相同的格式。
How can I solve this?
我怎么解决这个问题?
1 个解决方案
#1
1
Instead of exporting table row by row, you can copy and paste the whole table at once.
与逐行导出表不同,您可以一次复制并粘贴整个表。
Sub Export_Click()
'export to word table
Dim objWord As Object
Dim objDoc As Object
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Sheets("export").Visible = True 'make hidden sheet visible
Set objDoc = objWord.Documents.Add()
lastrow = Sheets("export").Range("$G$1").Value 'number of lines to export
Range("A1:F" & lastrow).Copy 'mention the number of columns you want to copy
With objWord
.Documents.Add
.Selection.Paste
.Visible = True
End With
Application.CutCopyMode = False 'clear the clipboard
Sheets("export").Visible = 2 'hide the sheet
End Sub
#1
1
Instead of exporting table row by row, you can copy and paste the whole table at once.
与逐行导出表不同,您可以一次复制并粘贴整个表。
Sub Export_Click()
'export to word table
Dim objWord As Object
Dim objDoc As Object
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Sheets("export").Visible = True 'make hidden sheet visible
Set objDoc = objWord.Documents.Add()
lastrow = Sheets("export").Range("$G$1").Value 'number of lines to export
Range("A1:F" & lastrow).Copy 'mention the number of columns you want to copy
With objWord
.Documents.Add
.Selection.Paste
.Visible = True
End With
Application.CutCopyMode = False 'clear the clipboard
Sheets("export").Visible = 2 'hide the sheet
End Sub