Pregunta

Tengo lo siguiente que abre el cuadro de diálogo Saveas, sin embargo, no está guardando el archivo cuando hago clic en Guardar.

Dim SaveBox As Object
Set SaveBox = Application.FileDialog(msoFileDialogSaveAs)

With SaveBox
.AllowMultiSelect = False
.InitialFileName = "WeeklyLog " & Format(Now, "yyyy_mm_dd")
SaveBox.Show
End With

¿Fue útil?

Solución

"... abre el cuadro de diálogo Saveas, sin embargo, en realidad no está guardando el archivo cuando hago clic en Guardar"

FileDialog puede darle una cadena que contiene una ruta de archivo.Pero en realidad no realiza una operación "Guardar como".Depende de usted, el desarrollador, para usar esa ruta de archivo en su código para guardar algo en algún lugar.

Dim SaveBox As Object
Dim strFilePath As String

Set SaveBox = Application.FileDialog(2) ' msoFileDialogSaveAs
With SaveBox
    .InitialFileName = "WeeklyLog " & Format(Date, "yyyy_mm_dd")
    If .Show = True Then
        strFilePath = .SelectedItems(1)
    End If
End With

' now do something with strFilePath ...
If Len(strFilePath) > 0 Then
    MsgBox "File path: " & strFilePath
Else
    MsgBox "Selection cancelled."
End If

Otros consejos

Esto ahorrará un archivo de Excel, creo que solo necesita un poco de ajuste de ajustes para que guarde un PDF:

Sub GetFileName()
    Dim fd As FileDialog
    Dim fname As String

    Do
        Set fd = Application.FileDialog(msoFileDialogSaveAs)
        With fd
            .AllowMultiSelect = False
            .InitialFileName = "New To Do.xls"
            If .Show = -1 Then fname = .SelectedItems(1)

            If fname = fd.InitialFileName Then _
                MsgBox "Please enter a new filename", vbOKOnly, "Filename Needed!"
        End With
    Loop Until fname <> fd.InitialFileName

    If IsEmpty(fname) Or fname = vbNullString Then Exit Sub
    ThisWorkbook.SaveAs fname

End Sub

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top