Сохраните список со списком в файл .txt - PullRequest
0 голосов
/ 07 июля 2019

VBA: Есть ли способ сохранить список со списком в файл .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 голосов
/ 07 июля 2019

Следующий макрос напечатает список элементов из указанного комбинированного списка в указанный текстовый файл. Измените имя макроса, а также, соответственно, путь и имя файла назначения.

    '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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...