COleVariant vTrue((short)TRUE),
vFalse((short)FALSE),
vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
//开始一个Microsoft Word实例
if (!oWordApp.CreateDispatch("Word.Application"))
{
AfxMessageBox("CreateDispatch failed.", MB_OK | MB_SETFOREGROUND);
return;
}
//建立一个新的文档
Documents oDocs;
_Document oDoc;
oDocs = oWordApp.GetDocuments();
oDoc = oDocs.Open(COleVariant("d:\\demo.doc"),
COleVariant((short)0),
vFalse, vFalse,COleVariant(""), COleVariant(""),vTrue, COleVariant(""),
COleVariant(""),vFalse, vFalse, vTrue);
PageSetup pageset = oDoc.GetPageSetup();
long lPage = ((Range)oDoc.GetContent()).GetInformation(4).lVal;
for(int i=1;i<=lPage;i++)
{
try{
Shapes shapes = oDoc.GetShapes();
Range page = oDoc.Range(COleVariant((long)0),COleVariant((long)0));
page = page.GoTo(COleVariant((long)1),vOpt,COleVariant((long)i),vOpt);
page = page.GoTo(COleVariant((long)-1),vOpt,vOpt,COleVariant("\\Page"));
VARIANT varRange;
::VariantInit(&varRange);
varRange.vt = VT_DISPATCH;
varRange.pdispVal= page.m_lpDispatch;
Shape shape = shapes.AddPicture("C:\\Documents and Settings\\Administrator\\My Documents\\My Pictures\\1.bmp",vFalse,vTrue,COleVariant((float)0),COleVariant((float)0),/*&page.GetInformation(wdHorizontalPositionRelativeToPage),&page.GetInformation(wdVerticalPositionRelativeToPage,*/ COleVariant((float)pageset.GetPageWidth()),COleVariant((float)pageset.GetPageHeight()),&varRange);
shape.SetRelativeHorizontalPosition(1);
shape.SetRelativeVerticalPosition(1);
shape.SetLeft((float)0);
shape.SetTop((float)0);
WrapFormat wrapFormat = shape.GetWrapFormat();
wrapFormat.SetSide(0);
wrapFormat.SetType(3);
shape.ZOrder(5);
PictureFormat picFormat = shape.GetPictureFormat();
picFormat.SetBrightness(0.5);
picFormat.SetContrast(0.5);
picFormat.SetColorType(1);
FillFormat fillFormat = shape.GetFill();
fillFormat.SetVisible(false);
fillFormat.Solid();
fillFormat.SetTransparency(0);
shape.ScaleHeight(1,true,0);
shape.ScaleWidth(1,true,0);
picFormat.SetTransparentBackground(true);
picFormat.SetTransparencyColor(RGB(255,255,255));
fillFormat.SetVisible(false);
::VariantClear(&varRange);
picFormat.ReleaseDispatch();
fillFormat.ReleaseDispatch();
wrapFormat.ReleaseDispatch();
page.ReleaseDispatch();
shape.ReleaseDispatch();
shapes.ReleaseDispatch();
}catch(COleDispatchException *e)
{
}
}
oWordApp.SetVisible(true);
目的是给每页添加一张图片,但是在FOR循环中会出现释放错误,但是相应的宏在WORD执行正常! |