描述:
使用com addin在word中添加了一个工具条按钮,现在希望在这个按钮里面实现在word中添加图片的功能,要怎么做呢?好象shapes.addpicture的方法不能直接用,是不是需要添加什么头文件或引入什么申明之类的东西呢?
解决方案1:
_Application app;
app.CreateDispatch(_T("Word.Application"));
app.SetVisible(TRUE);
Documents docs = app.GetDocuments();
CComVariant Template(_T(""));
CComVariant NewTemplate(false),DocumentType(0),Visible;
docs.Add(&Template,&NewTemplate,&DocumentType,&Visible);
UpdateData(TRUE);
Selection sel = app.GetSelection();
CString strword = "11111111";
sel.TypeText(_T(strword));
sel.TypeParagraph();
_Document doc = app.GetActiveDocument();
InlineShapes inlineshapes;
LPDISPATCH pInlineShapes = sel.GetInlineShapes();
inlineshapes.AttachDispatch(pInlineShapes);
Paragraphs oParagraphs (doc.GetParagraphs()); //
Paragraph oParagraph (oParagraphs.GetLast());
Range oTargetRange(oParagraph.GetRange());
VARIANT vRange;
vRange.vt = VT_DISPATCH;
vRange.pdispVal = oTargetRange.m_lpDispatch;
//add picture
CString filePath = "c:\\back.bmp";
CComVariant LinkToFile(false),SaveWithDocument(true);
inlineshapes.AddPicture(filePath,&LinkToFile,&SaveWithDocument,&vRange);
在cpp里面加上#include "msword.h"
#include <AtlBase.h>
msword.h是通过ClassWizard生成的,具体参见
http://www.vckbase.com/document/viewdoc/?id=1174