![]() |
|
#1
|
|||
|
|||
|
Hey everyone,
I have an add-in I use for my wok that has a suite of document templates and vba code to automate the specific layout and formating of the documents. Its used for writing technical data and making sure that they all look the same. The add-in has been around for many years in our organisation and has been developed since Office 97 through to XP and in MS Word since Office 97. Im now using this add-in in Office 2016. On the whole it works fine, but im having a problem opening a document already created. I keep getting a custom error message. I found the code where the message is being generated, but Im not that good at troubleshooting this code. Can anyone take a look at this code and tell me if there is anything here that would stop it from working in MS Word 2016? Basically the error states that I havent selected a file, but the code does not give me a chance to select a file. Code:
Public Function SelectFileOpenDialog(strInitDir As String, strTitle As String, strFilter As String) As String
Dim strTemp As String
Dim strTemp1 As String
Dim pathStr As String
Dim i As Long
Dim n As Long
Dim j As Long
Dim OFN As OPENFILENAME
Dim lReturn As Long
Dim strFilterString As String
Dim strFilename As String
OFN.lStructSize = Len(OFN)
strFilterString = Replace(strFilter, "|", Chr(0)) & Chr(0)
OFN.lpstrFilter = strFilterString
OFN.nFilterIndex = 1
OFN.lpstrFile = String(257, 0)
OFN.nMaxFile = Len(OFN.lpstrFile) - 1
OFN.lpstrFileTitle = OFN.lpstrFile
OFN.nMaxFileTitle = OFN.nMaxFile
OFN.lpstrInitialDir = strInitDir
OFN.lpstrTitle = strTitle
OFN.hwndOwner = GetActiveWindow()
OFN.flags = 0
lReturn = GetOpenFileName(OFN)
If lReturn = 0 Then
MsgBox "You didn't select any file"
strFilename = ""
Else
strFilename = Trim$(OFN.lpstrFile) ' copy the filename
End If
SelectFileOpenDialog = strFilename
End Function
Code:
Public Declare PtrSafe Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Public gstrTypeSelected As String
Public gstrJITypeSelected As String
Private Type typToolbarNamePos
strName As String
lngPosition As Long
End Type
'array to store the user's toolbar settings
Public garPrevToobars() As typToolbarNamePos
'array to store the user's style settings
Public garPrevStyles() As String
Public Type typStyleName
strStyleNameGeneric As String
strStyleName97 As String
strStyleName2003 As String
bPromotable As Boolean
bDemotable As Boolean
End Type
Public garStyleNames() As typStyleName
Thanks in advance. |
|
#2
|
|||
|
|||
|
We need the definition of the OPENFILENAME structure/object to be able to run the SelectFileOpenDialog function.
In the meantime the function appears to just return a filename. The OPENFILENAME requirement may be a red herring because whilst it is used in the function it is a local variable and so the only product it returns is the field/property OFN.lpstrFile. Word has moved on since your macro was writen and consequently it is possible to use a much simpler version to return a filename for subsequent use. Code:
Public Function SelectFileOpenDialog(strInitDir As String, strTitle As String, strFilter As String) As String
Dim fd As FileDialog
Dim lReturn As Long
Dim strFilename As String
Set fd = Application.FileDialog(msoFileDialogFilePicker)
fd.AllowMultiSelect = False
' str filter should contain a comma seperated list of filters of the form
' *.<application extention> e.g. *.docx or *.xlsx etc.
fd.Filters.Add "Files", strFilter, 1
fd.Title = strTitle
fd.InitialFileName = strInitDir
fd.InitialView = msoFileDialogViewDetails
lReturn = fd.Show
If lReturn = 0 Then
MsgBox "You didn't select any file"
strFilename = ""
Else
strFilename = Trim$(fd.SelectedItems(1)) ' copy the filename - The 1 might need to be a zero
End If
SelectFileOpenDialog = strFilename
End Function
e.g. C:\...........\<filename you selected> and so may need adjusting depending on how the returned filename is used. Try renaming your code to Public Function SelectFileOpenDialog1(strInitDir As String, strTitle As String, strFilter As String) As String paste in the code above and see what happens when you try to select a file. |
|
#3
|
|||
|
|||
|
Thanks for your reply.
Here is the code for the OPENFILENAME. Code:
Public 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
Code:
fd.Filters.Add "Files", strFilter, 1 |
|
#4
|
|||
|
|||
|
My code worked fine on my laptop using
SelectFileOpenDialog("Y:\Templates", "Hello World", "*.dotm") Based on the structures you are using your app would seem to be Autocad(?) (There is lots of similar stuff if you google "GetOpenFileName Lib "comdlg32.dll"" I don't have Autocad so my examples are using MS Word as the VBA host. When attempting to run your code it now gets stuck at Code:
OFN.hwndOwner = GetActiveWindow() I tried to get around this as follows Code:
OFN.hwndOwner = ActiveDocument.ActiveWindow.Hwnd ' GetActiveWindow() Code:
lReturn = GetOpenFileName(OFN) Your strategy of changing the 1 in Code:
fd.Filters.Add "Files", strFilter, 1 If the above doesn't work because you are in VBA for AutoCad then try looking at http://analystcave.com/vba-applicati...g-select-file/ as a random example of a google search. It may be that I can't resolve this problem to your using Autocad. The code below does not select a filename using your code (with my OFN.hwndOwner fudge, but my code works fine on my pc. I've also added a few more file filters so you can see how they work *IF* the file dialog works correctly. The code below is stand-alone when run in word sp please try it in an Office application first and then in Autocad. In autocad you will need to remove my OFN.hwndOwner fudge. Code:
Public Declare PtrSafe Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Public 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
Sub test()
Dim myString As String
myString = SelectFileOpenDialog("Y:\Templates", "Hello World", "*.dotm")
Debug.Print myString
myString = SelectFileOpenDialog2("Y:\Templates", "Hello World", "*.dotm")
Debug.Print myString
End Sub
Public Function SelectFileOpenDialog(strInitDir As String, strTitle As String, strFilter As String) As String
Dim strTemp As String
Dim strTemp1 As String
Dim pathStr As String
Dim i As Long
Dim n As Long
Dim j As Long
Dim OFN As OPENFILENAME
Dim lReturn As Long
Dim strFilterString As String
Dim strFilename As String
OFN.lStructSize = Len(OFN)
strFilterString = Replace(strFilter, "|", Chr(0)) & Chr(0)
OFN.lpstrFilter = strFilterString
OFN.nFilterIndex = 1
OFN.lpstrFile = String(257, 0)
OFN.nMaxFile = Len(OFN.lpstrFile) - 1
OFN.lpstrFileTitle = OFN.lpstrFile
OFN.nMaxFileTitle = OFN.nMaxFile
OFN.lpstrInitialDir = strInitDir
OFN.lpstrTitle = strTitle
OFN.hwndOwner = ActiveDocument.ActiveWindow.Hwnd ' GetActiveWindow()
OFN.flags = 0
lReturn = GetOpenFileName(OFN)
If lReturn = 0 Then
MsgBox "You didn't select any file"
strFilename = ""
Else
strFilename = Trim$(OFN.lpstrFile) ' copy the filename
End If
SelectFileOpenDialog = strFilename
End Function
Public Function SelectFileOpenDialog2(strInitDir As String, strTitle As String, strFilter As String) As String
Dim fd As FileDialog
Dim lReturn As Long
Dim strFilename As String
Set fd = Application.FileDialog(msoFileDialogFilePicker)
fd.AllowMultiSelect = False
' str filter should contain a comma seperated list of filters of the form
' *.<application extention> e.g. *.docx or *.xlsx etc.
fd.Filters.Clear
fd.Filters.Add "Word Files", "*.doc;*.dot;*.docx;*.dotx;*.dotm"
fd.Filters.Add "Text/CSV files", "*.txt;*.csv"
fd.Filters.Add "Excel files", "*.xlsx;*.xls;*.xlsm"
fd.Filters.Add "All files", "*.*"
fd.Filters.Add "Your file type description", strFilter
fd.Title = strTitle
fd.InitialFileName = strInitDir
fd.InitialView = msoFileDialogViewDetails
lReturn = fd.Show
If lReturn = 0 Then
MsgBox "You didn't select any file"
strFilename = ""
Else
strFilename = Trim$(fd.SelectedItems(1)) ' copy the filename - The 1 might need to be a zero
End If
SelectFileOpenDialog2 = CStr(strFilename)
End Function
|
|
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Error message when opening any template | spiff1116 | Word | 6 | 11-04-2013 04:44 PM |
| Template "File In Use" when opening 2 documents based on the same template | wendt | Word | 5 | 12-15-2009 12:37 AM |
| Error opening word from a doc. | Davem501 | Word | 0 | 12-01-2009 02:10 AM |
| Error when opening .vcf files | thorgal67 | Outlook | 0 | 04-06-2009 04:54 AM |
| Outlook XP *.msg opening error msg | slmka | Outlook | 0 | 06-27-2007 09:00 PM |