Solved

getting the following error in word 2010 vba Run-time error '-2147467259(80004005)'" Method 'AddPicture' of object 'InlineShapes' failed

Posted on 2013-06-26
22
1,899 Views
Last Modified: 2013-06-27
Got beautiful code from the amazing macroshadow may a thousand blessings come upon him! But its erring on my machine here is the error
 Run-time error '-2147467259(80004005)'"
Method 'AddPicture' of object 'InlineShapes' failed

here is the code
With Selection
        .HomeKey Unit:=wdStory

        With .Find
            .ClearFormatting
            .Text = strTextToReplace
            ' Loop until Word can no longer
            ' find the search string, inserting the specified image at each location
            Do While .Execute
                Selection.MoveRight
                Selection.InlineShapes.AddPicture FileName:=strImagePath, LinkToFile:=False, SaveWithDocument:=True
            Loop
        End With
    End With

Open in new window

yes the variables were declared and filled with apropriate data
0
Comment
Question by:Dov_B
  • 12
  • 10
22 Comments
 
LVL 26

Expert Comment

by:MacroShadow
Comment Utility
What kind of file are you inserting?
0
 
LVL 26

Expert Comment

by:MacroShadow
Comment Utility
Try changing the name of your picture.
0
 

Author Comment

by:Dov_B
Comment Utility
hmmm didn't think of that wait a second it is aaaaaa .jpg I will upload it now
th.jpg
0
 
LVL 26

Expert Comment

by:MacroShadow
Comment Utility
Put a break point on line 11, what name is the strImagePath variable holding?
0
 

Author Comment

by:Dov_B
Comment Utility
I added a watch and put in the break point copied the path from the watch aand here is what I got
"C:\Documents and Settings\Dov\My Documents\Downloads\th.jpg"
0
 
LVL 26

Expert Comment

by:MacroShadow
Comment Utility
hmm.
0
 

Author Comment

by:Dov_B
Comment Utility
well I added this and it seems to have started to work
ActiveDocument.SetCompatibilityMode wdWord2003
0
 
LVL 26

Accepted Solution

by:
MacroShadow earned 500 total points
Comment Utility
Do you have the latest updates installed?
0
 

Author Comment

by:Dov_B
Comment Utility
not sure but guess what I forgot to mention being the clever kind of guy that I am....I am using xp sorry for wasting your time!
0
 
LVL 26

Expert Comment

by:MacroShadow
Comment Utility
So am I.
0
 

Author Comment

by:Dov_B
Comment Utility
darn well maybe I'm not up to date on the updates
0
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!

 
LVL 26

Expert Comment

by:MacroShadow
Comment Utility
Check it out on http://update.microsoft.com/microsoftupdate/v6/default.aspx?ln=en

In the mean time, click on the request attention (just below the question) to have a moderator reopen the question.
0
 
LVL 26

Expert Comment

by:MacroShadow
Comment Utility
Although this doesn't answer your question as far as the error goes, it works on my machine and may work on yours too.

p.s.
one modification from the original code (that's erring out) is a folder picker to select the folder with your documents. It can be implemented in the first version of my code too.
Option Explicit

Sub ProcessAll(strTxtToReplace As String, strImgPath As String)

    Dim WdDoc As Document, strFile As String, strPath As String

    strPath = PickFolder '("C:\Test") In this optional parameter you can add the starting directory for the f

    strFile = Dir(strPath & "*.docx")
    'Loop through all .docx files in that path
    Do While strFile <> ""
        Set WdDoc = Application.Documents.Open(strPath & strFile)

        'Do your Search/Replace
        ReplaceTextWithImage strTxtToReplace, strImgPath

        'Save it
        WdDoc.Close wdSaveChanges
        strFile = Dir
    Loop
End Sub

Sub ReplaceTextWithImage(strTextToReplace As String, strImagePath As String)

    Dim rngPicture As Range
    Dim strPicture As String
    Dim i As Integer
    Selection.HomeKey wdStory
    Selection.Find.ClearFormatting
    With Selection.Find
        Do While .Execute(FindText:=strTextToReplace, Forward:=True, MatchWholeWord:=False, _
                          MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=False) = True
            Set rngPicture = Selection.Range
            strImagePath = Replace(strImagePath, "\", "/")
            rngPicture.Text = ""
            ActiveDocument.Fields.Add rngPicture, wdFieldEmpty, "IncludePicture " & Chr(34) & strImagePath & Chr(34)
        Loop
    End With
    With ActiveDocument
        For i = .Fields.Count To 1 Step -1
            If .Fields(i).Type = wdFieldIncludePicture Then
                .Fields(i).Unlink
            End If
        Next i
    End With
    
End Sub

Function PickFolder(Optional strStartDir As Variant) As String
    Dim SA As Object, f As Object
    Set SA = CreateObject("Shell.Application")
    If Len(strStartDir) > 2 Then
        Set f = SA.BrowseForFolder(0, "Choose a folder", 16 + 32 + 64, strStartDir)
    Else
        Set f = SA.BrowseForFolder(0, "Choose a folder", 16 + 32 + 64)
    End If
    If (Not f Is Nothing) Then
        PickFolder = f.Items.Item.Path & "\"
    End If
    Set f = Nothing
    Set SA = Nothing
End Function

Open in new window

0
 

Author Comment

by:Dov_B
Comment Utility
Alright admit it!! Macroshadow shmacroshadow your....... Eliyahu Hanavi!!
0
 
LVL 26

Expert Comment

by:MacroShadow
Comment Utility
Did it work?
0
 

Author Comment

by:Dov_B
Comment Utility
Oh yeah! Yesssssssssss! Indeed!
0
 
LVL 26

Expert Comment

by:MacroShadow
Comment Utility
I'm glad, but I still want to get to the bottom of the error you get using the first method.
0
 

Author Comment

by:Dov_B
Comment Utility
there was one teensy correction which I added on line 49 I put in a default path of my documents by the optional parameter and removes as variant
0
 

Author Comment

by:Dov_B
Comment Utility
well when I checked for updates while I did have the crtitcal ones there were custom ones that I did not have including xp and office 2010 amongst a few others my guess is that is the issue.
0
 
LVL 26

Expert Comment

by:MacroShadow
Comment Utility
Please try updating.
0
 
LVL 26

Expert Comment

by:MacroShadow
Comment Utility
The advantage of putting it in as a parameter as apposed to hard coding it like you did, is the fact that the code is portable and can be reused "out of the box" without need to adapt it.
0
 

Author Comment

by:Dov_B
Comment Utility
Unfortunately they removed the related question otions but I am posting another question based on the last code you gave me? The question is
btw what do I do if I want to keep the word the picture is replacing and put the picture either
a0after the word or
b float it on above the word.
0

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

I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

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

7 Experts available now in Live!

Get 1:1 Help Now