3,248
社区成员




COleVariant varFilePath(szTemplate);
COleVariant covTrue((short)TRUE);
COleVariant covFalse((short)FALSE);
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
if(!InitAppInstance())
{
myApp->Quit(covFalse,covTrue,covFalse);
return FALSE;
}
spDocs = myApp->GetDocuments();//获得文档对象
//打开模版文档
spDocs->Open(varFilePath,covFalse,covFalse,covFalse,
covOptional,covOptional,covFalse,covOptional,
covOptional,COleVariant(long(1)),covOptional,
covTrue,covOptional,covOptional,covOptional);
inline Word::_DocumentPtr Word::Documents::Open ( VARIANT * FileName, VARIANT * ConfirmConversions, VARIANT * ReadOnly, VARIANT * AddToRecentFiles, VARIANT * PasswordDocument, VARIANT * PasswordTemplate, VARIANT * Revert, VARIANT * WritePasswordDocument, VARIANT * WritePasswordTemplate, VARIANT * Format, VARIANT * Encoding, VARIANT * Visible, VARIANT * OpenAndRepair, VARIANT * DocumentDirection, VARIANT * NoEncodingDialog, VARIANT * XMLTransform ) {
struct _Document * _result = 0;
HRESULT _hr = raw_Open(FileName, ConfirmConversions, ReadOnly, AddToRecentFiles, PasswordDocument, PasswordTemplate, Revert, WritePasswordDocument, WritePasswordTemplate, Format, Encoding, Visible, OpenAndRepair, DocumentDirection, NoEncodingDialog, XMLTransform, &_result);
if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
return _DocumentPtr(_result, false);
}
#import "C:\Program Files\Common Files\Microsoft Shared\OFFICE14\MSO.DLL" rename("RGB", "msRGB")
#import "C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB"
#import "C:\Program Files\Microsoft Office\Office14\MSWORD.OLB" \
rename("FindText", "msFindText")\
rename("ExitWindows","msExitWindows")
void DumpComError(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
TCHAR szBuff[1024] = {0};
_stprintf_s(szBuff,
_T("Oops - hit an error!\n")
_T("\a\tCode = %08lx\n")
_T("\a\tCode meaning = %s\n")
_T("\a\tSource = %s\n")
_T("\a\tDescription = %s\n"),
e.Error(),
e.ErrorMessage(),
(LPCTSTR) bstrSource,
(LPCTSTR) bstrDescription );
TRACE(szBuff);
AfxMessageBox(szBuff);
ASSERT(0);
}
void TestWord()
{
using namespace Office;
using namespace Word;
_ApplicationPtr pApp;
_DocumentPtr pDoc;
try
{
do
{
if(pApp.CreateInstance(L"Word.Application") != S_OK)
break;
pApp->Visible = true;
//pDoc = pApp->GetDocuments()->Add();
_variant_t vtFile("C:\\temp\\Test.docx");
pDoc = pApp->GetDocuments()->Open( &vtFile );
{
Word::LineFormatPtr pLine = pDoc->Shapes->AddLine(100, 100, 100, 200)->Line; //画线
pLine->PutStyle(msoLineSingle); //Selection.ShapeRange.Line.Style = msoLineSingle //单根线
pLine->PutWeight(6); //Selection.ShapeRange.Line.Weight = 6# //6磅
pLine->PutDashStyle(msoLineLongDash); //Selection.ShapeRange.Line.DashStyle = msoLineLongDash //点画线
pLine->ForeColor->PutRGB( RGB(51, 102, 255) ); // Selection.ShapeRange.Line.ForeColor.RGB = RGB(51, 102, 255) //线颜色
//两段箭头
pLine->put_BeginArrowheadStyle(msoArrowheadTriangle); //Selection.ShapeRange.Line.BeginArrowheadStyle = msoArrowheadTriangle
pLine->put_EndArrowheadStyle(msoArrowheadTriangle); // Selection.ShapeRange.Line.EndArrowheadStyle = msoArrowheadTriangle
pLine->put_BeginArrowheadWidth(msoArrowheadWidthMedium); //Selection.ShapeRange.Line.BeginArrowheadWidth = msoArrowheadWidthMedium
pLine->put_BeginArrowheadLength(msoArrowheadLengthMedium); //Selection.ShapeRange.Line.BeginArrowheadLength = msoArrowheadLengthMedium
//可见
pLine->PutVisible(msoTrue); //Selection.ShapeRange.Line.Visible = msoTrue
}
Sleep(1000);
pDoc->Save();
pDoc->Close();
pApp->Quit();
}while(0);
}
catch(_com_error &e)
{
DumpComError(e);
}
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
OleInitialize(NULL);
TestWord();
OleUninitialize();
return 0;
}
spsel->WholeStory();//全选
spsel->MoveLeft(COleVariant((short)1),COleVariant((short)1),COleVariant((short)0));
//spsel->Copy();//复制
//移动到行末
COleVariant ftext(L"<THEPOCODE>");
COleVariant covTrue((short)TRUE);
COleVariant covFalse((short)FALSE);
COleVariant cov2((short)2);
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
COleVariant Rptext(L"SN_TESTINFORMATION");
pFinds = spsel->GetFind();
pReplaced = pFinds->GetReplacement();
pFinds->ClearFormatting();
pReplaced->ClearFormatting();
BOOL flag = TRUE;
while(flag)
{
flag = pFinds->Execute(
&ftext,
covTrue,
covTrue,
covFalse,
covFalse,
covFalse,
covTrue,
covFalse,//wdFindStop,
covFalse,
&Rptext,
cov2,
covFalse,
covFalse,
covFalse,
covFalse);
}
如果用open上述功能就正常,但是open不能打开docx文件。。。