将表格导出到单词表会产生奇怪的结果

时间:2016-04-08 12:26:32

标签: excel vba excel-vba

我写了这个宏来将Excel表格导出到单词表中:

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

结果是一个奇怪的格式化table,单元格朝向底部越来越宽,而原始工作表在所有行和列上具有相同的格式。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您可以一次复制并粘贴整个表格,而不是逐行导出表格。

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