Share via

Help with Reading from Text File

Anonymous
2014-03-16T22:15:29+00:00

I'm working with VBA in Microsoft Word 2010, and I'd like to know if there is a way for me to use a code to read/get lines from a Text file, and then put them into an Array for a ComboBox. This way I can add or remove items from the ComboBox by simply editing the text file.

Is there a way to accomplish this? How about using other types of files, like Excel or something? If there is could someone help me, I'll need to know the codes to put in to read from the file, put them into an array, and then how to get the ComboBox to add items from the array.

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

8 answers

Sort by: Most helpful
  1. Steve Rindsberg 99,166 Reputation points MVP Volunteer Moderator
    2014-03-17T01:20:11+00:00

    You'll need to adapt this for Word.  I've written it for PowerPoint.

    Sub TestTxtToCbo()

        Dim cbo As Object

        Set cbo = ActivePresentation.Slides(1).Shapes(1).OLEFormat.Object

        TxtToCbo cbo, "c:\temp\test.txt"

    End Sub

    Sub TxtToCbo(cbo As Object, sFileName As String)

    ' Pass a reference to the combo in the cbo object variable

    ' and the full path to your text file in sFileName

        Dim iNotesFileNum As Integer

        Dim sBuf As String

        ' is it there? quit if not

        If Len(Dir$(sFileName)) = 0 Then

            MsgBox sFileName & " is missing"

            Exit Sub

        End If

        ' open the file and go to work

        iNotesFileNum = FreeFile()

        Open sFileName For Input As iNotesFileNum

        While Not EOF(iNotesFileNum)

            Line Input #iNotesFileNum, sBuf

            cbo.AddItem (sBuf)

        Wend

        ' close the file

        Close iNotesFileNum

    End Sub

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2014-03-17T00:17:46+00:00

    Okay, so that code will populate the ComboBox upon opening the document.

    What would be the code to take the items from a different Text file and put them in the combobox?

    Example.

    Report.doc is the form used to file the report. It is also the document that when opened needs to fill the combobox.

    Users.txt is the text file that I want to take data from and populate Report.doc's combobox with.

    Ex. Users.txt when opened looks like this:

    John

    Joe

    James

    What codes do I use, to take those usernames from Users.txt and put them into Report.doc's combobox when Report.doc opens?

    Apologizes if this is hard to answer. I'm relearning VBA as I go and just need to figure this one bit out.

    Was this answer helpful?

    0 comments No comments
  3. Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
    2014-03-16T23:51:52+00:00

    While I would use a userform, the following code in the This Document object will load a combobox

    Private Sub Document_Open()

    Dim i As Long

    For i = 1 To 10

        ComboBox1.AddItem "Item" & i

    Next i

    End Sub

    You probably however should be creating a template in which case you should use the Document_New() event.

    For more information on userforms, see the following page of Greg Maxey's website:

    http://gregmaxey.mvps.org/Create\_and\_employ\_a\_UserForm.htm

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2014-03-16T23:03:14+00:00

    Okay, here's a little more info.

    I'm creating a "form" that allows my work and co-workers to fill out reports each day. So in Word 2010 I have the forms with the necessary comboboxes for their names.

    I would like to know, if there is a way upon opening the Word document, to populate the combobox for the worker's names via another text file located in the same folder as the Report form.

    So in other words, the Word document is the active one, and I want to populate the combobox in it with text from a different file altogether. Can this be done upon "Document_Open()"

    Little more information. I'm not using a UserForm, I've simply placed VBA objects inside the regular Word document since the reports needs to be saved in the Word document. Is using a UserForm the only way to populate ComboBoxes from outside sources? Like a Text file?

    Example.

    Was this answer helpful?

    0 comments No comments
  5. Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
    2014-03-16T22:50:18+00:00

    If the text file is the active document, you can use the following to populate the text box

    With mycombobox

        For i = 1 To ActiveDocument.Paragraphs.count

            .AddItem ActiveDocument.Paragraphs(i).range.text

        Next i

    End With

    Also see the following page of Greg Maxey's website:

    http://gregmaxey.mvps.org/Populate\_UserForm\_ListBox.htm

    Was this answer helpful?

    0 comments No comments