FileDialog gives compile error

A

Alfred

Hi all,
I want to use the FileDialog property (VB6, Word 2003, Windows XP) in
the following macro (with some more code, added on later):

Sub ShowFileDialog()
Dim dlgOpen As FileDialog
Set dlgOpen = Application.FileDialog( _
FileDialogType:=msoFileDialogOpen)
With dlgOpen
.AllowMultiSelect = True
.Show
End With
End Sub

Running this macro in VB6/Word 2003, causes a compile error
on .FileDialog: 'Method or Data Member not found.'

In VB Tools/References both Microsoft Word 11.0 Object Library and
Microsoft Office 11.0 Object Library are checked.

What am I doing wrong?
 
J

Jay Freedman

Let me get something sorted out. Are you using VB6 to run an external
program that automates Word, or are you writing a macro in VBA inside Word?
It makes a difference in the syntax.

If it's a VBA macro inside Word, then the Application keyword is
automatically defined to refer to the host application (Word). If it's VB6
outside Word, then you have to declare and initialize an object to point to
the Word application (using a call to either CreateObject or GetObject), and
use that object instead of Application.

The code you showed (which is straight out of the VBA help topic on
FileDialog) works for me as a macro in either Word 2003 or Word 2007.
Unfortunately, it does have one problem: The dialog appears, but nothing
happens when you select files and click OK. Microsoft neglected to include
the necessary call to the .Execute method after the .Show method.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
A

Alfred

Thanks Jay!
I want to use this VB macro as a module inside Word 2003, but I still
don’t manage to make it work, without getting a compile error. You are
right that this macro is straight from the help topic. It does,
however, exactly what I want: display a File Open dialog that allows
multiple file selection, which in the built-in Word FileOpen dialog is
impossible. When I get the FileDialog to work, I want to add a few
lines of code.
Alfred
 
J

Jay Freedman

Thanks Jay!
I want to use this VB macro as a module inside Word 2003, but I still
don’t manage to make it work, without getting a compile error. You are
right that this macro is straight from the help topic. It does,
however, exactly what I want: display a File Open dialog that allows
multiple file selection, which in the built-in Word FileOpen dialog is
impossible. When I get the FileDialog to work, I want to add a few
lines of code.
Alfred

I don't know what's wrong. I just tried it again on another PC, and it works
fine.

I think you're right to suspect the references. The ones that are checked here
are

Visual Basic for Applications
Microsoft Word 11.0 Object Library
OLE Automation
Normal
Microsoft Office 11.0 Object Library

When I run the macro in Word 2007, the list is the same except that the Word and
Office object libraries are version 12.0. The macro works the same in both
versions -- no errors.

You might check with Microsoft Update to make sure you have the latest files.
Office 2003 is up to SP3, while Office 2007 is currently at SP1.
 
A

Alfred

I run Office SP3. I did check all the references you mentioned. But
after some more research I think it’s definitely me who makes a
mistake: in ‘all libraries’ the members of the msoFile Dialog show up,
so they are there.
I installed the macro in Normal/New Macros. Doing that, ‘normal’ does
not show up in the list of references. Maybe there goes something
wrong? Should I install the macro in another way?
Thanks for your time!
Alfred
 
A

Alfred

Instead of in ‘Normal – NewMacros’, I put the FileDialog macro (plus
an extra .execute) in Normal – NewDocument. I added my lines of code
and it worked! One problem remains, however: when I assign a shortkey
to this macro within Word, it does open the FileDialog, but it does
not run my added code. Starting the macro within Word in Tools/Macro/
Macros/Run works perfect! Trying other shortcuts makes no difference.
Anyway, I am almost there, thanks to your help!
 
A

Alfred

Hi Jay,
My FileDialog works fine now, including the shortcut.
Thanks for your help and time!
Alfred
 
J

Jay Freedman

Alfred said:
Hi Jay,
My FileDialog works fine now, including the shortcut.
Thanks for your help and time!
Alfred

You're welcome. Glad to help!

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 

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