[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
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
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.
Re: Word 2003 VBA code stops running when text file is opened
Quote:
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
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.