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
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