example source code for OpenFile and Save file using comdlg32.dl

S

Stumped

I am not able to use the Common Dialog control because the way my users are set up the control is not "licensed" (even though it is we used http://support.microsoft.com/?kbid=312594 with no luck).

The BEST solution is to use the comdlg32.dll rather than the comdlg32.ocx. However even with the API books and Internet I can't find a straight forward example of how to apply the code to create the "OpenFile" and "SaveFile" methods using the "GetOpenFileName" function. The best example I found was in German. I need something I can put into a module and step through to throughly understand. The GetOpenFileNameFunction by MSDN did not help because it doesn't give a real example. Any help would be appreciated.
Thank you.
 
J

Jonathan West

Stumped said:
I am not able to use the Common Dialog control because the way my users
are set up the control is not "licensed" (even though it is we used
http://support.microsoft.com/?kbid=312594 with no luck).
The BEST solution is to use the comdlg32.dll rather than the comdlg32.ocx.
However even with the API books and Internet I can't find a straight forward
example of how to apply the code to create the "OpenFile" and "SaveFile"
methods using the "GetOpenFileName" function. The best example I found was
in German. I need something I can put into a module and step through to
throughly understand. The GetOpenFileNameFunction by MSDN did not help
because it doesn't give a real example. Any help would be appreciated.

Is there any reason why you wouldn't want to use Word's own built-in
dialogs?

For instance, the following code returns the full pathname of the selected
file or a blank string oif the user clicks cancel.

Function GetOpenFileName() As String
With Dialogs(wdDialogFileOpen)
If .Display = -1 Then
GetOpenFileName = WordBasic.FileNameInfo$(.Name, 1)
End If
End With
End Function
 
S

Stumped

Jonathan,
I need a dialog box with a bit more beef. I want to be able to tell it which directory to open in and what type of file it is to look for. The dialog box for Word only features 5 built in functions. None that I can use. I also need it to return only the file name (not the path with the file name). It needs the user to be able to open a excle file and then save a file in either the xls or snf file format. My books are not showing me this capability. Please prove them wrong.

Thank you
 
J

Jonathan West

Stumped said:
Jonathan,
I need a dialog box with a bit more beef. I want to be able to tell it
which directory to open in and what type of file it is to look for. The
dialog box for Word only features 5 built in functions. None that I can use.
I also need it to return only the file name (not the path with the file
name). It needs the user to be able to open a excle file and then save a
file in either the xls or snf file format. My books are not showing me this
capability. Please prove them wrong.
Not too hard.

Yes, it can return the name only (in fact that is less code than for thr
full pathname!)
Preset the initial folder and the filetype, yes that can also be done.

Try this

Function GetOpenFileName(Folder as String, FileType as String) As String
With Dialogs(wdDialogFileOpen)
.Name = Folder
.Name = FileType
If .Display = -1 Then
GetOpenFileName = .Name
End If
End With
End Function

As for saving the files in different formats, that is the business of your
code, not of the dialog. Once you have the filename from the dialog, you can
do whatever you want with it in your VBA code. All the common dialog would
do is pass you the file name.
 
J

Jezebel

As for saving the files in different formats, that is the business of your
code, not of the dialog. Once you have the filename from the dialog, you can
do whatever you want with it in your VBA code. All the common dialog would
do is pass you the file name.

None the less, Jon, the CommonDialog does have a heap more grunt for this
kind of application -- specifying possible and default extension filters,
guaranteeing that the file exists and is or is not read-only, confirming
that the user wants to overwrite an existing file, etc.
 
J

Jay Freedman

Stumped said:
I am not able to use the Common Dialog control because the way my users are set up the control is not "licensed" (even though it is we used http://support.microsoft.com/?kbid=312594 with no luck).

The BEST solution is to use the comdlg32.dll rather than the comdlg32.ocx. However even with the API books and Internet I can't find a straight forward example of how to apply the code to create the "OpenFile" and "SaveFile" methods using the "GetOpenFileName" function. The best example I found was in German. I need something I can put into a module and step through to throughly understand. The GetOpenFileNameFunction by MSDN did not help because it doesn't give a real example. Any help would be appreciated.
Thank you.

The following code is slightly modified from
http://support.microsoft.com/?id=161286 to run in a Word userform, and
I verified that it works in Word 2003/Windows XP.

Private Declare Function GetOpenFileName Lib "comdlg32.dll" _
Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) _
As Long

Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type

Private Sub CommandButton1_Click()
Dim OpenFile As OPENFILENAME
Dim lReturn As Long
Dim sFilter As String
OpenFile.lStructSize = Len(OpenFile)
OpenFile.hwndOwner = 0
OpenFile.hInstance = 0
sFilter = "Word document Files (*.doc)" & _
Chr(0) & "*.DOC" & Chr(0)
OpenFile.lpstrFilter = sFilter
OpenFile.nFilterIndex = 1
OpenFile.lpstrFile = String(257, 0)
OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1
OpenFile.lpstrFileTitle = OpenFile.lpstrFile
OpenFile.nMaxFileTitle = OpenFile.nMaxFile
OpenFile.lpstrInitialDir = "C:\"
OpenFile.lpstrTitle = "Use the Comdlg API not the OCX"
OpenFile.flags = 0
lReturn = GetOpenFileName(OpenFile)
If lReturn = 0 Then
MsgBox "The user pressed the Cancel button"
Else
MsgBox "The user chose " & Trim(OpenFile.lpstrFile)
End If
End Sub

Sorry, I don't have anything handy for SaveFile, but it's not that
different.
 

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