// WordOffice.cpp: implementation of the CWordWriter class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "WordWriter.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CWordWriter::CWordWriter()
{
m_bIsNewApp = TRUE;
}
CWordWriter::~CWordWriter()
{
CloseApp();
}
//操作
BOOL CWordWriter::CreateApp()
{
CLSID clsid;
HRESULT hr;
hr=::CLSIDFromProgID(L"Word.Application",&clsid); //通过ProgID取得CLSID
if(FAILED(hr))
{
AfxMessageBox(_T("没有安装OFFICE"));
return FALSE;
}
IUnknown *pUnknown=NULL;
IDispatch *pDispatch=NULL;
hr=::GetActiveObject(clsid,NULL,&pUnknown); //查找是否有WORD程序在运行
if(FAILED(hr)) // 没有正在运行的,则启动新应用
{
if (FALSE == m_wdApp.CreateDispatch("Word.Application"))
{
AfxMessageBox("Application创建失败,请确保安装了word 2000或以上版本!", MB_OK|MB_ICONWARNING);
return FALSE;
}
m_bIsNewApp = TRUE;
return TRUE;
}
// 否则,获得当前程序
try
{
hr=pUnknown->QueryInterface(IID_IDispatch,(LPVOID *)&m_wdApp);
if(FAILED(hr)) throw(_T("没有取得IDispatchPtr"));
pUnknown->Release(); pUnknown=NULL;
m_bIsNewApp = FALSE;
}
catch(LPCTSTR lpErr)
{
AfxMessageBox(lpErr);
}
return TRUE;
}
BOOL CWordWriter::CreateDocuments()
{
if (FALSE == CreateApp())
{
return FALSE;
}
m_wdDocs.AttachDispatch(m_wdApp.GetDocuments());
if (!m_wdDocs.m_lpDispatch)
{
AfxMessageBox("Documents创建失败!", MB_OK|MB_ICONWARNING);
return FALSE;
}
return TRUE;
}
BOOL CWordWriter::InitVariabels()
{
COleVariant vOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
//得到selection变量
m_wdSel = m_wdApp.GetSelection();
if (!m_wdSel.m_lpDispatch)
{
AfxMessageBox("Select获取失败!", MB_OK|MB_ICONWARNING);
return FALSE;
}
//得到全部DOC的Range变量
m_wdRange = m_wdDoc.Range(vOptional,vOptional);
if(!m_wdRange.m_lpDispatch)
{
AfxMessageBox("Range获取失败!", MB_OK|MB_ICONWARNING);
return FALSE;
}
m_wdTable = m_wdDoc.GetTables();
if(!m_wdTable.m_lpDispatch)
{
AfxMessageBox("Table获取失败!", MB_OK|MB_ICONWARNING);
return FALSE;
}
return TRUE;
}
BOOL CWordWriter::CreateDocument()
{
if (!m_wdDocs.m_lpDispatch)
{
AfxMessageBox("Documents为空!", MB_OK|MB_ICONWARNING);
return FALSE;
}
COleVariant varTrue(short(1),VT_BOOL),vOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
CComVariant Template(_T("")); //没有使用WORD的文档模板
CComVariant NewTemplate(false),DocumentType(0),Visible(true);
m_wdDocs.Add(&Template,&NewTemplate,&DocumentType,&Visible);
//得到document变量
m_wdDoc = m_wdApp.GetActiveDocument();
if (!m_wdDoc.m_lpDispatch)
{
AfxMessageBox("Document获取失败!", MB_OK|MB_ICONWARNING);
return FALSE;
}
InitVariabels();
return TRUE;
}
BOOL CWordWriter::Create()
{
if (FALSE == CreateDocuments())
{
return FALSE;
}
return CreateDocument();
}
void CWordWriter::ShowApp()
{
m_wdApp.SetVisible(TRUE);
}
void CWordWriter::HideApp()
{
m_wdApp.SetVisible(FALSE);
}
BOOL CWordWriter::OpenDocument(CString fileName)
{
if (!m_wdDocs.m_lpDispatch)
{
AfxMessageBox("Documents为空!", MB_OK|MB_ICONWARNING);
return FALSE;
}
COleVariant vTrue((short)TRUE),
vFalse((short)FALSE),
vOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR),
vZ((short)0);
COleVariant vFileName(_T(fileName));
//得到document变量
m_wdDoc.AttachDispatch(m_wdDocs.Open(
vFileName, // FileName
vTrue, // Confirm Conversion.
vFalse, // ReadOnly.
vFalse, // AddToRecentFiles.
vOptional, // PasswordDocument.
vOptional, // PasswordTemplate.
vOptional, // Revert.
vOptional, // WritePasswordDocument.
vOptional, // WritePasswordTemplate.
vOptional, // Format. // Last argument for Word 97
vOptional, // Encoding // New for Word 2000/2002
vOptional, // Visible
vOptional, // OpenAndRepair
vZ, // DocumentDirection wdDocumentDirection LeftToRight
vOptional, // NoEncodingDialog
vOptional
) // Close Open parameters
); // Close AttachDispatch
if (!m_wdDoc.m_lpDispatch)
{
AfxMessageBox("Document获取失败!", MB_OK|MB_ICONWARNING);
return FALSE;
}
InitVariabels();
return TRUE;
}
BOOL CWordWriter::Open(CString fileName)
{
if (FALSE == CreateDocuments())
{
return FALSE;
}
return OpenDocument(fileName);
}
BOOL CWordWriter::SetActiveDocument(short i)
{
COleVariant vIndex(_T(i)),vOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
m_wdDoc.AttachDispatch(m_wdDocs.Item(vIndex));
m_wdDoc.Activate();
if (!m_wdDoc.m_lpDispatch)
{
AfxMessageBox("Document获取失败!", MB_OK|MB_ICONWARNING);
return FALSE;
}
InitVariabels();
HideApp();
return TRUE;
}
BOOL CWordWriter::SaveDocument()
{
if (!m_wdDoc.m_lpDispatch)
{
AfxMessageBox("Document获取失败!", MB_OK|MB_ICONWARNING);
return FALSE;
}
m_wdDoc.Save();
return TRUE;
}
BOOL CWordWriter::SaveDocumentAs(CString fileName)
{
if (!m_wdDoc.m_lpDispatch)
{
AfxMessageBox("Document获取失败!", MB_OK|MB_ICONWARNING);
return FALSE;
}
COleVariant vTrue((short)TRUE),
vFalse((short)FALSE),
vOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
COleVariant vFileName(_T(fileName));
m_wdDoc.SaveAs(
vFileName, //VARIANT* FileName
vOptional, //VARIANT* FileFormat
vOptional, //VARIANT* LockComments
vOptional, //VARIANT* Password
vOptional, //VARIANT* AddToRecentFiles
vOptional, //VARIANT* WritePassword
vOptional, //VARIANT* ReadOnlyRecommended
vOptional, //VARIANT* EmbedTrueTypeFonts
vOptional, //VARIANT* SaveNativePictureFormat
vOptional, //VARIANT* SaveFormsData
vOptional, //VARIANT* SaveAsAOCELetter
vOptional, //VARIANT* Encoding
vOptional, //VARIANT* InsertLineBreaks
vOptional, //VARIANT* AllowSubstitutions
vOptional, //VARIANT* LineEnding
vOptional //VARIANT* AddBiDiMarks
);
return TRUE;
}
BOOL CWordWriter::CloseDocument()
{
COleVariant vTrue((short)TRUE),
vFalse((short)FALSE),
vOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
m_wdDoc.Close(vFalse, // SaveChanges.
vTrue, // OriginalFormat.
vFalse // RouteDocument.
);
m_wdDoc.AttachDispatch(m_wdApp.GetActiveDocume
- 1
- 2
- 3
- 4
- 5
前往页