I am modifying a MFC application to convert DOCX files to RTF, so they can be used on an automated Word 2003. To do so, I am using the text converter "Wordcnvpxy.cnv", installed by the Office 2007 Compatibility Pack. I've read the "External Text File Converters SDK" (available here) and went through the samples provided to learn how to call the ForeignToRtf32 function that does exactly what I need. So, I have
The function signature :
typedef long (PASCAL *PFN_RTF)(long, long);
short ForeignToRtf32(HANDLE ghszFile, Istorage* pstgForeign, HANDLE ghBuff, HANDLE ghszClass, HANDLE ghszSubset, PFN_RTF lpfnOut);
A Cstring variable containing the name of the DOCX file (which corresponds to the first parameter of RtfToForeign32)
With this, and being a C++ novice, I have several problems :
For information, the mandatory calls to InitConverter32 and UninitConverter work fine.
OK, so I finally solved my problem. To sum it up, my problem was : "how to handle DOCX documents on an automated Word 2003 application", and I found an easier solution than using the Converter SDK.
What I had to do to get a successful conversion was (copy/paste from the same question I asked in MSDN) :
Here are some excerpts of the code :
OpenDoc( CString inFileName, BOOL tryAgain ) {
m_oDoc = NULL;
FileConverter fc = NULL;
// isDocxFile checks the file signature
BOOL isDocX = isDocxFile(inFileName);
FileConverters fcList = m_oWordApp.GetFileConverters();
if (fcList.GetCount() > 1) {
// Beginning the loop on "0" won't work. The fcList starts at 1.
for (long i=1; i < fcList.GetCount() + 1; i++) {
FileConverter fcTemp = fcList.Item(COleVariant((long) i));
if (fcTemp.GetClassName() == "Word12") {
fc = fcTemp;
}
}
}
if (isDocX) {
m_oDoc = m_oDocs.Open( COleVariant( inFileName ), //FileName
vFalse, //ConfirmConversions
vTrue, //ReadOnly
vFalse, //AddToRecentFiles
vOpt, //PasswordDocument
vOpt, //PasswordTemplate
vFalse, //Revert
vOpt, //WritePasswordDocument
vOpt, //WritePasswordTemplate
COleVariant(fc.GetOpenFormat()), //Format
vOpt, //Encoding
vOpt); //Visible
}
else
{
m_oDoc = m_oDocs.Open( COleVariant( inFileName ), //FileName
vFalse, //ConfirmConversions
vTrue, //ReadOnly
vFalse, //AddToRecentFiles
vOpt, //PasswordDocument
vOpt, //PasswordTemplate
vFalse, //Revert
vOpt, //WritePasswordDocument
vOpt, //WritePasswordTemplate
vOpt, //Format
vOpt, //Encoding
vOpt); //Visible
}
}
With all this done, my document can be rendered correctly, and saved as RTF if need be.