Macro code produces error

G

gwh

Version: 2004 Operating System: Mac OS X 10.5 (Leopard) Processor: Intel Hi everyone,

When I test the macro code below on Word 2004, I get the following error:

Compile error: user-defined type not defined

This only happens when I run the code on my mac. When I test it on a PC with Word 2007, it works fine.

I wondered if someone could run it for me and tell me if you get the same error?

Appreciate any assistance.

Sub GetImage()

Dim dlg As Office.FileDialog
    Dim strFilePath As String

Set dlg = Application.FileDialog(msoFileDialogFilePicker)

With dlg
    .AllowMultiSelect = False 'Make single selection only
    If .Show() 0 Then
    strFilePath = .SelectedItems(1)
    End If
    End With

Selection.InlineShapes.AddPicture FileName:= _
    strFilePath, LinkToFile:=False, _
    SaveWithDocument:=True

End Sub
 
P

Peter Jamieson

I have the same problem here, and it's because the Offce.FileDialog
object is simply not available on Mac VBA. "Word 2004" sounds as if it
ought to be similar to "Word 2003," which has that object, but in fact
it's more like Word 2000 as far as VBA is concerned.

So I think you'll need to change your code to work with

Dim dlg As Word.Dialog
Set dlg = Application.Dialogs(wdDialogInsertPicture)

e.g. something like

Sub GetImage()

Dim dlg As Word.Dialog

Set dlg = Application.Dialogs(wdDialogInsertPicture)
With dlg
.linktofile = False
' .SaveWithDocument not available on Mac
' .SaveWithDocument = True
If .Display() <> 0 Then
Selection.InlineShapes.AddPicture FileName:= _
.Name, linktofile:=False, _
SaveWithDocument:=True
End If
End With

End Sub


Peter Jamieson

http://tips.pjmsn.me.uk
 
G

gwh

Thanks for the reply,

I tried your code but received the following error on testing:

Run-time error '4605'
This command is not available.

Do you know what this is about?
 
R

Rob Schneider

It probably means that the command on the line of code where the macro
stopped is not available. :) Check out that specific line of code to
see what might be wrong.



--rms

www.rmschneider.com
 
P

Peter Jamieson

It can help if you can click the "Debug" button (if you see one) in the
error dialog box and see which line caused the error. Or you can use
View->Toolbars to enable the Debug toolbar and step through the code
line-by-line to identify the problem code.

Peter Jamieson

http://tips.pjmsn.me.uk
 
G

gwh

When I press the debug button, the following line is highlighted:

Set dlg = Application.Dialogs(wdDialogInsertPicture)

I'm not sure what's wrong with it.
 
G

gwh

Yes my document is protected for forms. Is there a workaround as the code needs to work with the locked form.
 
P

Peter Jamieson

As far as I know the only options are to unprotect the form, do the
insert, then re-protect

(surround your macro with

ActiveDocument.Unprotect Password:="YourPassword"

and

ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, _
NoReset:=True, _
Password:="YourPassword"

which has the disadvantage of embedding the password in the VBA)

or if your layout allows it, create a separate Word section and leave it
unprotected, which has the disadvantage that there is no restriction on
what the user can insert.

Peter Jamieson

http://tips.pjmsn.me.uk
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top