C++ Builder操作Word,如何改变paste进去图片的大小?
参照老妖的方法,
vFind.OlePropertySet("Text",strFind); // 要查找文本标记strFind vFind.OleFunction("Execute"); Clip->Assign(imgMain->Picture); vSelect.OleProcedure("Paste"); //将其替换成图片
#include <Comobj.hpp>#include <clipbrd.hpp>void __fastcall TForm1::Button1Click(TObject *Sender){ String strDocName = "C:\\ccrun\\123.doc"; Variant vWordApp; try { vWordApp = Variant::CreateObject("Word.Application"); } catch(...) { MessageBox(Handle, "启动Word出错!", Application->Title.c_str(), MB_OK | MB_ICONERROR); vWordApp = Unassigned; return; } vWordApp.OlePropertySet("Visible", true); vWordApp.OlePropertyGet("Documents"). OleFunction("Open", strDocName.c_str()); Variant vSelect = vWordApp.Exec(PropertyGet("Selection")); String strText = "测试"; // 查找字符串 Variant vFind = vSelect.OlePropertyGet("Find"); vFind.OleProcedure("ClearFormatting"); vFind.OlePropertySet("Text", strText.c_str()); vFind.OlePropertyGet("Replacement").OlePropertySet("Text", ""); vFind.OlePropertySet("Forward", true); vFind.OlePropertySet("Wrap", false); vFind.OlePropertySet("Format", false); vFind.OlePropertySet("MatchCase", false); vFind.OlePropertySet("MatchWholeWord", false); vFind.OlePropertySet("MatchByte", false); vFind.OlePropertySet("MatchWildcards", false); vFind.OlePropertySet("MatchSoundsLike", false); vFind.OlePropertySet("MatchAllWordForms", false); bool bResult = vFind.OleFunction("Execute"); if (bResult) { Clipboard()->Assign(imgMain->Picture); // 将图像复制到剪贴板,再粘贴到Word中 vSelect.OleProcedure("Paste"); Application->ProcessMessages(); // 选中刚粘贴进来的图片,山寨方法 vSelect.OleProcedure("MoveLeft"); vSelect.OleProcedure("MoveRight", 1, // Unit:=wdCharacter, 1, // Count:=1, 1 // Extend:=wdExtend ); if (int(vSelect.OlePropertyGet("Range") .OlePropertyGet("InlineShapes").OlePropertyGet("Count")) > 0) { // 获取到这个图像 Variant vShape = vSelect.OlePropertyGet("Range") .OlePropertyGet("InlineShapes").OleFunction("item", 1); // 无需按比例缩放 vShape.OlePropertySet("LockAspectRatio", 0); // msoFalse // 更改其高度和宽度 vShape.OlePropertySet("Height", 200); vShape.OlePropertySet("Width", 300); ShowMessage("搞定的说"); } else ShowMessage("可能是图片没有粘贴成功或没有选中这个图像"); } else ShowMessage("木有找到关键字"); // // ...}
[解决办法]
高人!