SOL9 2.0 Class: WordPublisher

 SOL9 C++ Class Library  SOL9 Samples  SOL9 Tutorial  SOL9 FAQ  SOL9 ClassTree  SOL9 ClassList 

Source code

/******************************************************************************
 *
 * Copyright (c) 2012 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions, and the following disclaimer.
 *  
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *
 *  WordPublisher.h
 *
 *****************************************************************************/

// class WordPublish can publish a MS-Word docfile as a file of format of 
// 'html', 'mhtml', 'txt', 'rtf', 'xps', 'pdf' or 'odt'.

//2012/05/11
//2012/05/11 The docFile and saveFile parameters to publish method can take a full or relative path.

#include <sol/office/WordApplet.h>
#include <sol/office/WordDocument.h>
#include <sol/office/OfficeFileVerifier.h>

#include <sol/BString.h>
#include <sol/ArgT.h>
#include <sol/Path.h>

namespace SOL {

class WordPublisher :public WordApplet {

public:
  WordPublisher()
  {
    putScreenUpdating(VARIANT_FALSE);

    /*
    enum WdAlertLevel{
      wdAlertsNone = 0,
      wdAlertsMessageBox = -2,
      wdAlertsAll = -1};
    */

    putDisplayAlerts(Word::WdAlertLevel(0));

  }

private:
  bool verifySaveFormat(const TCHAR* saveFormat,
    Word::WdSaveFormat& xformat, StringT<TCHAR>& extension)
  {
    bool rc = false;
    static ArgT<TCHAR> names[] =
    {
      {_T(".html"),  Word::wdFormatHTML},
      {_T(".mhtml"),   Word::wdFormatWebArchive},
      {_T(".txt"),  Word::wdFormatText},
      {_T(".rtf"), Word::wdFormatRTF},
      {_T(".xps"),  Word::wdFormatXPS},
      {_T(".pdf"),  Word::wdFormatPDF},
      {_T(".odt"), Word::wdFormatOpenDocumentText},
    };

    for (int i = 0; i< CountOf(names); i++) {
      StringT<TCHAR> name = names[i].name;
      if (name.endsWithIgnoreCase(saveFormat)) {
        xformat = (Word::WdSaveFormat)names[i].value;
        extension = names[i].name;
        rc = true;
        break;
      }
    }
    if (!rc) {
      throw Exception(0, _T("Unsupported saveformat %s"), saveFormat);
    }

    return rc;
  }

public:
  bool publish(const TCHAR* docFile, const TCHAR* saveFormat)
  {
    bool rc = false;
    if (docFile == NULL || saveFormat == NULL) {
      throw Exception(0, _T("Exception: Invalid parameter to publish method")); 
    }

    OfficeFileVerifier verifier;
    if ( !(verifier.isWordFileExtension(docFile) &&
         verifier.isOfficeFile(docFile)) ) {
      throw Exception(0, _T("Exception: Invalid Office file '%s'"), docFile ); 
    }

    try {

      StringT<TCHAR> extension;
      Word::WdSaveFormat xformat;

      verifySaveFormat(saveFormat, xformat, extension);

      Path docPath = docFile;
      String docFullPath = docPath.getFullPathName();

      BString docname = (const TCHAR*)docFullPath;

      WordDocument document = open((BSTR)docname, false);
      printf("Opened a document %S\n", (BSTR)docname);

      BString filename = docname + (const TCHAR*)extension;  
      _variant_t newfilename = (BSTR)filename;
      _variant_t format = (short)xformat;

      document.saveAs(&newfilename, &format);
      printf("Saved As %S\n", (BSTR)filename);

      document.close();
      printf("Closed a document\n");
      rc = true;

    } catch (HRESULT hr) {
      printf("Exception %x\n", hr);
    } catch (Exception& ex) {
      ex.printf();
    } catch (...) {
      printf("Unknown Exception \n");
    }
    return rc;
  }

public:
  bool publish(const TCHAR* docFile, const TCHAR* saveFile, const TCHAR* saveFormat)
  {
    bool rc = false;
    if (docFile == NULL || saveFile == NULL || saveFormat == NULL) {
      throw Exception(0, _T("Exception: Invalid parameter to publish method")); 
    }

    OfficeFileVerifier verifier;
    if ( !(verifier.isWordFileExtension(docFile) &&
         verifier.isOfficeFile(docFile)) ) {
      throw Exception(0, _T("Exception: Invalid Office file '%s'"), docFile ); 
    }

    try {

      StringT<TCHAR> extension;
      Word::WdSaveFormat xformat;

      verifySaveFormat(saveFormat, xformat, extension);

      Path docPath = docFile;
      String docFullPath = docPath.getFullPathName();

      Path savePath = saveFile;
      String saveFullPath = savePath.getFullPathName();

      BString docname  = (const TCHAR*)docFullPath;
      BString savename = (const TCHAR*)saveFullPath;

      WordDocument document = open((BSTR)docname, false);
      printf("Opened a document %S\n", (BSTR)docname);
      try {
        BString filename = savename;// + (const TCHAR*)extension;  
        _variant_t newfilename = (BSTR)filename;
        _variant_t format = (short)xformat;

        document.saveAs(&newfilename, &format);
        printf("Saved As %S\n", (BSTR)filename);
        rc = true;
      } catch (...) {
        //printf("SaveAs failed\n");
        //Catch an IException from saveAs method.
      }
      //Close the opened document anyway, even if document.saveAs method failed
      document.close();
      printf("Closed a document\n");

    } catch (HRESULT hr) {
      printf("Exception %x\n", hr);
    } catch (Exception& ex) {
      ex.printf();
    } catch (...) {
      printf("Unknown Exception \n");
    }
    return rc;
  }

};

}


Last modified: 5 May 2019

Copyright (c) 2009-2019 Antillia.com ALL RIGHTS RESERVED.