看了妖哥的范例,嘗試于BCB中產生表格,更改框線并插入圖片及文字,
成功的在表格的指定欄位里插入文字,
但一直無法插入圖片,以下是程式代碼,是否有請高手相救?
PS.BCB能否修改WORK表格的框線??
謝謝~ m(_._)m
WideString strPicFile, sTmp;
Variant vWordApp, vWordDocuments, vActiveDoc, vShape, vSelect;
Variant vTable, vCell;
vWordApp = CreateOleObject("Word.Application");
vWordApp.OlePropertySet("Visible", true);
vWordDocuments = vWordApp.OlePropertyGet("Documents");
vWordDocuments.OleFunction("Add");
vActiveDoc = vWordApp.OlePropertyGet("ActiveDocument");
vActiveDoc.OlePropertyGet("PageSetup").OlePropertySet("TopMargin", 35 );
vActiveDoc.OlePropertyGet("PageSetup").OlePropertySet("BottomMargin", 20 );
vActiveDoc.OlePropertyGet("PageSetup").OlePropertySet("RightMargin", 20 );
vActiveDoc.OlePropertyGet("PageSetup").OlePropertySet("LeftMargin", 35 );
vSelect = vWordApp.OlePropertyGet("Selection");
vTable = vWordApp.OlePropertyGet("ActiveDocument")
.OlePropertyGet("Tables").OleFunction("Add",
vSelect.OlePropertyGet("Range"),
1,
3, // 列數
1, // DefaultTableBehavior:=wdWord9TableBehavior
0); // AutoFitBehavior:=wdAutoFitFixed
vCell = vTable.OleFunction("Cell", 1, 1);
vCell.OlePropertySet("Range", WideString("測試1\r\n"));
strPicFile = "C:\\A002166.jpg";
vWordApp.OlePropertyGet("Selection").OlePropertyGet("InlineShapes").OleFunction("AddPicture", strPicFile, false, true);
vCell = vTable.OleFunction("Cell", 1, 2);
vCell.OlePropertySet("Range", WideString("測試2"));
strPicFile = "C:\\A002166-0322103253186.jpg";
vWordApp.OlePropertyGet("Selection").OlePropertyGet("InlineShapes").OleFunction("AddPicture", strPicFile, false, true);
uj5u.com熱心網友回復:
OLE專業戶來也!!!先解決插入圖片的問題:
// 第一格子
vCell = vTable.OleFunction("Cell", 1, 1);
vCell.OlePropertyGet("Range").OleProcedure("Select");
// 插入文字
vWordApp.OlePropertyGet("Selection").OleProcedure("TypeText", WideString("測試1\r\n"));
// 插入圖片
strPicFile = "D:\\ccrun\\1.jpg";
if (FileExists(strPicFile))
{
vWordApp.OlePropertyGet("Selection").OlePropertyGet("InlineShapes")
.OleFunction("AddPicture", strPicFile, false, true);
}
// 第二個格子
vCell = vTable.OleFunction("Cell", 1, 2);
vCell.OlePropertyGet("Range").OleProcedure("Select");
// 插入文字
vWordApp.OlePropertyGet("Selection").OleProcedure("TypeText", WideString("測試2\r\n"));
// 插入圖片
strPicFile = "D:\\ccrun\\2.jpg";
if (FileExists(strPicFile))
{
vWordApp.OlePropertyGet("Selection").OlePropertyGet("InlineShapes")
.OleFunction("AddPicture", strPicFile, false, true);
}
uj5u.com熱心網友回復:
妖哥~超帥!!
原來我一直沒有 Select
我又改了一下~整個WORD可以置中,
另外請教一下,
如果我想回到第一格子中的內容,最后再插入新的文字跟圖案,
該怎么Select??
謝謝~~
//[前略]
vSelect.OlePropertyGet("ParagraphFormat").OlePropertySet("Alignment", 1 ); //置中
//…
// 第一格子
vCell = vTable.OleFunction("Cell", 1, 1);
vCell.OlePropertyGet("Range").OleProcedure("Select");
// 插入文字
vWordApp.OlePropertyGet("Selection").OleProcedure("TypeText", WideString("測試1\r\n"));
// 插入圖片
strPicFile = "D:\\ccrun\\1.jpg";
if (FileExists(strPicFile))
{
vWordApp.OlePropertyGet("Selection").OlePropertyGet("InlineShapes")
.OleFunction("AddPicture", strPicFile, false, true);
}
// 第二個格子
vCell = vTable.OleFunction("Cell", 1, 2);
vCell.OlePropertyGet("Range").OleProcedure("Select");
// 插入文字
vWordApp.OlePropertyGet("Selection").OleProcedure("TypeText", WideString("測試2\r\n"));
// 插入圖片
strPicFile = "D:\\ccrun\\2.jpg";
if (FileExists(strPicFile))
{
vWordApp.OlePropertyGet("Selection").OlePropertyGet("InlineShapes")
.OleFunction("AddPicture", strPicFile, false, true);
}
uj5u.com熱心網友回復:
回到第一個格子中,還是要對應Cell的Range再Select一下,如果要將文字或圖片插入到已有資料的后面,可以模擬向下的按鍵,不過可能不準確。uj5u.com熱心網友回復:
感謝您~ 我試試看!!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/154316.html
標籤:基礎類
