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