Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
2,145 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
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 2000 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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…
Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.

688 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