Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-02-2020, 12:04 PM
marceepoo marceepoo is offline Is it possible to use a .txt file to populate a msgbox prompt field Windows 7 64bit Is it possible to use a .txt file to populate a msgbox prompt field Office 2010 64bit
Novice
Is it possible to use a .txt file to populate a msgbox prompt field
 
Join Date: Sep 2012
Posts: 22
marceepoo is on a distinguished road
Default Is it possible to use a .txt file to populate a msgbox prompt field

I get a call stack error when I try to use the following code to populate a msgbox prompt field:


Quote:
Dim TextFile As Integer
Dim FilePath As String
Dim FileContent As String

'File Path of Text File
FilePath = sFullname ' "E:\Zmani\Logging\202004011500__Tmp_61385499383529 09.py_.txt"

'Determine the next file number available for use by the FileOpen function
TextFile = FreeFile

'Open the text file
Open FilePath For Input As TextFile

'Store file content inside a variable
FileContent = Input(LOF(TextFile), TextFile)

'Close Text File
Close TextFile

'Report Out Text File Contents
MsgBox FileContent
The following is the text inside the file ["202004011500__Tmp_6138549938352909.py_.txt"] I'm trying to use as data to populate the Msgbox prompt field:

Quote:
1. Check the "Name of Bookmark" and "Exhibit No." fields below to make sure that the data in those fields is correct.
2. If the data in those fields is correct: Push the light blue colored button (in the lower right hand corner of the form) entitled "Insert Bookmark Where the Cursor is Situated".
3. But if the data in those fields is NOT correct:
[A] Move your cursor to the paragraph (in the Table of Exhibits) to which you want to make a cross-reference; and then ...
[B] Push the light green "Update Fields" button.
NB: Pushing that button will update the fields, and return your cursor to the place where it was when you pushed the "Update Fields" button.

This form will remain open until you close it, in order to make it possible for you to manually populate as many field as you want, by going to paragraphs in the Table of Exhibits and then clicking the "Update Fields" button.
I would prefer to populate my Error messages in my programs with uniform text that I could easily save in text files or rtf or doc or docx or html, etc files, instead of having to code the messages in vba, line by line, etc., e.g.:

"ERROR Message from Module " & chr(34) & strModuleName & chr(34) & _
"a Macro named, " & chr(34) & strMacroName & Chr(34) & _
"[etcetera]"

I frequently modify my error messages and other messages, and it would be much easier for me to produce my standardized text using a python script; and I would save a lot of time if I could import those error message files into a msgbox prompt field.

I would appreciate any suggestions, that might [1] enable me to import text to Msgbox prompt field from files, and/or [2] help me learn more about call stacks, and how to handle call stack issues.

Thanks,
Marc
Reply With Quote
  #2  
Old 04-02-2020, 08:33 PM
gmayor's Avatar
gmayor gmayor is offline Is it possible to use a .txt file to populate a msgbox prompt field Windows 10 Is it possible to use a .txt file to populate a msgbox prompt field Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Maybe the following is more what you had in mind.

Code:
Dim TextFile As Integer
Dim FilePath As String
Dim FileContent As String
Dim FileNum As Integer
Dim DataLine As String

    'File Path of Text File
    FilePath = "E:\Zmani\Logging\202004011500__Tmp_61385499383529 09.py_.txt"
    

    FileNum = FreeFile()
    Open FilePath For Input As #FileNum

    While Not EOF(FileNum)
        Line Input #FileNum, DataLine
        FileContent = FileContent & DataLine & vbCr
    Wend
    FileContent = Left(FileContent, Len(FileContent) - 1)
    Close

    'Report Out Text File Contents
    MsgBox FileContent
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 04-26-2020, 01:24 PM
marceepoo marceepoo is offline Is it possible to use a .txt file to populate a msgbox prompt field Windows 7 64bit Is it possible to use a .txt file to populate a msgbox prompt field Office 2010 64bit
Novice
Is it possible to use a .txt file to populate a msgbox prompt field
 
Join Date: Sep 2012
Posts: 22
marceepoo is on a distinguished road
Default

Dear Graham:

While doing some research to understand how and why your solution worked, I came across the following:

The "FreeFile function" page (located at: FreeFile function (Visual Basic for Applications) | Microsoft Docs), which led me to:
1. The "Open statement" page (located at: 404 - Content Not Found | Microsoft Docs and
2. The "Writing data to files" page (located at: Writing data to files (VBA) | Microsoft Docs)
which contained two information items that perplex me:

The first item was an admonition not to use the Open statement in Word Vba:
Note[:] The Open statement should not be used to open an application's
own file types. For example, don't use Open to open a Word document,
a Microsoft Excel spreadsheet, or a Microsoft Access database.
Doing so will cause loss of file integrity and file corruption.
But my subsequent use of Word's macro recorder (View, Record macro ...), seemed to recommend that I may or should use the Open statement:
Code:
Sub subOpenExistingWordDoc()
        '
            ChangeFileOpenDirectory _
                "E:\DirName"
            Documents.Open FileName:="Filename.docx", ConfirmConversions:=False, _
                 ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="", _
                PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
                WritePasswordTemplate:="", Format:=wdOpenFormatAuto, XMLTransform:="", _
                DocumentDirection:=wdLeftToRight
        End Sub
I think that I am missing some idea that has a role in why you used the FreeFile function in your solution.
I recognize that you were using the Open statement for reading as distinguished from writing.
But I also suspect that I am missing something central.
I would appreciate it if you could point me to where I could get some insight into what the admonition not to use the Open statement is all about.


The second item that perplexed me on "Writing data to files" page was the table stating that I could write to a file using either the "Print #" or "Write #" statements.

I could not find pages where Microsoft explains how each of those statements operate, e.g., the syntax and any related information, particularly for the "Print #" statement, which I have never seen used in VBA for the purpose of recording information to a file.
Do you know where I could find more info about the "Print #" and "Write #" statements, to which that page was referring?

If I shouldn't be asking you this type of question, or if you don't have time to waste on these things, please forgive me for asking.

Thanks again the help you gave me.

Marc
Reply With Quote
  #4  
Old 04-26-2020, 08:24 PM
gmayor's Avatar
gmayor gmayor is offline Is it possible to use a .txt file to populate a msgbox prompt field Windows 10 Is it possible to use a .txt file to populate a msgbox prompt field Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Maybe The VBA Guide To Interacting With Text (.Txt) Files — The Spreadsheet Guru will help?
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #5  
Old 04-27-2020, 08:34 AM
marceepoo marceepoo is offline Is it possible to use a .txt file to populate a msgbox prompt field Windows 7 64bit Is it possible to use a .txt file to populate a msgbox prompt field Office 2010 64bit
Novice
Is it possible to use a .txt file to populate a msgbox prompt field
 
Join Date: Sep 2012
Posts: 22
marceepoo is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
Thank you. Much appreciated.
Marc
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
How To Make 2013 Populate To Field abraxis Outlook 2 08-10-2018 06:41 AM
populate list with unique field value on repeated field value sukusnairss Excel 1 04-03-2018 11:47 PM
Have Text Entered in Content Control / PreDefined Field Populate Field in Footer bzowk Word 1 04-14-2016 11:50 AM
Is it possible to use a .txt file to populate a msgbox prompt field Prompt a macro to run when opening a specific file type gmanword Word VBA 5 11-30-2013 02:55 PM
Openin File in Use Locks PC Up, No Read only Prompt barkster Office 0 06-08-2010 01:22 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:55 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft