MS Word-search & replace certain strings in header,footer,textbox(

A

Alex Bremo

Hi guys,

I’m trying to establish a connection to ms word with the task to search and
replace a certain string. Everything is fine so far, unfortunately my code
isn’t able to replace the string in the header, footer or in a textbox. I
found some code for VBA, but I’m looking for C++. (See VBA-Code:
http://word.mvps.org/faqs/customization/ReplaceAnywhere.htm)
Any ideas?

Enclosed my code in C++ (MFC) so far:

COleVariant vTrue((short)TRUE),
vFalse((short)FALSE),
vBlank = COleVariant(""),
vZero = COleVariant((short)0),
vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
_Application oWordApp;


if (!oWordApp.CreateDispatch("Word.Application", NULL))
{
AfxMessageBox("CreateDispatch failed.", MB_OK | MB_SETFOREGROUND);
return;
}

//Create a new document
Documents oDocs;
_Document oDoc;
oDocs = oWordApp.GetDocuments();
//oDoc = oDocs.Add(vOpt, vOpt);

COleVariant FileName = "c:\\test.doc";

oDocs.Open( FileName,
vFalse, // ConfirmConversions
vFalse, // ReadOnly
vFalse, // AddToRecentFiles,
vBlank, // PasswordDocument,
vBlank, // PasswordTemplate,
vFalse, // Revert,
vBlank, // WritePasswordDocument,
vBlank, // WritePasswordTemplate
vZero // Format = wdOpenFormatAuto = 0
);

//Add text to the document
Selection oSel;
oSel = oWordApp.GetSelection();

Find oFind;
oFind = oSel.GetFind();

oFind.ClearFormatting();

while(oFind.Execute(
COleVariant("Test"), // FindText
vTrue, // MatchCase
vFalse, // MatchWholeWord
vFalse, // MatchWildcards
vFalse, // MatchSoundsLike
vFalse, // MatchAllWordForms
vTrue, // Forward :
vTrue, // Wrap
// wdFindAsk :
// wdFindContinue
// wdFindStop
vFalse, // Format
COleVariant("NEW"), // ReplaceWith
vTrue // Replace
// wdReplaceAll
// wdReplaceNone
// wdReplaceOne
));


//Save the document
_Document oActiveDoc;
oActiveDoc = oWordApp.GetActiveDocument();
oActiveDoc.SaveAs(COleVariant("c:\\test.doc"),
COleVariant((short)0),
vFalse, COleVariant(""), vTrue, COleVariant(""),
vFalse, vFalse, vFalse, vFalse, vFalse);


//Quit the application
oWordApp.Quit(vOpt, vOpt, vOpt);
oWordApp.DetachDispatch();


Thanks a lot,
Alex
 
C

Cindy M.

Hi =?Utf-8?B?QWxleCBCcmVtbw==?=,
I’m trying to establish a connection to ms word with the task to search and
replace a certain string. Everything is fine so far, unfortunately my code
isn’t able to replace the string in the header, footer or in a textbox. I
found some code for VBA, but I’m looking for C++. (See VBA-Code:
http://word.mvps.org/faqs/customization/ReplaceAnywhere.htm)
The VBA code sample provides everything you need, you just have to translate it
into C++. What's so difficult about looping the StoryRanges collection?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 

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

Similar Threads


Top