703
社区成员




// ---------------------------------------------------------------------------
#include <vcl.h>
#include <Comobj.hpp>
#pragma hdrstop
#include "Unit1.h"
// 为方便操作建立的宏
#define PG OlePropertyGet
#define PS OlePropertySet
#define FN OleFunction
#define PR OleProcedure
// ---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
// ---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
Label1->Caption = L"OLE操作Word插入图片、设置边框并保存doc文档";
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Variant WordApp; // WordApplication
try
{
WordApp = Variant::CreateObject(L"Word.Application");
}
catch (...)
{
Application->MessageBox(L"无法启动Word,请检查Word程序是否安装了。", L"操作Word",
MB_OK | MB_ICONWARNING);
return;
}
WordApp.PG(L"Documents").FN(L"Add"); // 新建一个文档
Variant Selection = WordApp.PG(L"Selection");
Selection.FN(L"TypeText", WideString(L"插入图片并设置边框")); // 输入文字
Selection.PR(L"TypeParagraph"); // 输入回车
UnicodeString exePath = ExtractFilePath(Application->ExeName); // EXE路径
UnicodeString picPath = exePath + L"a.jpg";
Selection.PG(L"InlineShapes").FN(L"AddPicture", WideString(picPath),
0, 1); // 插入图片
// 设置图片边框,从Word操作录制的宏VBA代码翻译,枚举值可查MSDN
// Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.FN(L"MoveLeft", 1, 1, 1);
// With Selection.InlineShapes(1)
Variant Shape1 = Selection.PG(L"InlineShapes").FN(L"Item", 1);
// With .Borders(wdBorderLeft)
Variant Border1 = Shape1.PG(L"Borders").FN(L"Item", -2);
// .LineStyle = wdLineStyleDashDot
Border1.PS(L"LineStyle", 6);
// .LineWidth = wdLineWidth300pt
Border1.PS(L"LineWidth", 24);
// .Color = wdColorSkyBlue
Border1.PS(L"Color", 16763904);
// End With
// With .Borders(wdBorderRight)
Border1 = Shape1.PG(L"Borders").FN(L"Item", -4);
// .LineStyle = wdLineStyleDashDot
Border1.PS(L"LineStyle", 6);
// .LineWidth = wdLineWidth300pt
Border1.PS(L"LineWidth", 24);
// .Color = wdColorSkyBlue
Border1.PS(L"Color", 16763904);
// End With
// With .Borders(wdBorderTop)
Border1 = Shape1.PG(L"Borders").FN(L"Item", -1);
// .LineStyle = wdLineStyleDashDot
Border1.PS(L"LineStyle", 6);
// .LineWidth = wdLineWidth300pt
Border1.PS(L"LineWidth", 24);
// .Color = wdColorSkyBlue
Border1.PS(L"Color", 16763904);
// End With
// With .Borders(wdBorderBottom)
Border1 = Shape1.PG(L"Borders").FN(L"Item", -3);
// .LineStyle = wdLineStyleDashDot
Border1.PS(L"LineStyle", 6);
// .LineWidth = wdLineWidth300pt
Border1.PS(L"LineWidth", 24);
// .Color = wdColorSkyBlue
Border1.PS(L"Color", 16763904);
// End With
// .Borders.Shadow = False
// End With
// With Options
// .DefaultBorderLineStyle = wdLineStyleDashDot
// .DefaultBorderLineWidth = wdLineWidth300pt
// .DefaultBorderColor = wdColorSkyBlue
// End With
// 保存Word文档并退出
UnicodeString docPath = exePath + L"Doc1.doc";
WordApp.PG(L"ActiveDocument").FN(L"SaveAs", WideString(docPath));
WordApp.PG(L"ActiveDocument").PR(L"Close");
WordApp.PR(L"Quit");
WordApp = Unassigned;
Application->MessageBox((L"已保存为 " + docPath).c_str(), L"保存Word文档",
MB_OK | MB_ICONINFORMATION);
}
// ---------------------------------------------------------------------------