请教各位才子Excel操作问题
Range("A6:A20").Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = xlVertical
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
我录制的宏,With Selection End With函数应该怎么转化为OLE代码呢?
m_varSheet.OlePropertyGet("Range", "A9:A20").OleFunction(1, 1, false, 1, false, 0, false, 1, true);好像不对
[解决办法]
这个问题虽然不难,但是对于不太熟悉OLE的朋友来说,还是有点棘手。
#include <comobj.hpp>void __fastcall TForm1::Button1Click(TObject *Sender){ Variant vExcelApp = CreateOleObject("Excel.Application"); // 使其可视 vExcelApp.OlePropertySet("Visible", true); // 准备打开的文件名,请根据需要更换此文件名 String strXlsFile = "d:\\ccrun\\123.xls"; // 打开文档 vExcelApp.OlePropertyGet("Workbooks").OleFunction("Open", strXlsFile.c_str()); // 操作这个工作表 Variant vSheet = vExcelApp.OlePropertyGet("ActiveWorkbook") .OlePropertyGet("Sheets", 1); // 选中A9:A20的区域 vSheet.OlePropertyGet("Range", "A9:A20").OleProcedure("select"); // 针对当前选择的区域进行设置 Variant vSelect = vExcelApp.OlePropertyGet("Selection"); vSelect.OlePropertySet("HorizontalAlignment", 1); // xlGeneral vSelect.OlePropertySet("VerticalAlignment", -4108); // xlCenter vSelect.OlePropertySet("WrapText", false); vSelect.OlePropertySet("Orientation", -4166); // xlVertical vSelect.OlePropertySet("AddIndent", false); vSelect.OlePropertySet("IndentLevel", 0); vSelect.OlePropertySet("ShrinkToFit", false); vSelect.OlePropertySet("ReadingOrder", -5002); // xlContext vSelect.OlePropertySet("MergeCells", true); // 后续代码略 // ...}
[解决办法]