VBA Excel : 텍스트 파일 / 사용자 정의 파일 유형에 대한 SaveAs 대화 상자 사용

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

  •  02-01-2020
  •  | 
  •  

문제

특정 문자열을 텍스트 파일에 기록하는 Excel 매크로가 있습니다.catch는 사용자 정의 파일 ( ".us1")으로 저장해야합니다.아래의 현재 코드를 첨부하고 있습니다.나는 열린 대화 상자의 이상한 역전을 일으켰습니다.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
.

도움이 되었습니까?

해결책

SaveAs 대화 상자를 통해 파일 경로를 선택하여 문제를 해결할 수있었습니다.나는 놀랍게도 파일 필터에 문제가 없었습니다.

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
.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top