Results 1 to 4 of 4
  1. #1
    2 Star Lounger
    Join Date
    May 2006
    Location
    Currently in Europe
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts

    vba Shell command for text file

    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.
    Stylus

  2. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    Try https://logicallyproven.blogspot.com...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.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  3. The Following User Says Thank You to Andrew Lockton For This Useful Post:

    Stylus (2016-09-15)

  4. #3
    2 Star Lounger
    Join Date
    May 2006
    Location
    Currently in Europe
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts
    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.
    Stylus

  5. #4
    Gold Lounger Maudibe's Avatar
    Join Date
    Aug 2010
    Location
    Pa, USA
    Posts
    2,633
    Thanks
    115
    Thanked 647 Times in 590 Posts
    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:\Users\Maudibe\Desktop\"
        DocName = Split(ThisDocument.Name, ".")(0) & ".txt"
        Open DocName For Output As #2
            'WRITE TO THE TEXT FILE
        Close #2
    End Sub
    Replace the FilePath with your path

    HTH,
    Maud

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •