VBA Excel: usando Saveas Dialog para archivos de texto / Tipos de archivos personalizados

StackOverflow https://stackoverflow.com//questions/25083325

  •  02-01-2020
  •  | 
  •  

Pregunta

Tengo una macro de Excel que escribe una cadena específica a un archivo de texto.La captura es, necesito guardarlo como un archivo personalizado (".us1").Estoy adjuntando mi código actual a continuación.Terminé haciendo una reversión extraña del diálogo abierto.¿Qué haría para cambiar este código para usar el diálogo SaveAs?

Private Sub CommandButton2_Click()

Dim fso As New FileSystemObject
Dim stream As TextStream
Dim FilePath As String
Dim ofD As Object
CommandButton2.Height = 53.25
CommandButton2.Width = 83.25
CommandButton2.Left = 222.75
CommandButton2.Top = 508.5

If OptionButton5.Value = True Then

    MsgBox "NOTE! This is a bi-phasic script. Every other bit, starting with the first, is a sign bit!!! DISREGARD the graph!!!"

    Set ofD = Application.FileDialog(3)
    ofD.AllowMultiSelect = False

    If ofD.Show = False Then
        MsgBox "Script Generation Canceled"
    Else
        FilePath = ofD.SelectedItems(1)
        Set stream = fso.OpenTextFile(FilePath, ForWriting, True)

        stream.WriteLine "F3 07 00 31 FA 12 // "
        stream.WriteLine "F3 07 00 31 FB " + Cells(27, 2) + " // "
        stream.WriteLine "F3 07 00 31 FC 00 // "
        stream.WriteLine "F3 25 00 31 FF " + Cells(33, 2) + "// "
        stream.WriteLine "F3 07 00 31 FA 13 // "

        stream.Close
    End If
Else
    Set ofD = Application.FileDialog(3)

    ofD.AllowMultiSelect = False

    If ofD.Show = False Then
        MsgBox "Script Generation Canceled"
    Else
        FilePath = ofD.SelectedItems(1)
        Set stream = fso.OpenTextFile(FilePath, ForWriting, True)

        stream.WriteLine "F3 07 00 31 FA 10 // "
        stream.WriteLine "F3 07 00 31 FB " + Cells(27, 2) + " // "
        stream.WriteLine "F3 07 00 31 FC 00 // "
        stream.WriteLine "F3 25 00 31 FF " + Cells(33, 2) + "// "
        stream.WriteLine "F3 07 00 31 FA 11 // "

        stream.Close
    End If
End If
End Sub

¿Fue útil?

Solución

Pude resolver mi problema seleccionando la ruta del archivo a través del diálogo Saveas.Sorprendentemente no tuve problemas con el filtro de archivos.

Private Sub CommandButton2_Click()

Dim fso As New FileSystemObject
Dim stream As TextStream
Dim FilePath As String
Dim saveDialog As Variant
CommandButton2.Height = 53.25
CommandButton2.Width = 83.25
CommandButton2.Left = 222.75
CommandButton2.Top = 508.5

If OptionButton5.Value = True Then

    MsgBox "NOTE! This is a bi-phasic script. Every other bit, starting with the first, is a sign bit!!! DISREGARD the graph!!!"

    saveDialog = Application.GetSaveAsFilename(FileFilter:="Script Files(*.us1), *.us1")

        Set stream = fso.OpenTextFile(saveDialog, ForWriting, True)

        stream.WriteLine "F3 07 00 31 FA 12 // "
        stream.WriteLine "F3 07 00 31 FB " + Cells(27, 2) + " // "
        stream.WriteLine "F3 07 00 31 FC 00 // "
        stream.WriteLine "F3 25 00 31 FF " + Cells(33, 2) + "// "
        stream.WriteLine "F3 07 00 31 FA 13 // "

        stream.Close

Else
    saveDialog = Application.GetSaveAsFilename(FileFilter:="Script Files(*.us1),*.us1")

        Set stream = fso.OpenTextFile(saveDialog, ForWriting, True)

        stream.WriteLine "F3 07 00 31 FA 10 // "
        stream.WriteLine "F3 07 00 31 FB " + Cells(27, 2) + " // "
        stream.WriteLine "F3 07 00 31 FC 00 // "
        stream.WriteLine "F3 25 00 31 FF " + Cells(33, 2) + "// "
        stream.WriteLine "F3 07 00 31 FA 11 // "

        stream.Close
End If

End Sub

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