简体   繁体   English

如何临时打开Word文档并使用它复制粘贴范围?

[英]How to temporarily open a Word document and use it to copy-paste a range?

I have formatted cells that I wish to copy and paste into an html form. 我已经格式化了单元格,希望将其复制并粘贴到html表单中。

  • If I copy the contents to a Word document and paste them from there to an html form, it works. 如果我将内容复制到Word文档并将其从那里粘贴到html表单中,那么它将起作用。
  • If I copy the contents directly from the spreadsheet into the html form, the formatting is lost. 如果我直接从电子表格中将内容复制到html表单中,则格式会丢失。
  • If the range is in the clipboard after copying from a Word document, I can paste it to the html form. 从Word文档复制后,如果范围在剪贴板中,则可以将其粘贴到html表单中。

I need a way to copy the range to clipboard and retain the formatting. 我需要一种将范围复制到剪贴板并保留格式的方法。 Since from Word it works, that's where I'm starting... but if there is another way... 由于从Word可以正常工作,所以这就是我的出发点...但是,如果还有其他方法...

I'm thinking of either an embed word file, or a hidden one. 我正在考虑一个嵌入的单词文件,或一个隐藏的文件。

Copy cell, paste there, select all and copy from there. 复制单元格,粘贴到那里,全选并从那里复制。 Afterwards, close/discard it. 之后,关闭/丢弃它。

EDIT: Managed to get some code... but it worked, then stopped, then worked again... no idea why... 编辑:设法获得一些代码...但是它起作用了,然后停止了,然后又起作用了...不知道为什么...

Sub TempDoc()

Dim WDApp As Word.Application
Dim WDDoc As Word.Document

    Application.ScreenUpdating = False

Set WDObj = Sheets("Text2Form").OLEObjects("WDOC")

WDObj.Activate
WDObj.Object.Application.Visible = False

Set WDApp = GetObject(, "Word.Application")
Set WDDoc = WDApp.ActiveDocument

    WDApp.Visible = False

Worksheets("Text2Form").Cells(12, 4).Copy
    WDApp.Selection.Goto What:=wdGoToLine, Which:=wdGoToLast
    WDApp.Selection.PasteSpecial xlPasteValues

WDApp.ActiveDocument.Content.InsertAfter vbNewLine

Worksheets("Text2Form").Cells(14, 4).Copy
    WDApp.Selection.Goto What:=wdGoToLine, Which:=wdGoToLast
    WDApp.Selection.PasteSpecial xlPasteValues

WDDoc.Content.Copy
WDDoc.Content.Delete

WDApp.Quit
Application.ScreenUpdating = True

End Sub

您是否尝试过右键单击要粘贴到的单元格,然后单击“选择性粘贴”?

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

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