Link to home
Start Free TrialLog in
Avatar of Fredd
Fredd

asked on

Word VBA 438 when inserting picture

I'm getting a Run-time error 438 - object doesn't support this property or method.
this only happens like every third or fourth time I run the code
Prior to executing, I'm sure all word instances are shutdown
Environment:  Access 2016 - running VBA to manipulate MS Word 2016 document.
All the references are good I believe (included Word object module, etc)

see attached word file and simple database - see module 1 for sample
it generates a 438 on the line
owordapp.activedocument.content.selection.Goto what:=wdgotobookmark, Name:="photo"
Option Explicit
Sub photoproblem()

Dim owordapp As Object, i As Integer, a As String, v As Shape, rng As range, dir1 As String
Dim template As String, newphoto As String

dir1 = "U:\Data\photoproblem\"
template = dir1 & "SAMPlE FILE.docx"
newphoto = dir1 & "dolphin.png"


Set owordapp = CreateObject("Word.Application")

With owordapp

    .Documents.Open template
    .Visible = True
    .screenupdating = True
    
End With

owordapp.activedocument.content.selection.Goto what:=wdgotobookmark, Name:="photo"

Set rng = selection.range

Set v = activedocument.inlineshapes.addpicture(FileName:=newphoto, range:=rng, savewithdocument:=True).converttoshape

Set rng = Nothing
Set owordapp = Nothing

End Sub

Open in new window

Sample-file.docx
photoproblem1.accdb
dolphin.png
ASKER CERTIFIED SOLUTION
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You could try adding line 2.
owordapp.activedocument.content.selection.goto what:=wdgotobookmark name:="photo" 
DoEvents

Open in new window

Avatar of Fredd
Fredd

ASKER

thanks - this is not access but rather word  - I'm inserting into word.  I thought doevents as an access thing.  Also  - it fails onthe goto line - so will never get to the doevents line.
other ideas - you might download the database and try something out perhaps?

also - the comma was a copy/paste error someohow - the code has the properly formatting command. i corrected the question - see dbase for actual code - this code fails right now now everytime so it was working occasionally but not at all.
Avatar of Fredd

ASKER

any help on this?
Sorry, but I don't do Access so I can't come up with anything else, but I added Access to your topics in hope that it will attract someone who can.