简体   繁体   中英

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.

  • If I copy the contents to a Word document and paste them from there to an html form, it works.
  • If I copy the contents directly from the spreadsheet into the html form, the formatting is lost.
  • If the range is in the clipboard after copying from a Word document, I can paste it to the html form.

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...

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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