• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2233
  • Last Modified:

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

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
Dov_B
Asked:
Dov_B
  • 12
  • 10
1 Solution
 
MacroShadowCommented:
What kind of file are you inserting?
0
 
MacroShadowCommented:
Try changing the name of your picture.
0
 
Dov_BAuthor Commented:
hmmm didn't think of that wait a second it is aaaaaa .jpg I will upload it now
th.jpg
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
MacroShadowCommented:
Put a break point on line 11, what name is the strImagePath variable holding?
0
 
Dov_BAuthor Commented:
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
 
MacroShadowCommented:
hmm.
0
 
Dov_BAuthor Commented:
well I added this and it seems to have started to work
ActiveDocument.SetCompatibilityMode wdWord2003
0
 
MacroShadowCommented:
Do you have the latest updates installed?
0
 
Dov_BAuthor Commented:
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
 
MacroShadowCommented:
So am I.
0
 
Dov_BAuthor Commented:
darn well maybe I'm not up to date on the updates
0
 
MacroShadowCommented:
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
 
MacroShadowCommented:
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
 
Dov_BAuthor Commented:
Alright admit it!! Macroshadow shmacroshadow your....... Eliyahu Hanavi!!
0
 
MacroShadowCommented:
Did it work?
0
 
Dov_BAuthor Commented:
Oh yeah! Yesssssssssss! Indeed!
0
 
MacroShadowCommented:
I'm glad, but I still want to get to the bottom of the error you get using the first method.
0
 
Dov_BAuthor Commented:
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
 
Dov_BAuthor Commented:
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
 
MacroShadowCommented:
Please try updating.
0
 
MacroShadowCommented:
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
 
Dov_BAuthor Commented:
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 12
  • 10
Tackle projects and never again get stuck behind a technical roadblock.
Join Now