word文档里插入图片后,无法改边框颜色

hexx 2020-11-20 12:05:32
我的代码如下:
vShape = vWordDoc.OlePropertyGet("Shapes").OleFunction("AddPicture", WideString(strFileName), false, true, 0, 0);
vShape.OlePropertyGet("Line").OlePropertyGet("ForeColor").OlePropertySet("RGB", 0x202020);
vShape.OlePropertyGet("Line").OlePropertySet("Visible", true);
结果边框颜色并没改变,请问应怎样写?
...全文
465 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
青蛙工作室 2020-11-24
  • 打赏
  • 举报
回复
OleFunction的返回值可能并不是你想要的图片
ooolinux 2020-11-21
  • 打赏
  • 举报
回复
一些枚举常量值可以查msdn,查对应数字。 主要就是翻译成以下4个: OlePropertyGet OlePropertySet OleFunction OleProcedure
ooolinux 2020-11-21
  • 打赏
  • 举报
回复
我用word2003录制的宏,还不会翻译成C++,你可以试试看: Sub Macro1() ' ' Macro1 Macro ' 宏在 2020/11/21 Saturday 由 AutoBVT 录制 ' Selection.TypeText Text:="标题文字" Selection.TypeParagraph Selection.InlineShapes.AddPicture FileName:= _ "C:\Users\Administrator\Desktop\a.jpg", LinkToFile:=False, _ SaveWithDocument:=True Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend With Selection.InlineShapes(1) With .Borders(wdBorderLeft) .LineStyle = wdLineStyleDashDot .LineWidth = wdLineWidth300pt .Color = wdColorSkyBlue End With With .Borders(wdBorderRight) .LineStyle = wdLineStyleDashDot .LineWidth = wdLineWidth300pt .Color = wdColorSkyBlue End With With .Borders(wdBorderTop) .LineStyle = wdLineStyleDashDot .LineWidth = wdLineWidth300pt .Color = wdColorSkyBlue End With With .Borders(wdBorderBottom) .LineStyle = wdLineStyleDashDot .LineWidth = wdLineWidth300pt .Color = wdColorSkyBlue End With .Borders.Shadow = False End With With Options .DefaultBorderLineStyle = wdLineStyleDashDot .DefaultBorderLineWidth = wdLineWidth300pt .DefaultBorderColor = wdColorSkyBlue End With End Sub
ooolinux 2020-11-21
  • 打赏
  • 举报
回复
翻译了一下,运行效果如图:
// ---------------------------------------------------------------------------

#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);
}
// ---------------------------------------------------------------------------

703

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder ActiveX/COM/DCOM
社区管理员
  • ActiveX/COM/DCOM社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧