Show content file .txt in msgbox

Gilang

New Member
Joined
Feb 21, 2021
Messages
48
Office Version
  1. 2007
Platform
  1. Windows
I created a txt file with notepad with the name Message.txt the content "please read carefully the message that appears"
I want this message to appear in the msgbox. is there a way to call it and display in vba msgbox?
 

Attachments

  • Message.png
    Message.png
    5.9 KB · Views: 59

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
See here for how to do that: Read text file into string variable

Then you would just add:
VBA Code:
MsgBox strFileContent
to the bottom of the code to return that in a message box.

Just be sure to update the file path/name in the code to watch your text file.
 
Upvote 0
Did you look at that link? You pretty much copy that code, verbatim, dropping it into a Sub procedure.
The file path/name should be pretty evident (it is the only file path/name in the whole code).
Rich (BB code):
Sub MyReturnString()

Dim strFilename As String: strFilename = "C:\temp\yourfile.txt"
Dim strFileContent As String
Dim iFile As Integer: iFile = FreeFile
Open strFilename For Input As #iFile
strFileContent = Input(LOF(iFile), iFile)
Close #iFile

MsgBox strFileContent

End Sub
The file name/path in red is the part you need to replace your your text file path/name.
The line in blue is the part we added to the original code to return the value in the message box.
 
Upvote 0
Did you look at that link? You pretty much copy that code, verbatim, dropping it into a Sub procedure.
The file path/name should be pretty evident (it is the only file path/name in the whole code).
Rich (BB code):
Sub MyReturnString()

Dim strFilename As String: strFilename = "C:\temp\yourfile.txt"
Dim strFileContent As String
Dim iFile As Integer: iFile = FreeFile
Open strFilename For Input As #iFile
strFileContent = Input(LOF(iFile), iFile)
Close #iFile

MsgBox strFileContent

End Sub
The file name/path in red is the part you need to replace your your text file path/name.
The line in blue is the part we added to the original code to return the value in the message box.
I've tried it, but what appears is the data in the sheets, not showing the data in the txt file that I want.
 
Upvote 0
Upvote 0
That is impossible except if the textfile has the same content than the sheet ! …​
 
Upvote 0
I don't see how that could be, unless you are running the wrong procedure.
If you can upload your Excel file and text file to a file sharing site and provide a link to it, we can try it for ourselves.
Just be sure to remove any sensitive data from the files first.
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top