none
Problem automated word SaveAs functionality RRS feed

  • Question

  • I am using the following code to read document file and save it as PDF. When we use the ExportAsFixedFormat function it works fine with windows 2007. But when i try touse SaveAs function exception is being raised without any clear description (Example: "T", "U"..) which are not clear why they are getting raised. Below is the block of code written in C++ to accomplish this task, any further assistance on this is appreciated.

    DWORD AutomateWordByImport()

    {

        CoInitialize(NULL);

        try

        {

            Word::_ApplicationPtr spWordApp;

            HRESULT hr = spWordApp.CreateInstance(__uuidof(Word::Application));

            if (FAILED(hr))

            {

                printf("CreateInstance failed w/err 0x%08lx\n", hr);

                return 1;

            }

            printf("Word.Application is started\n");

     

            spWordApp->Visible = VARIANT_FALSE;

            Word::DocumentsPtr documents = spWordApp->Documents;

            Word::_DocumentPtr document = documents->Open(&_variant_t("E:\\a.docx") );

            if ( document==NULL )

            {

                printf("Document could not be opened\n");

                return 0;

            }

            Word::RangePtr headers = document->Sections->First->Headers->Item(Word::WdHeaderFooterIndex::wdHeaderFooterPrimary)->Range;

            headers->Delete();

            Word::RangePtr footers = document->Sections->First->Footers->Item(Word::WdHeaderFooterIndex::wdHeaderFooterPrimary)->Range;

            footers->Delete();

            long pageCount = document->ComputeStatistics(Word::wdStatisticPages);

            _bstr_t outputFile = "C:\\Users\\Rajitha\\Desktop\\2.pdf";

            //document->SaveAs(&_variant_t("E:\\1.pdf"), &_variant_t(Word::WdExportFormat::wdExportFormatPDF) );

            document->ExportAsFixedFormat(outputFile, Word::WdExportFormat::wdExportFormatPDF, 0,

                                            Word::WdExportOptimizeFor::wdExportOptimizeForOnScreen,

                                            Word::WdExportRange::wdExportAllDocument, 1, pageCount,

                                            Word::WdExportItem::wdExportDocumentWithMarkup, 1,1,

                                            Word::WdExportCreateBookmarks::wdExportCreateNoBookmarks,0,0,0);

            printf("Quit the Word application\n");

            document->Save();

            document->Close(&_variant_t(Word::wdSaveChanges));

            spWordApp->Quit();

        }

        catch (_com_error &err)

        {

            printf("Word throws the error: %s\n", err.ErrorMessage());

            printf("Description: %s\n", (LPCSTR) err.Description());

        }

        CoUninitialize();

        return 0;

    }

     


    saikalyan

    Tuesday, September 25, 2012 7:58 AM

Answers

  • Hello Saikalyan,

    Word's SaveAs() method expects a Variant pointer as its arguments. When you pass &_variant_t(Word::WdSaveFormat::wdFormatPDF), it interprets the argument as VT_INT type which is an unexpected type for Word SaveAs method. Hence, it is throwing an exception while we pass the FileFormat argument.

    You should set the type to VT_I4 to fix this issue. Modify the FileFormat argument value as mentioned below:

    VARIANT fileFormat;
    fileFormat.vt = VT_I4;
    fileFormat.intVal = Word::WdSaveFormat::wdFormatPDF;
    document->SaveAs(&_variant_t(outputFile), &fileFormat);

    Also, note that SaveAs method is deprecated and you should use SaveAs2() method instead. The code will be as given below:

    VARIANT fileFormat;
    fileFormat.vt = VT_I4;
    fileFormat.intVal = Word::WdSaveFormat::wdFormatPDF;
    document->SaveAs2(&_variant_t(outputFile), &fileFormat);

    Thanks,

    Sreerenj G Nair

    • Marked as answer by SAI KALYAN Tuesday, October 16, 2012 4:12 AM
    Friday, October 5, 2012 9:42 AM

All replies

  • The WdExportFormat enumeration is not a valid value for the SaveAs method. SaveAs requires a member of the WdSaveFormat enumeration. This has a member wdFormatPDF, but it will not work with all releases of Office 2007.

    The original release (with no Service Packs) was not allowed to incorporate a file converter, so used an Add-in (ExportToFixedFormat method) for saving Word documents as PDF. Later, Microsoft and Adobe reached an agreement and the PDF converter could be incorporated. From that point on (I think it was SP1) Office applications could SaveAs PDF.


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, September 25, 2012 8:12 AM
    Moderator
  • Below is the statement I used in automated word sample:

    document->SaveAs(&_variant_t(outputFile), &_variant_t(Word::WdSaveFormat::wdFormatPDF));

     The exception raised is:

    Word throws the error: T

    Description: (null)

    I have tried even in Office 2010 getting the same error.


    saikalyan

    Wednesday, September 26, 2012 6:34 AM
  • Can anybody suggest any thing on SaveAs problem

    saikalyan

    Friday, October 5, 2012 3:52 AM
  • Hello Saikalyan,

    Word's SaveAs() method expects a Variant pointer as its arguments. When you pass &_variant_t(Word::WdSaveFormat::wdFormatPDF), it interprets the argument as VT_INT type which is an unexpected type for Word SaveAs method. Hence, it is throwing an exception while we pass the FileFormat argument.

    You should set the type to VT_I4 to fix this issue. Modify the FileFormat argument value as mentioned below:

    VARIANT fileFormat;
    fileFormat.vt = VT_I4;
    fileFormat.intVal = Word::WdSaveFormat::wdFormatPDF;
    document->SaveAs(&_variant_t(outputFile), &fileFormat);

    Also, note that SaveAs method is deprecated and you should use SaveAs2() method instead. The code will be as given below:

    VARIANT fileFormat;
    fileFormat.vt = VT_I4;
    fileFormat.intVal = Word::WdSaveFormat::wdFormatPDF;
    document->SaveAs2(&_variant_t(outputFile), &fileFormat);

    Thanks,

    Sreerenj G Nair

    • Marked as answer by SAI KALYAN Tuesday, October 16, 2012 4:12 AM
    Friday, October 5, 2012 9:42 AM