简体   繁体   English

将 Excel 范围复制到 Word 文档,包括其隐藏的子范围

[英]Copy Excel range to Word document, including its hidden sub-range

I am trying to copy an Excel Range, using the.Copy method, which has hidden and not hidden parts.我正在尝试使用 .Copy 方法复制 Excel 范围,该方法具有隐藏部分和非隐藏部分。 But, after copy the range, when I paste it in a word.docx document, only the not hidden parts are pasted.但是,复制范围后,当我将其粘贴到 word.docx 文档中时,只会粘贴未隐藏的部分。

Is it possible paste the whole range, including the hidden parts?是否可以粘贴整个范围,包括隐藏部分?

Even if I make hidden = false, then copy, then hidden = true, it doesn't work.即使我让hidden = false,然后复制,然后hidden = true,它也不起作用。 It still pastes only not hidden parts.它仍然只粘贴不隐藏的部分。

The code I have now is as follows.我现在的代码如下。 Macro2 and Macro3 do not work, only Macro4 works for me, but Rows(1) have to be not hidden, which I don't want. Macro2Macro3不起作用,只有Macro4对我有用,但Rows(1)必须不隐藏,这是我不想要的。

Sub Macro1()
    Rows(1).Hidden = True
    Rows(2).Hidden = False
End Sub

Sub Macro2()
    Range(Cells(1, 1), Cells(2, 1)).Copy
End Sub

Sub Macro3()
    Rows(1).Hidden = False
    Range(Cells(1, 1), Cells(2, 1)).Copy
    Rows(1).Hidden = True
End Sub

Sub Macro4()
    Rows(1).Hidden = False
    Range(Cells(1, 1), Cells(2, 1)).Copy
End Sub

An InlineShape / OLEFormat approach InlineShape / OLEFormat方法

The code below functions from Excel (with the use of the Microsoft Word ##.0 Object Library reference) by hooking into a word document, and embedding an Excel document as an InlineShape .下面的代码从 Excel(使用Microsoft Word ##.0 Object Library参考)通过挂钩到 Word 文档,并将 Excel 文档作为InlineShape嵌入。 This object is then editted directly, allowing for the data to be copied over.然后直接编辑此 object,允许复制数据。

There may still be a simpler option out there, but this was the best that I could come up with.那里可能还有一个更简单的选择,但这是我能想到的最好的选择。

Public Function CopyToWord(ByRef rng As Excel.Range, ByRef wd As Word.Document) _
                    As Word.InlineShape
    
    Dim wd_wb   As Excel.Workbook   '' word workbook
    Dim wd_ws   As Excel.Worksheet  '' word wordsheet
    Dim ole_xl  As Word.OLEFormat   '' the format object that holds the above
    Dim wd_is   As Word.InlineShape '' the shape object that holds the above
  
    '' add inline shape
    Set wd_is = wd.Content.InlineShapes.AddOLEObject( _
                ClassType:="Excel.Sheet.12", _
                Filename:="", _
                LinkToFile:=False, _
                DisplayAsIcon:=False)
    Set ole_xl = wd_is.OLEFormat    '' get format
    Call ole_xl.Activate            '' start editing
    Set wd_wb = ole_xl.Object       '' get workbook
    Set wd_ws = wd_wb.Sheets(1)     '' get worksheet
    
    '' actually do the copying
    Call rng.Copy(Destination:=wd_ws.[A1].Resize(rng.Rows.Count, rng.Columns.Count))

    Call wd_wb.Close                '' stop editting
    Set CopyToWord = wd_is          '' return inline shape object

End Function

Here is a demo subroutine that shall show that the function这是一个演示子程序,它将显示 function

Public Sub demo()
  
    [A1] = "Sneaky"
    [A:A].EntireRow.Hidden = True
  
    Dim word_doc As New Word.Document
    Let word_doc.Application.Visible = True

    Call CopyToWord(rng:=[A1:B2], wd:=word_doc) '' or `CopyToWord [A1:B2], word_doc`

End Sub

A useful reference有用的参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM