Get SaveAs File Name and Path

Started by AlexHP, December 30, 2015, 03:05:48 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

AlexHP

Yes, I already know the answer to this one...  :-\

Visio does not have the GetSaveAsFileName functionality that exists in other VBA applications (i.e. Excel, Word, etc.). But, is there a way to achieve the same thing? I have been banging my head against the wall trying to find something, all to no avail.  ???

PLEASE HELP!!!! Thank you.

Alex...

Nikolay


AlexHP

No, you're not my answer, Nikolay! You can't be!!

Actually, I DID use "the search" and found this but I could not get it to work in order to test it for my use. I know you posted a "fix" (which I used) but I still could not get it to work. I used the sub-routine that Visio Guy posted to call your fixed code and I get the error "Run-time error '424': Object required". When I click on the "Debug" option it highlights the "Call OpenExcelFile.FindExcelFile(pathExcel, bCancelled)" line of code. And I cannot figure out what's wrong.

Also, I tried using aledlund's posted sub but all that does is call the "Open File" dialog, which then opens a file if you click on one rather than returning the path and file name you select.

So, this is not the reply you are looking for... Move along... and see if you can still help me out!  ;)

Alex...

Nikolay

#3
Hi Alex,

My fix was supposed to fix the code for 64-bit Visio. Note that it's uncommon - if you don't use 64-bit visio (64-bit VISIO, NOT 64-bit windows) then you don't need it.
The original code from visguy seems to work just fine with normal 32-bit Visio.

If you see the error at the line OpenExcelFile.FindExcelFile, that means that you need to add the code from that post (both pieces are required, not just the second one)

AlexHP

Nikolay,

I guess it would help if I paid attention to the Visio version.  :o

I do have the 32-bit version so I went back and copied BOTH of the code sections into a module and tried to run them. I got the same error. Still no joy.  :'(

What now...?

Alex...

AlexHP

I know what the problem is...

I have the code placed in a module called "Module5" but it needs to be called "OpenExcelFile" since the code says "Call OpenExcelFile.FindExcelFile(pathExcel, bCancelled)". If it were just "Call FindExcelFile(pathExcel, bCancelled)" then it would have worked the first time. Thanks.

Alex...

AlexHP

Nicolay,

OK, so the function works but it is designed for opening Excel files, not saving Visio files as .vsd or PDF. I have modified the code to operate as a Save File As dialog.

'// Module: SaveAsFile
'//
'// This is code that uses the Windows API to invoke
'// the Save File common dialog.

Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias _
    "GetSaveFileNameA" (pSavefilename As SAVEFILENAME) As Long

Private Type SAVEFILENAME
    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

Public Sub GetSaveAsFile(ByRef filePath As String, _
                            ByRef cancelled As Boolean)

    Dim SaveFile As SAVEFILENAME
    Dim lReturn As Long
    Dim sFilter As String
   
    On Error GoTo errTrap
   
    SaveFile.lStructSize = Len(SaveFile)
   
    '// Sample filter:
    '// "Text Files (*.txt)" & Chr$(0) & "*.sky" & Chr$(0) & "All Files (*.*)" & Chr$(0) & "*.*" & Chr$(0) & "Excel Files (*.xl*)" & Chr(0) & "*.xl*"
    sFilter = "Drawing (*.vsd)" & Chr$(0) & "*.vsd"
   
    SaveFile.lpstrFilter = sFilter
    SaveFile.nFilterIndex = 1
    SaveFile.lpstrFile = String(257, 0)
    SaveFile.nMaxFile = Len(SaveFile.lpstrFile) - 1
    SaveFile.lpstrFileTitle = SaveFile.lpstrFile
    SaveFile.nMaxFileTitle = SaveFile.nMaxFile
    SaveFile.lpstrInitialDir = ThisDocument.path
   
    SaveFile.lpstrTitle = "Save File As..."
    SaveFile.flags = 0
    lReturn = GetSaveFileName(SaveFile)
   
    If lReturn = 0 Then
        cancelled = True
        filePath = vbNullString
    Else
        cancelled = False
        filePath = Trim(SaveFile.lpstrFile)
        filePath = Replace(filePath, Chr(0), vbNullString)
    End If
   
    Exit Sub
   
errTrap:
    Exit Sub
    Resume

End Sub


Private Function m_getSaveAsFile() As String
   
    Dim pathFile As String
    Dim bCancelled As Boolean
   
    Call SaveAsFile.GetSaveAsFile(pathFile, bCancelled)
   
    If bCancelled Then
        m_getSaveAsFile = vbNullString
    Else
        m_getSaveAsFile = pathFile
    End If
   
End Function


One thing I am missing is getting the file path/name to include the extension (i.e. ".vxd") when it returns from the dialog (I did not type it in to the dialog, just the name). Shouldn't it automatically append it or do I have to do that manually. Can you help? Thanks.

Alex...

Nikolay

Try setting default file extension

    SaveFile.lpstrFileTitle = SaveFile.lpstrFile
    SaveFile.nMaxFileTitle = SaveFile.nMaxFile
    SaveFile.lpstrInitialDir = ThisDocument.Path
    .....
    SaveFile.lpstrDefExt = ".vsd"   ' <---------
    .....

AlexHP

That did it! Thanks, Nikolay. Happy New Year.

Alex...