将组合框列表保存到.txt文件中

时间:2019-07-07 17:39:34

标签: excel vba

VBA:是否可以将组合框列表保存到.txt文件?

我在这里做了这件事,将txt文件的信息放入组合框

Dim InFile As Integer
InFile = FreeFile
Open "MYFILE.txt" For Input As InFile
While Not EOF(InFile)
  Line Input #InFile, NextTip
   ComboBox1.AddItem NextTip
   ComboBox1.ListIndex = 0

Wend
Close InFile

1 个答案:

答案 0 :(得分:0)

下面的宏将打印从指定组合框到指定文本文件的项目列表。相应地更改宏的名称以及目标文件的路径和文件名。

    'Force the explict declaration of variables
    Option Explicit

    Private Sub CommandButton1_Click()

        'Declare the variables
        Dim destFile As String
        Dim fileNum As Long
        Dim i As Long

        'Assign the path and file name for the destination file (change accordingly)
        destFile = "C:\Users\Domenic\Desktop\sample.txt"

        'Get the next available file number
        fileNum = FreeFile()

        'Print list items from combobox to destination file
        Open destFile For Output As #fileNum
            With Me.ComboBox1
                For i = 0 To .ListCount - 1
                    Print #fileNum, .List(i)
                Next i
            End With
        Close #fileNum

        MsgBox "Completed!", vbExclamation

    End Sub