I am trying to make a module that imports data from fields in a word document work. I got some good advice earlier for some code to import the data from the document, but the code the program uses to get the filenames of the document is currently
So I want to change this to a file open dialog.
I am sure the answer here is simple, I just need to find out how to set strDocName to the file name once the user chooses one. However, I cannot make it work. I took the following code from a sample database, not sure if it's quite right but I am guessing the function LaunchCD returns the filename that is chosen. If that assumption is correct I think I just need to figure out what to pass to the function when I call it. What is that arguement for?
strDocName = LaunchCD() would be nice, the examples I see have LaunchCD(Me), but i have no idea what Me is, and it spits out an error.
Code:
strDocName = "C:\Contracts\" & _
InputBox("Enter the name of the Word contract " & _
"you want to import:", "Import Contract")
Set appWord = GetObject(, "Word.Application")
Set doc = appWord.Documents.Open(strDocName)
So I want to change this to a file open dialog.
I am sure the answer here is simple, I just need to find out how to set strDocName to the file name once the user chooses one. However, I cannot make it work. I took the following code from a sample database, not sure if it's quite right but I am guessing the function LaunchCD returns the filename that is chosen. If that assumption is correct I think I just need to figure out what to pass to the function when I call it. What is that arguement for?
strDocName = LaunchCD() would be nice, the examples I see have LaunchCD(Me), but i have no idea what Me is, and it spits out an error.
Code:
'Open a Common Dialog box to export file too
Private Declare Function apiGetSaveFileName Lib "comdlg32.dll" _
Alias "GetSaveFileNameA" (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
Function LaunchCD(strform As Form) As String
Dim OpenFile As OPENFILENAME
Dim lReturn As Long
Dim sFilter As String
OpenFile.lStructSize = Len(OpenFile)
OpenFile.hwndOwner = strform.Hwnd
sFilter = "Microsoft Word Document (*.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 = "Select a file for import"
OpenFile.flags = 0
lReturn = apiGetSaveFileName(OpenFile)
If lReturn = 0 Then
MsgBox "A file was not selected!", vbInformation, _
"Select a file"
Else
LaunchCD = Trim(OpenFile.lpstrFile)
End If
End Function
Last edited: