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
2,051 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 10
22 Comments
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39279406
What kind of file are you inserting?
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39279414
Try changing the name of your picture.
0
 

Author Comment

by:Dov_B
ID: 39279416
hmmm didn't think of that wait a second it is aaaaaa .jpg I will upload it now
th.jpg
0
Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

 
LVL 27

Expert Comment

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

Author Comment

by:Dov_B
ID: 39279431
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 27

Expert Comment

by:MacroShadow
ID: 39279439
hmm.
0
 

Author Comment

by:Dov_B
ID: 39279525
well I added this and it seems to have started to work
ActiveDocument.SetCompatibilityMode wdWord2003
0
 
LVL 27

Accepted Solution

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

Author Comment

by:Dov_B
ID: 39279558
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 27

Expert Comment

by:MacroShadow
ID: 39279567
So am I.
0
 

Author Comment

by:Dov_B
ID: 39279579
darn well maybe I'm not up to date on the updates
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39279600
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 27

Expert Comment

by:MacroShadow
ID: 39279703
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
ID: 39279741
Alright admit it!! Macroshadow shmacroshadow your....... Eliyahu Hanavi!!
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39279757
Did it work?
0
 

Author Comment

by:Dov_B
ID: 39279765
Oh yeah! Yesssssssssss! Indeed!
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39279771
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
ID: 39279775
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
ID: 39279783
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 27

Expert Comment

by:MacroShadow
ID: 39279789
Please try updating.
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39279795
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
ID: 39279796
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: 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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Authors who set out to write any sort of lengthy piece for online submission—be it a long question or comment on a technical form, an article, or a substantial blog entry—often find it useful to work up a draft in an editor other t…
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…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

734 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