VBA Excel:使用另存为对话框的文本文件/自定义文件类型 [英] VBA Excel: Using SaveAs Dialog for Text Files/Custom File Types

查看:133
本文介绍了VBA Excel:使用另存为对话框的文本文件/自定义文件类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Excel宏,它将特定的字符串写入文本文件.问题是,我需要将其另存为自定义文件(".us1").我在下面附加我的当前代码.我最终对打开"对话框做了一个怪异的逆转.我要怎么做才能将此代码切换为使用另存为"对话框?

I have an Excel macro which writes a specific string to a text file. The catch is, I need to save it as a custom file (".us1"). I'm attaching my current code below. I ended up doing a weird reversal of the Open dialog. What would I do to switch this code to using the SaveAs Dialog?

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对话框中选择文件路径来解决我的问题.令人惊讶的是,我的文件过滤器没有任何问题.

I was able to solve my problem by selecting the file path THROUGH the saveAs dialog. I surprisingly had no issues with the file 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

这篇关于VBA Excel:使用另存为对话框的文本文件/自定义文件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆