?
Solved

replacing text within a selection

Posted on 2004-08-13
8
Medium Priority
?
171 Views
Last Modified: 2010-05-02
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?

0
Comment
Question by:meuedyn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
8 Comments
 

Author Comment

by:meuedyn
ID: 11797374
Points raised, maybe this is harder than I thought it would be?
0
 
LVL 3

Accepted Solution

by:
realrael earned 500 total points
ID: 11798639

hey, have you looked at the VB "Replace" function? - rael

Replace(expression, find, replace[, start[, count[, compare]]])
0
 

Author Comment

by:meuedyn
ID: 11799739
Im sure its something like that;)... how would I replace within a selection?  Can you give an example or point me to one?

-meue
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Assisted Solution

by:TylerTheWimp
TylerTheWimp earned 200 total points
ID: 11801915
I suggest reading the template in on one text stream and writing out to another text stream.  Read each line in from the incoming text stream into a string variable.  Now that you have a line of text in a variable, scan that variable for your template indicator.  If the indicator exists, lookup the value from your database that should replace that template indicator.

sLineVariable = templatefile.readline

sLineVariable = Replace(sLineVariable, sTemplateInidcator, sPulledFromDatabase)

outputfile.writeline sLineVariable



0
 
LVL 3

Assisted Solution

by:realrael
realrael earned 500 total points
ID: 11807062

hi meue, post some code where i'll insert the Replace call and i'll get back to you :) - rael
0
 

Author Comment

by:meuedyn
ID: 11916359
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.Execute Replace:=wdReplaceAll
Next

Set oWord = Nothing
Set oDoc = Nothing

End Sub
0
 

Author Comment

by:meuedyn
ID: 12397503
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
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question