Question

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 :

  • I don't get how to go from a CString to a HANDLE (i've read numerous pages on it without finding a good solution). I can't use "clr" so the operator ^ isn't an option for me, and whatever I try, the return value is always "-1" (meaning the function couldn't open the input file).
  • I only need parameters 1 (file name), 3 (buffer containing the RTF output) and 6 (chunk of RTF used by the converter). Other parameters will be NULL. So how can I save into a file the buffer passed by the handle ghbuff ?

For information, the mandatory calls to InitConverter32 and UninitConverter work fine.

Was it helpful?

Solution

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

  1. Detect if my document is a DOCX document (the extension is not relevant, I need to check the file signature)
  2. Get the FileConverter object corresponding to the Word 2007 Converter (of course, the Compatibility Pack is required)
  3. Opening the document with the OpenFormat of the FileConverter as an argument in the "Documents.Open" method.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top