automating word

haq2k

Member
Joined
Jun 22, 2005
Messages
7
Programming Experience
3-5
I'm trying to replace occurrences of several fields with something else. For example, in the snippet below, I'm trying to replace <<PI>> with text. I'm doing this by automating Word through VB.NET. This code works everywhere EXCEPT for when it's in a table. My question is, how do you do a find and replace on text that's in a cell?
newDoc.Content.Find.Execute(FindText:="<<PI>>", ReplaceWith:="text", Replace:=Word.WdReplace.wdReplaceAll)

 
It should work that's my opinion as well (even i haven't tried to replace word from cell/s but this works OK for the rest of the text.

Set myRange = ActiveDocument.Content
myRange.Find.Execute(FindText:="hi", _
ReplaceWith:="hello", Replace:=wdReplaceAll)

However let me try to help you ... post some code how you add the tables within

Cheers ;)
 
update

I'm not creating a table manually. I'm copying and pasting. Then I'm doing a find and replace for several values. They all work except the ones in the cells. I attached a document with the table. All the fields with <<...>> need replacing.

Thanks for your help

Dim
newDoc AsNew Word.Document
Dim doc1 As Word.Document

Dim currentDate AsString = Now.ToString("MM\dd\yy")

Dim tmpDate AsDate

doc1 = WordApp.Documents.Open("c:\Transfer Letter.doc")

WordApp.Visible =
False

doc1.Activate()

WordApp.Selection.WholeStory()

WordApp.Selection.Copy()

newDoc.Activate()

WordApp.Selection.Paste()

While newDoc.Content.Find.Execute(FindText:="<<PI>>", ReplaceWith:="PI", Replace:=Word.WdReplace.wdReplaceAll, wrap:=Word.WdFindWrap.wdFindContinue)

EndWhile

newDoc.Content.Find.Execute(FindText:="<<CURRENT DATE>>", ReplaceWith:=currentDate, Replace:=Word.WdReplace.wdReplaceAll)

newDoc.Content.Find.Execute(FindText:="<<BORROWERNAME>>", ReplaceWith:=strValues(0, myEnum.BorrowerFirst) & " " & strValues(0, myEnum.BorrowerLast), Replace:=Word.WdReplace.wdReplaceAll)

newDoc.Content.Find.Execute(FindText:="<<MAILING STREET>>", ReplaceWith:=strValues(0, myEnum.MailingStreet), Replace:=Word.WdReplace.wdReplaceAll)

newDoc.Content.Find.Execute(FindText:="<<MAILING ADDRESS>>", ReplaceWith:=strValues(0, myEnum.MailingCity) & ", " & strValues(0, myEnum.MailingState) & " " & strValues(0, myEnum.MailingZip), Replace:=Word.WdReplace.wdReplaceAll)

newDoc.Content.Find.Execute(FindText:="<<LN>>", ReplaceWith:=strValues(0, myEnum.LoanNum), Replace:=Word.WdReplace.wdReplaceAll)

tmpDate =
CDate(strValues(0, myEnum.Due))

newDoc.Content.Find.Execute(FindText:="<<DUEDATE>>", ReplaceWith:=tmpDate.ToString("MMMM, dd yyyy"), Replace:=Word.WdReplace.wdReplaceAll)

'problem area
---------------------------------------------------------
newDoc.Content.Find.Execute(FindText:="<<PMI>>", ReplaceWith:="text", Replace:=Word.WdReplace.wdReplaceAll)
--------------------------------------------------------

newDoc.Content.Find.Execute(FindText:="<<MMIPMI>>", ReplaceWith:=strValues(0, myEnum.MMI_PMI), Replace:=Word.WdReplace.wdReplaceAll)
newDoc.Content.Find.Execute(FindText:="<<BUYDOWN>>", ReplaceWith:=strValues(0, myEnum.Buydown), Replace:=Word.WdReplace.wdReplaceAll)

newDoc.Content.Find.Execute(FindText:="<<TOTAL>>", ReplaceWith:=strValues(0, myEnum.total), Replace:=Word.WdReplace.wdReplaceAll)

newDoc.Content.Find.Execute(FindText:="<<INVLOAN>>", ReplaceWith:=strValues(0, myEnum.InvLoanNum), Replace:=Word.WdReplace.wdReplaceAll)



WordApp.ActiveDocument.SaveAs("c:\New Transfer Letter.doc")

WordApp.Documents.Close(Word.WdSaveOptions.wdDoNotSaveChanges)

WordApp.Quit()

WordApp =
Nothing

 

Attachments

  • Transfer Letter.doc
    45.5 KB · Views: 59
Last edited:
Back
Top