bool PowerPointWorker08::RemoveFields() { bool retVal = false; try { PowerPoint::SlidesPtr pSlides = m_spPresentation->Slides; for(long lIndexSlides = pSlides->Count; lIndexSlides > 0; lIndexSlides--) { PowerPoint::_SlidePtr pSlide = pSlides->Item(_variant_t(lIndexSlides)); PowerPoint::ShapesPtr pShapes = pSlide->Shapes; for (long lIndexShapes = pShapes->Count; lIndexShapes >0; lIndexShapes--) { PowerPoint::ShapePtr pShape = pShapes->Item(_variant_t(lIndexShapes)); if (IsThinkCellObject(pShape)) continue; if (pShape->Type == msoffice::msoEmbeddedOLEObject || pShape->Type == msoffice::msoLinkedOLEObject) { PowerPoint::ShapeRangePtr pShapeRange = NULL; HRESULT hr = pShape->raw_Ungroup(&pShapeRange); if(hr == E_ACCESSDENIED) continue; // the object does not have an Ungroup option e.g it's a picture retVal = true; if(FAILED(hr)) continue; // no point trying to regroup it try { // Type 7 embedded objects can become a type 6 group and thus require a second ungroup if (1 >= pShapeRange->Count) { pShapeRange = pShapeRange->Ungroup(); } pShapeRange->Group(); } catch(...) { LOG_WS_INFO(_T("Unable to ungroup (or regroup) a shape range")); } } } } return retVal; } catch(const Workshare::Exception&) { throw; } catch(...) { unexpected(); } }
bool PowerPointWorker08::RemoveComments() { bool retVal = false; try { PowerPoint::SlidesPtr spSlides = m_spPresentation->Slides; for(long lIndexSlides = spSlides->Count; lIndexSlides > 0; lIndexSlides--) { PowerPoint::_SlidePtr spSlide = spSlides->Item(_variant_t(lIndexSlides)); PowerPoint::ShapesPtr spShapes = spSlide->Shapes; for(long lIndexShapes = spShapes->Count; lIndexShapes > 0; lIndexShapes--) { PowerPoint::ShapePtr spShape = spShapes->Item(_variant_t(lIndexShapes)); int shapeType = spShape->Type; switch(shapeType) { //For grouped items, recurse case 6: { if (ContainsInkComments(spShape) && RemoveGroupedComments(spShape)) retVal = true; break; } // Ink Annotations is an Office 2003 feature consisting out of Ink [msoffice::msoInk = 22] // and Ink Comments [msoffice::msoInkComment = 23] and both are considered to be Comments. // In Offce XP/2000 Ink Annotaions are classified as Freeform Shapes [msoffice::msoFreeform = 5]. // Protect will not remove Ink Annotations from Office 2003 documents if the client is using // Office XP/2000 as this might lead to the removal of other Freeform Shapes. [Story 136.38] case 4: case 22: case 23: { spShape->Delete(); retVal = true; break; } default: continue; } } } return retVal; } catch(const Workshare::Exception&) { throw; } catch(...) { unexpected(); } }
bool CPPTUtil::SpeakerNotesExist(IDispatch* pX) { if(0 == pX) throw Workshare::Exception(_T("Invalid dispatch pointer passed in")); PowerPoint::_PresentationPtr spPresentation(pX); for(long index = 1; index <= spPresentation->Slides->Count; ++index) { PowerPoint::SlideRangePtr spNotesPage = spPresentation->Slides->Item(index)->NotesPage; PowerPoint::ShapesPtr spShapes = spNotesPage->Shapes; PowerPoint::ShapeRangePtr spShapesRange = spShapes->Range(); PowerPoint::TextFramePtr spTextFrame = spShapesRange->TextFrame; PowerPoint::TextRangePtr spTextRange = spTextFrame->TextRange; if(spTextRange->Text.length()) return true; } return false; }
bool CPPTUtil::FieldsExist(IDispatch* pX) { if(0 == pX) throw Workshare::Exception(_T("Invalid dispatch pointer passed in")); PowerPoint::_PresentationPtr spPresentation(pX); for(long index = 1; index <= spPresentation->Slides->Count; ++index) { PowerPoint::_SlidePtr pSlide = spPresentation->Slides->Item(_variant_t(index)); PowerPoint::ShapesPtr pShapes = pSlide->Shapes; for (long lIndexShapes = pShapes->Count; lIndexShapes >0; lIndexShapes--) { PowerPoint::ShapePtr pShape = pShapes->Item(_variant_t(lIndexShapes)); if (pShape->Type == msoffice::msoEmbeddedOLEObject || pShape->Type == msoffice::msoLinkedOLEObject) return true; } } return false; }
long CPPTUtil::GetCommentsCount(IDispatch* pX) { if(0 == pX) throw Workshare::Exception(_T("Invalid dispatch pointer passed in")); PowerPoint::_PresentationPtr spPresentation(pX); PowerPoint::SlidesPtr spSlides = spPresentation->Slides; long comments = 0; for(long i = spSlides->Count; i > 0; i--) { PowerPoint::_SlidePtr spSlide = spSlides->Item(_variant_t(i)); PowerPoint::CommentsPtr spComments = spSlide->Comments; comments += spComments->Count; PowerPoint::ShapesPtr spShapes = spSlide->Shapes; for(long j = spShapes->Count; j > 0; j--) { PowerPoint::ShapePtr spShape = spShapes->Item(_variant_t(j)); int shapeType = spShape->Type; switch(shapeType) { case 4: // msoffice::msoComment case 22: // msoffice::msoInk case 23: // msoffice::msoInkComment { ++comments; break; } default: continue; } } } return comments; }
bool CPPTUtil::CommentsExist(IDispatch* pX) { if (0 == pX) return false; PowerPoint::_PresentationPtr pPresentation(pX); for (long nI = pPresentation->Slides->Count; nI > 0; nI--) { PowerPoint::_SlidePtr spSlide = pPresentation->Slides->Item(_variant_t(nI)); try { if (0 < spSlide->Comments->Count) { return true; } } catch(...) { // Ignore this as it may mean that Comments is not a valid property (which is the // case for PowerPoint 97/2000 } PowerPoint::ShapesPtr spShapes = spSlide->Shapes; for(long lIndexShapes = spShapes->Count; lIndexShapes > 0; lIndexShapes--) { PowerPoint::ShapePtr spShape = spShapes->Item(_variant_t(lIndexShapes)); if (msoffice::msoComment == spShape->Type) { return true; } } } return false; }
// // 函数: AutomatePowerPointByImport(LPVOID) // // 功能: 使用#import指令智能指针的自动化PowerPoint // DWORD WINAPI AutomatePowerPointByImport(LPVOID lpParam) { // 初始化当前线程中的COM库并标识此并发模型为单线程(STA)。 // [-或-] CoInitialize(NULL); // [-或-] CoCreateInstance(NULL); CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); try { ///////////////////////////////////////////////////////////////////// // 使用#import指令智能指针创建PowerPoint.Application COM对象 // // 选项 1) 使用智能指针构造器创建对象 // // _ApplicationPtr是原始接口名_Application和后缀"Ptr"的组合 //PowerPoint::_ApplicationPtr spPpApp( // __uuidof(PowerPoint::Application) // 组件的CLSID // ); // 选项 2) 使用智能指针函数CreateInstance创建对象 PowerPoint::_ApplicationPtr spPpApp; HRESULT hr = spPpApp.CreateInstance(__uuidof(PowerPoint::Application)); if (FAILED(hr)) { wprintf(L"CreateInstance 出错 w/err 0x%08lx\n", hr); return 1; } _putws(L"PowerPoint.Application已经启动"); ///////////////////////////////////////////////////////////////////// // 使PowerPoint不可见(如 Application.Visible = 0) // // PowerPoint默认不可见,直到你设置它为可见: //spPpApp->put_Visible(Office::MsoTriState::msoFalse); ///////////////////////////////////////////////////////////////////// // 创建一个演示文稿 (如 Application.Presentations.Add) // PowerPoint::PresentationsPtr spPres = spPpApp->Presentations; PowerPoint::_PresentationPtr spPre = spPres->Add(Office::msoTrue); _putws(L"一个新的演示文稿被建立"); ///////////////////////////////////////////////////////////////////// // 插入一个幻灯片并加入一些文本 // PowerPoint::SlidesPtr spSlides = spPre->Slides; wprintf(L"当前演示文稿有 %ld 个幻灯片\n", spSlides->Count); // 插入一个幻灯片 _putws(L"插入一个幻灯片"); PowerPoint::_SlidePtr spSlide = spSlides->Add(1, PowerPoint::ppLayoutText); // Add some texts to the slide _putws(L"添加一些文本"); PowerPoint::ShapesPtr spShapes = spSlide->Shapes; PowerPoint::ShapePtr spShape = spShapes->Item((long)1); PowerPoint::TextFramePtr spTxtFrame = spShape->TextFrame; PowerPoint::TextRangePtr spTxtRange = spTxtFrame->TextRange; spTxtRange->Text = _bstr_t(L"一站式代码框架"); ///////////////////////////////////////////////////////////////////// // 保存演示文稿为pptx文件并关闭它。 // _putws(L"保存并关闭演示文稿"); // 设置文件名 // 获取当前exe文件名 wchar_t szFileName[MAX_PATH]; if (!GetModuleDirectory(szFileName, ARRAYSIZE(szFileName))) { _putws(L"GetModuleDirectory 出错"); return 1; } // 将"Sample1.pptx"连接到目录字符串 wcsncat_s(szFileName, ARRAYSIZE(szFileName), L"Sample1.pptx", 12); spPre->SaveAs(_bstr_t(szFileName), PowerPoint::ppSaveAsOpenXMLPresentation, Office::msoTriStateMixed); spPre->Close(); ///////////////////////////////////////////////////////////////////// // 退出PowerPoint应用程序。 // _putws(L"退出PowerPoint应用程序"); spPpApp->Quit(); ///////////////////////////////////////////////////////////////////// // 释放COM对象 // // 释放引用对于智能指针不是必须的 // ... // spPowerPointApp.Release(); // ... } catch (_com_error &err) { wprintf(L"PowerPoint抛出异常: %s\n", err.ErrorMessage()); wprintf(L"描述: %s\n", (LPCWSTR) err.Description()); } // 在此线程取消初始化 CoUninitialize(); return 0; }