简体   繁体   中英

Copy content of word OLE object without opening, in Excel with VBA

I have an Excel sheet with Microsoft Word OLE objects embedded.

My embedded Word documents have some fields that should been updated with specific cells.

I need to print the content of that embedded document, without visible document opening and " update document prompts ".

My problem is in copying the content of the Word-embedded OLE object in an invisible Word document without update prompting.

I try this:

This code paint a box around the embedded word document in destination printable document.

Please help me copy content of embedded document or ..., and print an embedded document without updating prompt and visible window.

Sub PrintIt(P As String, w, h As Double)
    Dim objWord As Object
    Dim ObjDoc As Object

    Application.ScreenUpdating = False

    Set objWord = CreateObject("Word.Application")
    objWord.Visible = False

    ActiveSheet.OLEObjects(P).Copy

    Set ObjDoc = objWord.Documents.Add
    ObjDoc.PageSetup.PageWidth = objWord.CentimetersToPoints(w)
    ObjDoc.PageSetup.PageHeight = objWord.CentimetersToPoints(h)
    ObjDoc.Content.Paste

    ObjDoc.PrintOut Background:=False
    ObjDoc.PrintOut

    objWord.Quit SaveChanges:=False
    Application.ScreenUpdating = True
End Sub 'Print it
Sub PrintIt(P As String)
Dim objWord As Object
Dim ObjDoc As Object
Dim Oshp As Object

Application.ScreenUpdating = False

ActiveSheet.OLEObjects(P).Activate
Set objWord = GetObject(, "Word.Application")
objWord.Visible = False

Set ObjDoc = objWord.ActiveDocument

ObjDoc.Fields.Update
For Each Oshp In ObjDoc.Content.ShapeRange
    Oshp.TextFrame.TextRange.Fields.Update
Next

ObjDoc.PrintOut Background:=False
ObjDoc.PrintOut

objWord.Quit SaveChanges:=False
Application.ScreenUpdating = True
End Sub 'Print it

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