【发布时间】:2011-12-11 23:41:17
【问题描述】:
我正在尝试将表格从 excel 复制并粘贴到 word 文档中。
我可以手动完成 - 突出显示单元格,CTRL+C,转到单词,CTRL+V。它工作正常。
但是当我编写一个宏来执行它时,单元格的高度是两倍,就像每个单元格中的行高由于某种原因而改变了一样。为什么不一样?我记录了手动过程,调用的是相同的函数(PasteExcelTable)。
Set wordDoc = wordApp.Documents.Open(wordDocPath)
With wordDoc
' cost report
Dim wordRng As Word.Range
Dim xlRng As Excel.Range
Dim sheet As Worksheet
Dim i As Integer
Dim r As String
'Copy the cost report from excel sheet
Set sheet = ActiveWorkbook.Sheets("COST REPORT")
i = sheet.Range("A:A").Find("TOTAL PROJECT COST", Range("A1"), xlValues, xlWhole, xlByColumns, xlNext).row
r = "A11:M" + Trim(Str(i))
Set xlRng = sheet.Range(r)
xlRng.Copy
'Copy and Paste Cost report from Excel
Set wordRng = .Bookmarks("CostReport").Range 'remember original range
If .Bookmarks("CostReport").Range.Information(wdWithInTable) Then
.Bookmarks("CostReport").Range.Tables(1).Delete
End If
.Bookmarks("CostReport").Range.PasteExcelTable False, False, False
.Bookmarks.Add "CostReport", wordRng 'reset range to its original positions
End With
【问题讨论】:
-
您可以check the documentation 并尝试使用
true作为最后两个参数之一,看看是否可以解决问题。 -
如果我将最后一个设置为 true (RTF),它看起来会有所不同,但看起来仍然不像 CTRL+V。但是我在将代码粘贴为 HTML 后更新了代码格式,非常接近。