Results 1 to 5 of 5

Thread: [RESOLVED] Word 2003 VBA code stops running when text file is opened

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2012
    Posts
    3

    Resolved [RESOLVED] Word 2003 VBA code stops running when text file is opened

    I am running vba code from a MS Word 2003 .dot file. The code opens an existing text file as plaintext and makes changes to the text file, saves the changes and closes the file. The code runs fine --except-- if Word is closed and then I open a Word file, attach the .dot and run the code, then the code stops running as soon as the /text file is opened. The file is opened and displayed in Word and then the code stops running. If I close the text file and re-run the code it always runs perfectly. The code only stops running if I execute it when the code is run when Word is first started. The run the code, I opened the .dot file in Word, clicked on Tools > Customize > clicked Macro on the left side of the Commands tab and dragged the macro from the right side up to the menu bar.
    To test this first you need to create a text file and set the path in the VBA code to that test file, make sure the text file has the words "original word" in it, and also create the menu so you can click that to run the code. Start the test by making sure Word is closed, then open Word, attach the .dot file, then click the macro that you previously dragged to the menu bar.
    Please excuse my ignorance, I am new at this, I have no idea what I am doing wrong or how to do it correctly.

    Code:
    Sub bMin()
    Dim MyString
    Documents.Open FileName:="C:\test.txt", ConfirmConversions:=False, Format:=wdOpenFormatText
        Selection.Find.ClearFormatting
        With Selection.Find
            .Text = "original word"
            .Replacement.Text = "new word"
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
    Documents.Save NoPrompt:=True, OriginalFormat:=wdOriginalDocumentFormat
    Documents.Close
    End Sub

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Word 2003 VBA code stops running when text file is opened

    probably the problem is you do not specify which document you are working with, letting word guess or save and close all open documents

    if the code, posted above, is the sum of your code, i doubt i would open the text file as a word document at all, preferring to work directly with file i/o
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2012
    Posts
    3

    Re: Word 2003 VBA code stops running when text file is opened

    The code I posted is not all of the code, it is the briefest possible amount of code to demonstrate the problem. You doubt that the code will open a text file as a word document? Why would I say that this code opens a word doc if it doesn't? *** is that all about? If you doubt it, try it. If "specifying which document you are working with" will solve the problem, perhaps you can give an example of how to do that.

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Word 2003 VBA code stops running when text file is opened

    You doubt that the code will open a text file as a word document?
    not at all, i just suggested i would maybe do it differently

    Code:
    set mydoc = Documents.Open (FileName:="C:\test.txt", ConfirmConversions:=False, Format:=wdOpenFormatText)
    you can then work with mydoc object variable
    Code:
    mydoc.save NoPrompt:=True, OriginalFormat:=wdOriginalDocumentFormat
    mydoc.close
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2012
    Posts
    3

    Thumbs up Re: Word 2003 VBA code stops running when text file is opened

    I apologize for my cranky response. THANK YOU!! you got it. I was certain that I had tried using "Set mydoc = Documents.Open . . . . . " Thank you soo much, I really appreciate your help and you sharing your knowledge and expertise, I was about to give up on this code. This code (the full version) is going to save me a lot of time and potential errors.

Posting Permissions

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



Click Here to Expand Forum to Full Width