meuedyn
asked on
replacing text within a selection
Howdy Experts,
I already have code that selects text in template word doc based on querys in access that correspond to bookmarks in the word doc. Once the selection is highlighed, I would like to, replace certain words with the values of text fields from access.
Setting up a recordset, pulling the values in, etc, is all stuff I have enough working knowledge to do... what I need help with is the simple find/replace within a selection command... how can I do this? Do I have to define a range object? or if the selection is always going to be a bookmark, could i find/replace within the bookmark object pretty easily?
I already have code that selects text in template word doc based on querys in access that correspond to bookmarks in the word doc. Once the selection is highlighed, I would like to, replace certain words with the values of text fields from access.
Setting up a recordset, pulling the values in, etc, is all stuff I have enough working knowledge to do... what I need help with is the simple find/replace within a selection command... how can I do this? Do I have to define a range object? or if the selection is always going to be a bookmark, could i find/replace within the bookmark object pretty easily?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Im sure its something like that;)... how would I replace within a selection? Can you give an example or point me to one?
-meue
-meue
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Well, I kind of answered this on myself by running a macro in word and then trying to figure out the code that it created.. so here is my replace subroutine.. it goes through all the fields in a recordset except for the first one uses the fieldname as the search string (encased in "<" and ">") and then replaces it with the value of the field. I guess what I Was really looking for was the oWord.Selection.Find section... This code assumes that the selection was selected before it sent the recordset to the subroutine. oh yeah, oWord is the word instance and oDoc is the document instance. I think thats all thats needed to understand the code.
Public Sub FieldReplace(repRS as recordset)
Dim strFind As String
Dim i As Integer
For i = 2 To repRS.Fields.Count
strFind = "<" & repRS.Fields.Item(i - 1).Name & ">"
With oWord.Selection.Find
.Text = strFind
.Replacement.Text = repRS.Fields.Item(i - 1)
.Wrap = wdFindStop
End With
oWord.Selection.Find.Execu te Replace:=wdReplaceAll
Next
Set oWord = Nothing
Set oDoc = Nothing
End Sub
Public Sub FieldReplace(repRS as recordset)
Dim strFind As String
Dim i As Integer
For i = 2 To repRS.Fields.Count
strFind = "<" & repRS.Fields.Item(i - 1).Name & ">"
With oWord.Selection.Find
.Text = strFind
.Replacement.Text = repRS.Fields.Item(i - 1)
.Wrap = wdFindStop
End With
oWord.Selection.Find.Execu
Next
Set oWord = Nothing
Set oDoc = Nothing
End Sub
ASKER
hello,
Actually, I think that my own answer is the correct one. I could get a refund, but since the answer is valid, I thought it would be better to have it around in the database. I dont think there is any reason to give me negative feedback for this! I was just waiting to see how it could be handled. Since it seems that you dont have any way of handling this, Ill just split the points and offer everyone a freebie;).
-meue
Actually, I think that my own answer is the correct one. I could get a refund, but since the answer is valid, I thought it would be better to have it around in the database. I dont think there is any reason to give me negative feedback for this! I was just waiting to see how it could be handled. Since it seems that you dont have any way of handling this, Ill just split the points and offer everyone a freebie;).
-meue
ASKER