By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,587 Members | 1,071 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,587 IT Pros & Developers. It's quick & easy.

change text in microsoft word programatically

P: 88
hi

i would like to open a document template (saved as a regular .doc), replace a couple of instances of "XXXX" with a user-specified number. i imported the library "microsoft word 11.0 object library" and tried playing around a little with the syntax help in vb6, but didn't find anything helpful.

is there an easy way to open a document, run a replace function, save it with a new file name and close all open documents programatically?

br / bm
Sep 4 '07 #1
Share this Question
Share on Google+
11 Replies


kadghar
Expert 100+
P: 1,295
I think thats possible.

First of all, it would be nice to know what version of VB you're using, since the VBA of word could make things way easier than VB6.

Then in the while, why dont you read a little bit about some sintaxis like Selection, Find, Replacement syntaxis, just google them.
Sep 4 '07 #2

QVeen72
Expert 100+
P: 1,445
Hi,

Check this :

Expand|Select|Wrap|Line Numbers
  1. '
  2.     Selection.WholeStory
  3.     Selection.Find.ClearFormatting
  4.     Selection.Find.Replacement.ClearFormatting
  5.     With Selection.Find
  6.         .Text = "Old"
  7.         .Replacement.Text = "New"
  8.         .Forward = True
  9.         .Wrap = wdFindAsk
  10.         .Format = False
  11.         .MatchCase = False
  12.         .MatchWholeWord = False
  13.         .MatchWildcards = False
  14.         .MatchSoundsLike = False
  15.         .MatchAllWordForms = False
  16.     End With
  17.     Selection.Find.Execute Replace:=wdReplaceAll
  18.     ActiveDocument.Save
  19.  
REgards
Veena
Sep 4 '07 #3

kadghar
Expert 100+
P: 1,295
Hi,

Check this :

Expand|Select|Wrap|Line Numbers
  1. '
  2.     Selection.WholeStory
  3.     Selection.Find.ClearFormatting
  4.     Selection.Find.Replacement.ClearFormatting
  5.     With Selection.Find
  6.         .Text = "Old"
  7.         .Replacement.Text = "New"
  8.         .Forward = True
  9.         .Wrap = wdFindAsk
  10.         .Format = False
  11.         .MatchCase = False
  12.         .MatchWholeWord = False
  13.         .MatchWildcards = False
  14.         .MatchSoundsLike = False
  15.         .MatchAllWordForms = False
  16.     End With
  17.     Selection.Find.Execute Replace:=wdReplaceAll
  18.     ActiveDocument.Save
  19.  
REgards
Veena
That will work for VBA in Word, remember that when you record a macro, it uses as many constants as posible. If you use VBA in PowerPoint, for example. and you create an object with a Word Application in it, it wont recognize "wdReplaceAll" since its a Word constant that PowerPoint's VBA dont know.

That's why its important to know which version of VB we are using.
Sep 4 '07 #4

QVeen72
Expert 100+
P: 1,445
That will work for VBA in Word, remember that when you record a macro, it uses as many constants as posible. If you use VBA in PowerPoint, for example. and you create an object with a Word Application in it, it wont recognize "wdReplaceAll" since its a Word constant that PowerPoint's VBA dont know.

That's why its important to know which version of VB we are using.

Hi,

The Question was to Replace in "Word" na, I dont think he wanted for PowerPoint...

Regards
Veena
Sep 4 '07 #5

kadghar
Expert 100+
P: 1,295
Ok anyway, since he was playing around with some VB6 sintax....

Anyway, for Word, may be this will be of help:

Expand|Select|Wrap|Line Numbers
  1. Sub ReplaceTxt()
  2. Dim Str1 As String
  3. Dim Int1 As Integer
  4. With Application.FileDialog(3)
  5.     .Filters.Clear
  6.     .Filters.Add "Word File (*.doc)", "*.doc"
  7.     .Filters.Add "All Files (*.*)", "*.*"
  8.     .Show
  9.     If .SelectedItems.Count > 0 Then
  10.         Str1 = .SelectedItems(1)
  11.     Else
  12.         Exit Sub
  13.     End If
  14. End With
  15. Documents.Open (Str1)
  16. Int1 = Documents.Count
  17. Documents(Int1).Activate
  18. With Selection.Find
  19.     .Text = InputBox("Text to be replaced", "Replacement", "")
  20.     .Replacement.Text = InputBox("With", "Replacement", "")
  21.     .Wrap = 1
  22.     .Execute Replace:=2
  23. End With
  24. Documents(Int1).Close savechanges:=True
  25. If MsgBox("Check another Document?", 4) = 6 Then Call ReplaceTxt
  26. End Sub
Sep 4 '07 #6

P: 88
i actually want to do it in a macro written in the macro editor in outlook (the one you open by pressing alt+f11 while outlook is focused), so all of your guesses were wrong =). i just assumed this editor was using vb6, but i might be wrong...? my main focus has been asp and asp.net for a long time, even though my programming career started with vb6 at age 12 =). i guess a lot has happened since...

to be more specific with what i need:

in my job i often use a standardized word document to give information to our customers. every time i send one of these, i need to open the standard doc, replace three instances of XXXX with a four digit issue number, close it and attach it to the email.
attaching it to the email is not that big of a deal, but this open word - do a couple of things - save as - close word takes a lot of unecessary time, so i hoped i would be able to do that by just pressing a button directly in outlook.
Sep 5 '07 #7

P: 21
i actually want to do it in a macro written in the macro editor in outlook (the one you open by pressing alt+f11 while outlook is focused), so all of your guesses were wrong =). i just assumed this editor was using vb6, but i might be wrong...? my main focus has been asp and asp.net for a long time, even though my programming career started with vb6 at age 12 =). i guess a lot has happened since...

to be more specific with what i need:

in my job i often use a standardized word document to give information to our customers. every time i send one of these, i need to open the standard doc, replace three instances of XXXX with a four digit issue number, close it and attach it to the email.
attaching it to the email is not that big of a deal, but this open word - do a couple of things - save as - close word takes a lot of unecessary time, so i hoped i would be able to do that by just pressing a button directly in outlook.
Just had a really quick look at Outlook, you can create a custom tool bar, then using vb, design yourself a nice window, with a replace "something" textbox with "something else" text box... behind that form you could then put all the code mentioned above... then add a button to the custom menu to launch your new form...

Just thinking outloud, I wouldnt know how to do this, but just looking quickly into it I think it could be a way forward?


Edit: Yeah, 5 min of poking around, you can add a custom toolbar, then add a button onto that toolbar that will open up a custom form, from there you should be able to do anything you want as long as the commands are supported by Outlook VBA...

Literally create a new form in the VB editor, make that form do what you want it to do and then add it as a button...

Hope this helps!

Martin
Sep 5 '07 #8

kadghar
Expert 100+
P: 1,295
i actually want to do it in a macro written in the macro editor in outlook (the one you open by pressing alt+f11 while outlook is focused), so all of your guesses were wrong =). i just assumed this editor was using vb6, but i might be wrong...? my main focus has been asp and asp.net for a long time, even though my programming career started with vb6 at age 12 =). i guess a lot has happened since...

to be more specific with what i need:

in my job i often use a standardized word document to give information to our customers. every time i send one of these, i need to open the standard doc, replace three instances of XXXX with a four digit issue number, close it and attach it to the email.
attaching it to the email is not that big of a deal, but this open word - do a couple of things - save as - close word takes a lot of unecessary time, so i hoped i would be able to do that by just pressing a button directly in outlook.
Of course its posible, just create an object with a Word Application in it and work with it :) the code i put above. Should be something like this:

Expand|Select|Wrap|Line Numbers
  1. Sub Replacing()
  2. Dim Obj1 As Object
  3. Dim Str1 As String
  4. Dim Int1 As Integer
  5. Set Obj1 = CreateObject("word.application")
  6. With Obj1.FileDialog(3)
  7.     .Filters.Clear
  8.     .Filters.Add "Word File (*.doc)", "*.doc"
  9.     .Filters.Add "All Files (*.*)", "*.*"
  10.     .Show
  11.     If .SelectedItems.Count > 0 Then
  12.         Str1 = .SelectedItems(1)
  13.     Else
  14.         Exit Sub
  15.     End If
  16. End With
  17. Obj1.Documents.Open (Str1)
  18. Obj1.Visible = False
  19. With Obj1.Selection.Find
  20.     .Text = InputBox("Text to be replaced", "Replacement", "")
  21.     .Replacement.Text = InputBox("With", "Replacement", "")
  22.     .Wrap = 1
  23.     .Execute Replace:=2
  24. End With
  25. Obj1.Visible = True
  26. Obj1.Documents(1).Close savechanges:=True
  27. Obj1.Quit
  28. If MsgBox("Check another Document?", 4) = 6 Then Call Replacing
  29. End Sub
Sep 5 '07 #9

kadghar
Expert 100+
P: 1,295
oh, please note that i had to use the FileDialogs of the Word Application, because the VBA of Outlook doesnt support them (dunno why)
Sep 5 '07 #10

P: 88
i actually wanted everything to work without the open file dialog, or any other on-screen action. here's a sub i found worked pretty well:

Expand|Select|Wrap|Line Numbers
  1. Sub CreateRMADoc()
  2.     Dim appWord As New Word.Application
  3.  
  4.     Dim intRMA, i As Integer
  5.     Dim strDate, strFileName As String
  6.     Dim Range As Range
  7.  
  8.     'Define the path where to save the RMA.doc
  9.     intRMA = InputBox("RMA Number:", "Create RMA Document", "")
  10.     strDate = Format(Now(), "yyyy-mm-dd")
  11.     strFileName = "CAR " & intRMA & " Repair and RMA Instruction Ericsson.doc"
  12.  
  13.     'Open the RMA Word Template
  14.     appWord.Documents.Open ("C:\Documents and Settings\qtomlyc\My Documents\Attachments\TEMPLATE Repair and RMA Instruction Ericsson.doc")
  15.  
  16.     ' Make Word invisible through the Application object.
  17.     appWord.Visible = False
  18.  
  19.     'Change date in document
  20.     For i = 1 To 15
  21.         Set Range = appWord.ActiveDocument.Content
  22.         Range.Find.Execute FindText:="2007-XX-XX", ReplaceWith:=strDate
  23.     Next i
  24.  
  25.     'Change reference of RMA in document
  26.      For i = 1 To 15
  27.         Set Range = appWord.ActiveDocument.Content
  28.         Range.Find.Execute FindText:="XXXX", ReplaceWith:=intRMA
  29.      Next i
  30.  
  31.     ' Save the new RMA document
  32.     appWord.ActiveDocument.SaveAs "C:\Documents and Settings\qtomlyc\My Documents\Attachments\CAR " & intRMA & " Repair and RMA Instruction Ericsson.doc"
  33.  
  34.  
  35.     ' Dispose objects to free memory
  36.     appWord.ActiveDocument.Close
  37.     Set appWord = Nothing
  38.     Set Range = Nothing
  39. End Sub
but i'm experiencing two problems:

1. it doesn't change anything in headers and footers, which i need it to. how do i access the header in the same manner as appWord.ActiveDocument.Content gives me all the content of the document? if possible, i would like to to this also with some text in a text box. however, i found a work-around to the later of the two by making a bitmap that looks like a textbox, and place it behind the text.

2. it seems that i'm not closing the files completely - the second time i run the macro i get a file sharing violation or something of that nature on the saveas line, stating that the file is already in use by another program. what can i do to close the file so that i can open and save again, without having to find the program still using the file, close it and then resume the macro?
Sep 6 '07 #11

P: 88
ok, i know double posting is probably not allowed, but i need help with this and i don't expect people to be looking at threads on page 4, so i'm bumping...

...anyone knows a solution to any of the problems stated above?
Sep 8 '07 #12

Post your reply

Sign in to post your reply or Sign up for a free account.