Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-02-2017, 03:40 PM
jeddell jeddell is offline Opening a doc template - an Add-in Error Windows 10 Opening a doc template - an Add-in Error Office 2016
Novice
Opening a doc template - an Add-in Error
 
Join Date: Oct 2017
Posts: 3
jeddell is on a distinguished road
Default Opening a doc template - an Add-in Error

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
Here's the getopenfilename code that seems to be returning nothing. This is one of the pieces that had to be declared ptrSafe as you can see
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
if you need any other parts of this code to further analyse, I'm happy to provide
Thanks in advance.
Reply With Quote
  #2  
Old 10-02-2017, 09:55 PM
slaycock slaycock is offline Opening a doc template - an Add-in Error Windows 7 64bit Opening a doc template - an Add-in Error Office 2013
Expert
 
Join Date: Sep 2013
Posts: 256
slaycock is on a distinguished road
Default

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
The code above return a path enabled filename

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.
Reply With Quote
  #3  
Old 10-03-2017, 06:10 PM
jeddell jeddell is offline Opening a doc template - an Add-in Error Windows 10 Opening a doc template - an Add-in Error Office 2016
Novice
Opening a doc template - an Add-in Error
 
Join Date: Oct 2017
Posts: 3
jeddell is on a distinguished road
Default

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
I must admit this is a little beyond me and I like your code better. I am getting an error 5 though. Ran the debug and it stopped at

Code:
fd.Filters.Add "Files", strFilter, 1
I'm guessing this could just be a typo. I did try to add the file extension there instead of the '1' but it did not help. Id be happy to continue working with your code if that's OK would certainly help and Lessen my learning curve. It certainly looks a lot simpler.
Reply With Quote
  #4  
Old 10-04-2017, 01:32 AM
slaycock slaycock is offline Opening a doc template - an Add-in Error Windows 7 64bit Opening a doc template - an Add-in Error Office 2013
Expert
 
Join Date: Sep 2013
Posts: 256
slaycock is on a distinguished road
Default

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()
as GetActiveWindow is another user defined function.

I tried to get around this as follows

Code:
OFN.hwndOwner = ActiveDocument.ActiveWindow.Hwnd ' GetActiveWindow()
and indeed the code runs. However using the inputs that work with my function do not produce anything i.e.

Code:
lReturn = GetOpenFileName(OFN)
just returns 0.

Your strategy of changing the 1 in

Code:
fd.Filters.Add "Files", strFilter, 1
was incorrect. If you want to understand why in your code place the cursor on the add and press F1. This should bring up the relevant help page (works for all VBA keywords and objects)

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
Reply With Quote
Reply



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

Other Forums: Access Forums

All times are GMT -7. The time now is 04:17 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft