将表单导出到单词表中会产生一个奇怪的结果 [英] Exporting a sheet to a word table gives a weird outcome

查看:118
本文介绍了将表单导出到单词表中会产生一个奇怪的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  Private Sub Export_Click()'export to word表
Dim objWord As Object
Dim objDoc As Object
Set objWord = CreateObject(Word.Application)
objWord.Visible = True
表格(export ).Visible = True'make hidden sheet visible
Set objDoc = objWord.documents.Add()
LastRow = Sheets(export)。Range($ G $ 1)。Value'要导出的
对于i = 1 To LastRow
表单(export)。Rows(i).EntireRow.Copy
objWord.Selection.Paste
Next i
Application.CutCopyMode = False'清除剪贴板
表单(export)Visible = 2'隐藏表单
End Sub

结果是格式化的表格细胞越来越宽的底部,而原始表格具有相同的格式在所有的行和列。 p>

我如何解决这个问题?

解决方案

行,您可以一次复制并粘贴整个表格。

  Sub Export_Click()
'导出为单词表
Dim objWord As Object
Dim objDoc As Object
Set objWord = CreateObject(Word.Application)
objWord.Visible = True
表格(export) .Visible = True'使隐藏表单可见
设置objDoc = objWord.Documents.Add()
lastrow =表(export)。Range($ G $ 1)。值'行数出口

范围(A1:F& lastplay).Copy'提及您要复制的列数

带objWord
.Documents.Add
.Selection.Paste
.Visible = True
结束

Application.CutCopyMode = False'清除剪贴板
表格(export)Visible = 2'隐藏表
End Sub


I wrote this macro to export an Excel sheet into a word table:

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?

解决方案

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

这篇关于将表单导出到单词表中会产生一个奇怪的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆