Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version
  1. First, you need to open the .txt file using VBA.

  2. Then, read the contents of the file using the VBA File Access Library. You can do this using the following code:

Open "filename.txt" For Input As #1
fileContent = Input(LOF(1), #1)
Close #1
  1. Next, you can insert the string into the label using the following code:
yourLabel.Caption = fileContent
  1. Finally, you can close the file by using the following code:
Close #1

Putting it all together, the code for reading a .txt file and inserting a string into a label in VBA might look something like this:

Sub ReadTxtFile()
    Dim fileContent As String

    ' Open file
    Open "filename.txt" For Input As #1

    ' Read file contents
    fileContent = Input(LOF(1), #1)

    ' Insert file contents into label
    yourLabel.Caption = fileContent

    ' Close file
    Close #1
End Sub

Note: Replace "filename.txt" and "yourLabel" with the actual file name and label name in your code.