Consulting

Results 1 to 5 of 5

Thread: SaveAs with embedded Word document - OLEObjects

  1. #1

    SaveAs with embedded Word document - OLEObjects

    Hi,

    I have a Word Document embedded as OLEObject in an Excel sheet. What I want to do is to store this embedded object as a file on the disc using VBA.

    I use the following code to access and store the embedded object:

    [VBA]
    Dim oleObject As Object
    Dim wordDocument As Object

    Set oleObject = ActiveWorkbook.Sheets(sheetName).OLEObjects(1)

    Set wordDocument = oleObject.Object

    wordDocument.SaveAs ("some filename")
    [/VBA]

    The previous code works well if double-click the embedded OLE object, thus get the focus, and then deactivate it by clicking on the Excel sheet. However, after a re-start of Excel, it doesn't work, so I have to double-click the object and then click on the sheet again in order to get it work. Otherwise I get a runtime error.

    I also tried to .Activate and set the .Verb property, with no success. Anybody has a solution for that?

    Thanks,
    Christoph

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,060
    Location
    Are you sure that this is all the relevant code?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  3. #3
    Yes, pretty sure. I made a simple excel file for you: Just click the button, this will execute the code that I posted. I tried Office 2000 and 2003. If I click the button after start up of Excel, it fails. After double-clicking the OLE object and then clicking the sheet (so we lose the focus on the OLE object), the code works and I get a Word document on the disc.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    This seems to work

    [vba]

    Private Sub CommandButton1_Click()
    Dim oleObject As Object
    Dim wordDocument As Object

    Set oleObject = ActiveWorkbook.Sheets("Sheet1").OLEObjects(1)
    oleObject.Verb Verb:=xlPrimary
    ActiveSheet.Range("A1").Select

    Set wordDocument = oleObject.Object

    wordDocument.SaveAs ("C:\test.doc")

    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Thank you. That's working fine.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •