We help IT Professionals succeed at work.

VBA: exposing word "document . content' property

boatful
boatful asked
on
I have a word document saved, as a template for a report.  It contains several tags in the form <*>. For each of these tags, I wish to insert a string generated programatically in VB.

The following lines of code show exactly where I get a VB error, and the nature of the error thrown.  Debug, anyone??

Dim WordDoc As Object
Dim strContentString As String

Set WordDoc = CreateObject("c:\DrDates.doc")

strContentString = WordDoc.content
MsgBox strContentString
'SO FAR, SO GOOD

strContentString = Replace(strContentString, "<urgent>", "WOW!")
MsgBox strContentString
'SO FAR, SO GOOD

WordDoc.content = strContentString
' ERROR: "method 'CONTENT' of object 'DOCUMENT' failed"

WordDoc.SaveAs "C:\TEST_MACRO_DOC.DOC"
WordDoc.Application.Quit
Set WordDoc = Nothing
Comment
Watch Question

Head of Software Services
CERTIFIED EXPERT
Commented:
Much easier to use word's find and replace mechanism. It is significantly faster than this method:

With Application.Selection.Find
  .Text = "<urgent>"
  .Replacement = "WOW!"
  .Execute
End With
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
i agree with TimCottee!

Besides of that:
WordDoc.Range.Text = strContentString

CHeers

Author

Commented:
Thanks you two!
Would the solution you give still apply if "<urgent>" were located inside a textbox in the word document??

Author

Commented:
Something is awry...
The messagebox consistently returns "false",
and an error occurs when I hit ".Replacement = ..."
I may need a bit more guidance...
*******************************
With Application.Selection.Find
.Text = "<urgent>"
MsgBox (.Found)
.Replacement = "WOW!"
.Execute
End With
End Sub

Author

Commented:
The syntax for line...
.Replacement = "WOW!"
...is...
.Replacement.Text = "WOW!"

This eliminates the error there,
but still "<urgent>" never gets replaced by "WOW!".

What is missing???
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
You are right that if the text is inside a textbox, the code will have to look completely different, but i can't find it right now in my head...

Author

Commented:
let's forget the textbox case.  I will be happy if the code is able to find/replace text in the main body of the document.
Question(s) below appears to have been abandoned. Your options are:
 
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you. You must tell the participants why you wish to do this, and allow for Expert response.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question. Again, please comment to advise the other participants why you wish to do this.

For special handling needs, please post a zero point question in the link below and include the question QID/link(s) that it regards.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click the Help Desk link on the left for Member Guidelines, Member Agreement and the Question/Answer process.  Click you Member Profile to view your question history and keep them all current with updates as the collaboration effort continues, in the event new items have been created since this listing was pulled.

http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.20098019.html
http://www.experts-exchange.com/questions/Q.20190409.html
http://www.experts-exchange.com/questions/Q.20203846.html
http://www.experts-exchange.com/questions/Q.20251787.html
http://www.experts-exchange.com/questions/Q.20260234.html
http://www.experts-exchange.com/questions/Q.20260235.html

PLEASE DO NOT AWARD THE POINTS TO ME.  
 
------------>  EXPERTS:  Please leave any comments regarding this question here on closing recommendations if this item remains inactive another three days.
 
Thank you everyone.
 
Moondancer
Moderator @ Experts Exchange


P.S.  For any year 2000 questions, special attention is needed to ensure the first correct response is awarded, since they are not in the comment date order, but rather in Member ID order.

Author

Commented:
CLose enough, Tim.  I find that I do best to record a WORD macro, and then look at the commands generated in the VB editor.  The plethora of VBA objects would take a PhD to learn.  Leave it to MIRCOSOFT!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.