Link to home
Start Free TrialLog in
Avatar of boatful
boatful

asked on

VBA: exposing word "document . content' property

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
ASKER CERTIFIED SOLUTION
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Guy Hengel [angelIII / a3]
i agree with TimCottee!

Besides of that:
WordDoc.Range.Text = strContentString

CHeers
Avatar of boatful
boatful

ASKER

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

ASKER

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
Avatar of boatful

ASKER

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???
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...
Avatar of boatful

ASKER

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.
https://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.

https://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.
https://www.experts-exchange.com/questions/Q.20098019.html
https://www.experts-exchange.com/questions/Q.20190409.html
https://www.experts-exchange.com/questions/Q.20203846.html
https://www.experts-exchange.com/questions/Q.20251787.html
https://www.experts-exchange.com/questions/Q.20260234.html
https://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.
Avatar of boatful

ASKER

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!