Welcome to Dream.In.Code
Getting VB Help is Easy!

Join 118,882 VB Programmers for FREE! Ask your question and get quick answers from experts. There are 1,982 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Problem with msoFileDialogOpen in VBA

 
Reply to this topicStart new topic

Problem with msoFileDialogOpen in VBA, .

Paro
post 2 Jul, 2008 - 07:04 AM
Post #1


New D.I.C Head

*
Joined: 24 Jun, 2008
Posts: 7

Hey, I'm working on an Addon for Word with some VBA code.

I'm having a problem with an OpenfileDialog.

CODE

Private Sub CommandButton2_Click()
With Application.FileDialog(msoFileDialogOpen)
.AllowMultiSelect = False
.Filters.Clear
.Filters.Add ("XML Files"), ("*.xml")
.Show
path = .SelectedItems(1)                    <<<THIS IS THE PROBLEM
End With


What I need to do is check on the dialog if anything is selected, because if I dont select anything and just hit the cancel button it maked an exception.
I've tried with an if, like this, but that doesn't seem to work either:

CODE

If Not .SelectedItems(1) = Null Then
path = .SelectedItems(1)
End If


But appearantly it already tries to load it in the if, and already throws the exception there.
User is offlineProfile CardPM

Go to the top of the page


AdamSpeight2008
post 2 Jul, 2008 - 07:29 AM
Post #2


DICula

Group Icon
Joined: 29 May, 2008
Posts: 616



Thanked 38 times

Dream Kudos: 2000
My Contributions


Try this "fudgey" code
CODE

Private Sub CommandButton2_Click()

With Application.FileDialog(msoFileDialogOpen)
.AllowMultiSelect = False
.Filters.Clear
.Filters.Add ("XML Files"), ("*.xml")
.tag=false '<< add set this to false
.Show
if <name for FileDialog>.tag=True then
path = .SelectedItems(1)                <<<Shouldn't be a problem now (Fingers Crossed)
End With

Hopeful this event is available. Add the follow sub to handle it
CODE

private sub HandleFileOpen() handles <name for FileDialog>.FileOK

<name for FileDialog>.tag=true
end sub


You could also but the could code to do the things which you going to do the file/s inside this sub.

This post has been edited by AdamSpeight2008: 2 Jul, 2008 - 07:32 AM
User is offlineProfile CardPM

Go to the top of the page

AdamSpeight2008
post 2 Jul, 2008 - 07:47 AM
Post #3


DICula

Group Icon
Joined: 29 May, 2008
Posts: 616



Thanked 38 times

Dream Kudos: 2000
My Contributions



Forget the above code it wont work
This will though
CODE

Private Sub CommandButton2_Click()
Dim reply as integer
reply=0
with application.FileDialog(msoFileDialogOpen)
.AllowMultiSelect = False
.Filters.Clear
.Filters.Add ("XML Files"), ("*.xml")
reply=.show
if reply=True then
path = .SelectedItems(1)                <<<Shouldn't be a problem now (Fingers Crossed)
endif
end with



reply = 0 when cancelled
reply = -1 open/save
User is offlineProfile CardPM

Go to the top of the page

Paro
post 3 Jul, 2008 - 04:17 AM
Post #4


New D.I.C Head

*
Joined: 24 Jun, 2008
Posts: 7

thanks a lot, ill just try that smile.gif
User is offlineProfile CardPM

Go to the top of the page

Paro
post 3 Jul, 2008 - 04:25 AM
Post #5


New D.I.C Head

*
Joined: 24 Jun, 2008
Posts: 7

It works just fine, thank you very much smile.gif
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/13/08 02:49AM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month