Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Visual Basic Programming Code Examples

Visual Basic > Common Dialogs Code Examples

Show the 'Save File' Common Dialog

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
Show the 'Save File' Common Dialog To show the save file common dialog use the following code: Option Explicit Private Declare Function GetSaveFileNameA Lib "comdlg32.dll" (pOpenfilename As OPENFILENAME) As Long Private Declare Function GetActiveWindow Lib "user32" () 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 SaveAsCommonDialog(Optional sTitle = "Save File", Optional sFilter As String, Optional sDefaultDir As String) As String Const clBufferLen As Long = 255 Dim OFName As OPENFILENAME, sBuffer As String * clBufferLen On Error GoTo ExitFunction OFName.lStructSize = Len(OFName) OFName.hwndOwner = GetActiveWindow 'or Me.hwnd in VB OFName.hInstance = 0 'or App.hInstance in VB If Len(sFilter) Then OFName.lpstrFilter = sFilter Else OFName.lpstrFilter = "Text Files (*.txt)" & Chr$(0) & "*.txt" & Chr$(0) & "All Files (*.*)" & Chr$(0) & "*.*" & Chr$(0) End If OFName.lpstrFile = sBuffer OFName.nMaxFile = clBufferLen 'Set max number of characters OFName.lpstrFileTitle = sBuffer OFName.nMaxFileTitle = clBufferLen 'Set max number of characters 'Set the initial directory If Len(sDefaultDir) Then OFName.lpstrInitialDir = sDefaultDir Else OFName.lpstrInitialDir = CurDir$ End If OFName.lpstrTitle = sTitle OFName.flags = 0 'Show dialog If GetSaveFileNameA(OFName) Then SaveAsCommonDialog = Left$(OFName.lpstrFile, InStr(1, OFName.lpstrFile, Chr(0)) - 1) Else SaveAsCommonDialog = "" End If ExitFunction: On Error GoTo 0 End Function 'Demostration routine Sub Test() Dim sFilePath As String sFilePath = SaveAsCommonDialog("Save workbook file", "*.xls", "C:\Program Files\") MsgBox "File to save " & sFilePath End Sub