Find/Replace Constant Strings in Word Document With Variable Data With VBA

A

Andy

Hi All,

I have a Word document that has strings in it of the form "dd1",
"dd2", "dd3", "dd4", etc. each of which represents locations where
variable data will be inserted into the document. Some of these
strings are in Word table cells and some are in Word paragraphs. What
I would like to do is within VB.Net replace all of the above mentioned
strings with actual data values from a .Net dataset. So basically I
want to traverse the Word document from beginning to end only ONCE and
everytime I find a string of the form "dd1", "dd2", etc. as mentioned
above I want to replace it with a value from a .Net dataset. So at
the same time that I'm looping through the Word document I'm looping
through my .Net dataset as well. So it's something like this . . .

1. Start at beginning of document.
2. Search for 1st string like "dd1", "dd2" etc.
3. When found replace it with an item in the first row of the dataset
4. Find the next string like "dd1", "dd2" etc.
5. When found replace it with an item in the next row of the dataset
and so on …

I would like to use VBA in .Net to do this and I was wondering if
anyone out there could help get me pointed in the right direction.

Thanks in advance,

Andy
 
A

Andy

Jay Freedman said:
Hi, Andy,

The best way to do this job is with a wildcard search
(http://www.mvps.org/word/FAQs/General/UsingWildcards.htm) in a loop,
incrementing the dataset row each time a match is found. When .Execute
returns True, the range covers the found text, so assigning a string to its
.Text property replaces the found text with the string. Your VB.Net
procedure should go something like this:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim oWord As Word._Application
Dim oDoc As Word.Document
Dim oRg As Word.Range
Dim nDataRow As Integer
Dim strData As String
Const wdCollapseEnd = 0

nDataRow = 1

oWord = CreateObject("Word.Application")
oWord.Visible = True
oDoc = oWord.Documents.Open("c:\temp\test_ddn.doc")
oRg = oDoc.Range
With oRg.Find
.MatchWildcards = True
.Text = "(dd)([0-9]{1,})"
.Format = False
.Forward = True
.Wrap = Word.WdFindWrap.wdFindStop
Do While .Execute
' retrieve the data for this row
' by whatever method you use,
' strData = function of nDataRow
' then...
oRg.Text = strData
nDataRow = nDataRow + 1
oRg.Collapse(Direction:=wdCollapseEnd)
Loop
End With
End Sub



--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://www.mvps.org/word
Hi All,

I have a Word document that has strings in it of the form "dd1",
"dd2", "dd3", "dd4", etc. each of which represents locations where
variable data will be inserted into the document. Some of these
strings are in Word table cells and some are in Word paragraphs. What
I would like to do is within VB.Net replace all of the above mentioned
strings with actual data values from a .Net dataset. So basically I
want to traverse the Word document from beginning to end only ONCE and
everytime I find a string of the form "dd1", "dd2", etc. as mentioned
above I want to replace it with a value from a .Net dataset. So at
the same time that I'm looping through the Word document I'm looping
through my .Net dataset as well. So it's something like this . . .

1. Start at beginning of document.
2. Search for 1st string like "dd1", "dd2" etc.
3. When found replace it with an item in the first row of the dataset
4. Find the next string like "dd1", "dd2" etc.
5. When found replace it with an item in the next row of the dataset
and so on .

I would like to use VBA in .Net to do this and I was wondering if
anyone out there could help get me pointed in the right direction.

Thanks in advance,

Andy

Well Jay you've come through for me again!!! All I can say is thanks
once again for your help. Always greatly appreciated ...

Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top