VBA Excel:使用另存为对话框文本文件/自定义文件types

我有一个Excelmacros,它将特定的string写入文本文件。 问题是,我需要将其保存为自定义文件(“.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 

我能够通过select文件path通过saveAs对话框来解决我的问题。 我惊讶地没有文件filter的问题。

 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