Board index » cppbuilder » Find and replace text in Words

Find and replace text in Words


2004-08-16 04:02:45 PM
cppbuilder48
I would like to find and replace textstrings in a words document using C++ Builder version 6.
Connect and disconnect to the WordsApplication component as well as connect and disconnect to the WordDocument works fine.
Also the following is ok:
WordApplication->Selection->TypeText(StringToOleStr("newCompany"));
WordApplication->Selection->Find->Forward = true;
But, if I try the following, I got the error messages mentioned there
WordApplication->Selection->Find->Replacement->Text=StringToOleStr("newCompanyName");
WordApplication->Selection->Find->Text = StringToOleStr("CompanyName");
[C++ Error] Unit1.cpp(115): E2247 'Find::Text' is not accessible
OleVariant replace = 1;
WordDocument->Range(EmptyParam, EmptyParam)->Find->Execute(StringToOleStr("CompanyName"),\
EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,\
EmptyParam, StringToOleStr("newCompany"), replace);
[C++ Error] Unit1.cpp(112): E2285 Could not find a match for 'Find::Execute(wchar_t *,OleVariant,OleVariant,OleVariant,OleVariant,OleVariant,OleVariant,OleVariant,OleVariant,wchar_t *,OleVariant)'
What is wrong here or is there a better method to replace text strings?
Thanks in advance
Bernd
 
 

Re:Find and replace text in Words

Hi Bernd,
find below a method which i use to replace text. It works with BCB6 and
Word97 components. Should also work
with the W2k components.
void TWordActions::ReplaceText(AnsiString findString, AnsiString
replaceString)
{
SelectionPtr selP= WordAppl->Selection; //WordAppl is a TWordApplication
instance
selP->Start=0;
selP->End=0;
FindPtr findP=selP->Find;
findP->ClearFormatting();
findP->Replacement->ClearFormatting();
findP->Execute(&TVariant(findString), // FindText
TNoParam(), // MatchCase
TNoParam(), // MatchWholeWord
TNoParam(), // MatchWildcards
TNoParam(), // MatchSoundsLike
TNoParam(), // MatchAllWordForms
TNoParam(), // Forward
TNoParam(), // Wrap
TNoParam(), // Format
&TVariant(replaceString), // ReplaceWith
&TVariant(wdReplaceAll)) ;
}
You may also take a look at the free Turbopower OfficePartner components at
sourceforge.net/projects/tpofficepartner/
They are not perfect but quite useful for common Word and Excel tasks
Regards,
Bassem