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,935 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 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Free Online PDF Converter 2 47
Word Template Mail merge with vb.net 4 57
Resetting Word when a document is closed 5 38
Set decimal tabstops on multiple tables using VBA 4 25
The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
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.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.

911 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

17 Experts available now in Live!

Get 1:1 Help Now