Private Const OFN_READONLY = &H1
Private Const OFN_OVERWRITEPROMPT = &H2
Private Const OFN_HIDEREADONLY = &H4
Private Const OFN_NOCHANGEDIR = &H8
Private Const OFN_SHOWHELP = &H10
Private Const OFN_ENABLEHOOK = &H20
Private Const OFN_ENABLETEMPLATE = &H40
Private Const OFN_ENABLETEMPLATEHANDLE = &H80
Private Const OFN_NOVALIDATE = &H100
Private Const OFN_ALLOWMULTISELECT = &H200
Private Const OFN_EXTENSIONDIFFERENT = &H400
Private Const OFN_PATHMUSTEXIST = &H800
Private Const OFN_FILEMUSTEXIST = &H1000
Private Const OFN_CREATEPROMPT = &H2000
Private Const OFN_SHAREAWARE = &H4000
Private Const OFN_NOREADONLYRETURN = &H8000
Private Const OFN_NOTESTFILECREATE = &H10000
Private Const OFN_SHAREFALLTHROUGH = 2
Private Const OFN_SHARENOWARN = 1
Private Const OFN_SHAREWARN = 0
Private Type tagOPENFILENAME
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
Private Declare Function GetSaveFileName Lib "COMDLG32.DLL" Alias "GetSaveFileNameA" (pOpenfilename As tagOPENFILENAME) As Long
Public Function GuardarArchivo( _
Optional ByVal Hwnd As Long, _
Optional ByVal DftPath As String, _
Optional ByVal DftFile As String, _
Optional ByVal DftTitle As String, _
Optional ByVal DftExt As String, _
Optional ByVal FilterIdx As Integer) As String
On Error GoTo Err_GuardarArchivo
Dim strFilter As String
strFilter = "Todos los archivos (*.*)" & Chr(0) & "*.*" & Chr(0) _
& "Archivos de texto delimitado (*.csv)" & Chr(0) & "*.csv" & Chr(0) _
& "Archivos de intercambio de datos (*.dif)" & Chr(0) & "*.dif" & Chr(0) _
& "Archivos de texto (*.txt)" & Chr(0) & "*.txt" & Chr(0) _
& "Libros de microsoft excel (*.xls)" & Chr(0) & "*.xls" & Chr(0)
If Trim(DftExt) = "" Then DftExt = "*.*"
If Trim(DftTitle) = "" Then DftTitle = "Guardar archivo como"
If Trim(DftPath) = "" Then DftPath = App.Path
If Len(DftFile) < 255 Then DftFile = DftFile & String(255 - Len(DftFile), " ")
If FilterIdx = 0 Then FilterIdx = 1
Dim File As tagOPENFILENAME
With File
.lStructSize = Len(File)
.hwndOwner = Hwnd
.lpstrFilter = strFilter & Chr(0)
.nFilterIndex = FilterIdx
.nMaxFile = Len(DftFile) + 1
.lpstrFile = DftFile & Chr(0)
.nMaxFileTitle = Len(DftTitle) + 1
.lpstrFileTitle = DftTitle & Chr(0)
.lpstrTitle = DftTitle & Chr(0)
.lpstrInitialDir = DftPath & Chr(0)
.lpstrDefExt = DftExt & Chr(0)
.flags = OFN_FILEMUSTEXIST Or OFN_HIDEREADONLY Or OFN_OVERWRITEPROMPT
.hInstance = App.hInstance
.lpfnHook = 0
.lpTemplateName = 0
.nFileOffset = 0
.lCustData = 0
End With
Dim lngRc As Long
lngRc = GetSaveFileName(File)
If lngRc <> 0 Then
GuardarArchivo = Left(File.lpstrFile, InStr(1, File.lpstrFile, Chr(0)) - 1)
Else
GuardarArchivo = ""
End If
Exit Function
Err_GuardarArchivo:
MsgBox "Error seleccionando archivo." & String(2, vbCrLf) _
& "(" & Err.Number & ") " & Err.Description & "." _
, vbCritical, "Guardar archivo"
End Function