How to Embed Images in Word 2000 documents as OLE objects through VB6 code?

Posted on 2007-11-29
Last Modified: 2013-11-25

I've got a problem with the attached code. What I'm trying to do is loop through adding external files as images embedded in a word 2000 document in vb6 code.

In my code - below - you can see that I set the linktofile flag to be false....

srcRange.InlineShapes.AddOLEObject fileName:=exportPath, LinkToFile:=False, DisplayAsIcon:=False, range:=range

yet when I run the following code:

wordDoc.SaveAs sfd.fileName, AddToRecentFiles:=False

I get a word document created, with a subdirectory and all the image files stored in there - referenced by the parent word document.

Any help would be greatly appreciated!

Public Sub OnSavingWordDocument(ByVal wordDoc As Word.Document, ByVal doc As DOMDocument)

    Dim fso As New FileSystemObject

    Dim ts As TextStream

    Dim nodes As MSXML2.IXMLDOMNodeList

    Dim nodeCount As Long

    Dim srcRange As Word.range


    Set nodes = doc.selectNodes(mXPath)

    Set srcRange = wordDoc.Content

    For nodeCount = 1 To nodes.length Step 1

        If (FindToken(srcRange, mToken)) Then

            srcRange.Text = ""

            Dim exportPath As String

            exportPath = GetTempFile("cdxml")

            Set ts = fso.CreateTextFile(exportPath, True, True)

            ts.Write nodes(nodeCount - 1).Text



            Dim range As Word.range

            Set range = srcRange

            srcRange.InlineShapes.AddOLEObject fileName:=exportPath, LinkToFile:=False, DisplayAsIcon:=False, range:=range

            srcRange.End = wordDoc.Content.End

            fso.DeleteFile exportPath, True

        End If

    Next nodeCount

End Sub


OnSavingWordDocument wordDoc, doc

wordDoc.SaveAs sfd.fileName, AddToRecentFiles:=False


Open in new window

Question by:rickbarnsley
LVL 38

Expert Comment

Comment Utility
Have you tried the InlineShapes.AddPicture method:

srcRange.InlineShapes.AddPicture exportPath

I'm not sure why the OLE object is creating links when you specify LinkToFile=False.  I'm not able to replicate that behaviour.  What kind of images are they?

Author Comment

Comment Utility
Solved it -

I changed...
                    wordDoc.SaveAs sfd.fileName, AddToRecentFiles:=False
                    wordDoc.SaveAs sfd.fileName, FileFormat:=wdFormatDocument, AddToRecentFiles:=False

Can't explain it, but it works now! Cheers

Accepted Solution

Computer101 earned 0 total points
Comment Utility
PAQed with points refunded (500)

EE Admin

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
A short article about a problem I had getting the GPS LocationListener working.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now