void TestSnapshotSaver::TestSavesCustomProperties()
{
    const CStdString sDocWithNoProperties = GET_TEST_FILE_PATH( _T("No Properties.doc") );
    const CStdString sCopyOfDoc = GET_TEST_TEMP_PATH( _T("CopyFile.doc") );
    CStdString sDestFile = GET_TEST_TEMP_PATH( _T("ResultsFile.doc") );

    ::DeleteFile( sCopyOfDoc.c_str() );
    ::DeleteFile( sDestFile.c_str() );
    assertTest( ::CopyFile( sDocWithNoProperties.c_str(), sCopyOfDoc.c_str(), FALSE ) );

    CustomPropertyStorage propStorage( sCopyOfDoc );
    propStorage.SetProperty( _T("Safe"), _T("Init") );

    Word::_ApplicationPtr pApp;
    pApp.CreateInstance( __uuidof( Word::Application ) );

    try
    {
        const Word::_DocumentPtr pDocument = pApp->Documents->Open( &_variant_t( sCopyOfDoc ) );

        SnapshotSaver saver( pDocument );
        saver.Save( sDestFile );

        CustomPropertyStorage destStorage( sDestFile );
        assertMessage( destStorage.GetProperty( _T("Safe") ) == CStdString( _T("Init") ), _T("Custom properties should be persisted in the snapshotted document") );
        pApp->Quit();
    }
    catch(...)
    {
        pApp->Quit();
        throw;
    }
}
void TestSnapshotSaver::TestSave()
{
    const CStdString sSourceFileName = CTestUtils::GetTestFileFolder() + _T("TestApplyChange_BinaryRead\\Synergy24TestDocuments\\FlagForFollowUp.doc");
    CStdString sDestFileName = CTestUtils::GetTestFileFolder() + _T("TestApplyChange_BinaryRead\\Synergy24TestDocuments\\rubbish.doc");

    ::DeleteFile( sDestFileName );
    assertFileNotExists( sDestFileName );

    Word::_ApplicationPtr pApp;
    pApp.CreateInstance( __uuidof( Word::Application ) );
    const Word::_DocumentPtr pDocument = pApp->Documents->Open( &_variant_t( sSourceFileName ) );

    try
    {
        SnapshotSaver snapshotSaver( pDocument );
        snapshotSaver.Save( sDestFileName );
    }
    catch(...)
    {
        pDocument->Close();
        pApp->Quit();
        throw;
    }

    pDocument->Close();
    pApp->Quit();

    assertFileExists( sDestFileName );
}
void WordOutputProcessor::Close()
{
	_variant_t vtFalse = VARIANT_FALSE;
	Word::_DocumentPtr pDoc = m_WordDoc;
	pDoc->Close(&vtFalse, &vtMissing, &vtFalse);
	m_hWndEditor = NULL;
	Word::_ApplicationPtr pApp = m_WordApp;
	m_WordApp = NULL;
	pApp->Quit();
}
WordVersions WordVersionChecker::GetWordVersion(Word::_ApplicationPtr& pApp)
{
	_bstr_t bsVer = pApp->GetVersion();

	CStdString sVer = bsVer;

	return ConvertNameToVersion(sVer);
}
Exemple #5
0
//---------------------------------------------------------------------------
void WordMacros::RunMacros(string_t tempFile)
{
  Word::_ApplicationPtr app;
  if (FAILED(app.CreateInstance("Word.Application")))
  {
    string_t msg = "Невозможно запустить Microsoft Word. Возможно он не установлен на компьютере.";
    MessageBox(::GetActiveWindow(), msg , L"Ошибка", MB_OK | MB_ICONERROR | MB_APPLMODAL);
    return;
  }
  try
  {
    _variant_t tamp = (const wchar_t*)tempFile;
    Word::_DocumentPtr      doc  = app->Documents->Add(&tamp);
    VBIDE::_VBProjectPtr    vbp  = doc->VBProject;
    VBIDE::_VBComponentsPtr vbc  = vbp->VBComponents;
    VBIDE::_VBComponentPtr  item = vbc->Item(1);
    VBIDE::_CodeModulePtr   cm   = item->CodeModule;

    CreateRuningMacros();
    string_t name = "M" + string_t(aux::itoa(m_MacroCounter));


    cm->AddFromString((const wchar_t*)m_CurMacros);
    app->Run((const wchar_t*)name);
  }
  catch (_com_error & e)
  {
    _bstr_t desc = e.Description();
    string_t msg = "Невозможно открыть документ Microsoft Word:\n" + tempFile + "\n";
    if (desc.length())
      msg += std::wstring(desc);
    MessageBox(::GetActiveWindow(), msg , L"Ошибка", MB_OK | MB_ICONERROR | MB_APPLMODAL); 
  }
  catch (...)
  {
    string_t msg = "Невозможно открыть документ Microsoft Word:\n" + tempFile + "\nили проблема с макросами или не смогли открыть шаблон файла";
    MessageBox(::GetActiveWindow(), msg , L"Ошибка", MB_OK | MB_ICONERROR | MB_APPLMODAL);
  }
  app->Visible = VARIANT_TRUE;

  m_Macros = "";
  m_MacroCounter = 0;
  m_CurMacros = "";
}
void TestRealWordObject::TestGetWordObjectApplication()
{
	CWordCreator wrdCreator;
	CWordObject wrdNewObject(&wrdCreator);
	
	HRESULT hr = wrdNewObject.GetWordObject(false);
	if(FAILED(hr))
	{
		assertTest(_T("Failed to create Word instance")==false);
	}
	
	Word::_ApplicationPtr pApp;	
	pApp = wrdNewObject.GetApplicationPtr();
	assertTest(pApp != NULL);

	BSTR bstrName;
	hr = pApp->get_Name(&bstrName);
	assertTest(SUCCEEDED(hr));
	assertTest(_bstr_t(bstrName) == _bstr_t(_T("Microsoft Word")));
}