Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trying to replace string in word doc with vb macro

Status
Not open for further replies.

mikeyd

Technical User
Jan 28, 2002
38
US
I am new at this.
I need assistance with a macro I am trying to create.
I want to replace the letter m with the letter l in the word mike in a existing word file.
When I try running it I get
"Statement invalid outside Type Block"
Sub mikesproject()
'
' mikesproject Macro
' Macro created 3/19/2004 by mike
newdoc = "C:\work.txt"
Open newdoc For Random As #1
Replace("mike", "m", "l") As String
Close #1
End Sub
 
The string you want to replace is not "mike", but newdoc. Therefore, once you have openned newodc, you need to copy the whole documne t to a string variable. You then need to use the replace function on the newdoc, but replace "mike" with "like", otherwise, it will replace every m with an l.

BB
 
Thanks for your reply.
Can you show me a example of "you need to copy the whole documnet to a string variable".
Any help is appreciated.
Thanks
 
You say in your subject line: <string in word doc with vb macro> but you appear to be looking in a .txt document. You also mention <macro>.

Is this a VB6 question, or a Word VBA question?

If this is a Word Macro question, the easiest way to see how macros work is to use Tools|Macros|Record new macro. Go through all the actions you want in the macro, stop the recorder tool and look at the code produced.

If this is a VB6 question about looking in a text file then lookup the Input function in VBHelp, and the FileSystemObject in VBHelp. If using the FSO look up ReadAll method of the TextStream object and then use the Replace method on the string produced by ReadAll

You will probably get clearer answers if you read faq222-2244 before posting your question, as VBA questions will generally do better in the VBA forum.

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
It is a word doc. I was just using .txt as a example file.
I am using a vba program from a ibm 3270 emulator(reflections). I want to get info that i got through a script and when done with that part send that data into a word doc. I can open the file fine. I want it to replace the string "username" with a text variable I captured in the script.
Like I said I can open the file but I can not replace the "username" in the doc.
By the way thanks for taking the time to respond to this post.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top