• vba Shell command for text file

    Author
    Topic
    #506910

    I have a macro that crashes so totally that it crashes Word, leaving no error messages. I want the macro to write a log to a .txt file, so that when Word crashes, the .txt file will still be open and I can use the log to figure out what’s going on.

    I can use this shell command to open notepad:

    Call shell (“notepad.exe”, vbMinimizedFocus)

    However, I can’t figure out how to name the file. Adding the desired filename to the shell command listed above makes the vba look for a .txt file with that name – which doesn’t exist yet.

    Can someone help me do the following:

    1) Use a Word macro to open a new text file

    2) Give the text file the same name as the word file running the macro

    3) Append text to the text file during runtime

    Any help (or better ideas!) appreciated.

    Viewing 2 reply threads
    Author
    Replies
    • #1577753

      Try https://logicallyproven.blogspot.com.au/2015/02/how-to-create-log-file-in-excel-and.html

      But a better idea would be to put break points in your code to help work out where the problem lies. If you can narrow it down to a specific line of code then you can work out how to fix the problem.

    • #1577780

      Thank you.

      Using an actual log file may be just as well, though I don’t know that I would have as much control over line breaks etc. (I will look into that.)

      As for break points: this macro is in a global template loaded whenever users open their instance of Word. It will therefore run on PCs used by people with varying levels of understanding of Word (to put it mildly). My aim is to have the logging function controlled by a switch, so that when circumstances trigger the crash, I can ask them to re-run the macro with the logging function on (doubtless slowing the macro considerably). The result will be a file they can email me.

    • #1579272

      Stylus,

      The following code will allow you to create a text file with the same name as your Word document, write to the file, then close it.

      Code:
      Public Sub Write2Txt()
          FilePath = “C:UsersMaudibeDesktop”
          DocName = Split(ThisDocument.Name, “.”)(0) & “.txt”
          Open DocName For Output As #2
              [COLOR=”#008000″]’WRITE TO THE TEXT FILE[/COLOR]
          Close #2
      End Sub
      
      

      Replace the FilePath with your path

      HTH,
      Maud

    Viewing 2 reply threads
    Reply To: vba Shell command for text file

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: