我正在使用 Microsoft.Office.Interop.Word 包 在Word文档中添加表格。
我已经创建了一个表OUTERTABLE
*(3 * 3)* 。
现在我想在INNERTABLE
的单元格(2,2)中包含另一个表OUTERTABLE
(2 * 2)。
以下是我的代码:
public object InvokeMethod(object obj, string memberName, object[] objParam)
{
return InvokeMember(obj, memberName, BindingFlags.InvokeMethod, objParam);
}
public object SetProperty(object obj, string memberName, object[] objParam)
{
return InvokeMember(obj, memberName, BindingFlags.SetProperty, objParam);
}
public object GetProperty(object obj, string memberName, object[] objParam)
{
return InvokeMember(obj, memberName, BindingFlags.GetProperty, objParam);
}
public object InvokeMember(object obj, string memberName, BindingFlags flag, object[] objParam)
{
// mzimmers 2015 06 29: needed this for something I can't now remember.
try
{
return obj.GetType().InvokeMember(memberName, flag, null, obj, objParam);
}
catch(Exception ex)
{
throw ex;
}
}
object oOuterTable = InvokeMethod(oDocTables, "Add", new object[] { wrdRng, 3, 3, oMissing, oMissing });
//Word.Table oTable = oDoc.Tables.Add(wrdRng, ai_row, ai_col, ref oMissing, ref oMissing);
oGalleryOuterTable = oOuterTable;
object objCell = InvokeMethod(oGalleryOuterTable, "Cell", new object[] { 2, 2 });
object objTR = GetProperty(objCell, "Range", null);
SetProperty(objTR, "Start", new object[] { });
object tblRng = InvokeMethod(objCell, "SetRange", new object[] { objTR, objTR });
object oTable = InvokeMethod(oGalleryOuterTable, "Add", new object[] { tblRng, ai_row, ai_col, oMissing, oMissing });
我用Google搜索并发现了这篇文章:
wrdRng.SetRange(OUTERTABLE.Cell(2, 2).Range.Start, OUTERTABLE.Cell(2, 2).Range.Start);
我想通过使用对象和上面的Get,Set,Invoke方法来复制上面的方法。我坚持设置“ Range.Start
”部分。
不确定如何解决这个问题。谁可以帮我这个事?我不确定该链接中给出的解决方案。
答案 0 :(得分:1)
根据您的评论,我已经更新了我的解决方案,所以它只使用PInvoke 我还添加了一些新的Get / SetProperty构造函数
class Program
{
public static object GetProperty(object obj, string memberName, object[] objParam)
{
return InvokeMember(obj, memberName, BindingFlags.GetProperty, objParam);
}
public static object SetProperty(object obj, string memberName, object[] objParam)
{
return InvokeMember(obj, memberName, BindingFlags.SetProperty, objParam);
}
public static void SetProperty(object obj, string sProperty, object oValue)
{
object[] oParam = new object[1];
oParam[0] = oValue;
obj.GetType().InvokeMember(sProperty, BindingFlags.SetProperty, null, obj, oParam);
}
public static object InvokeMethod(object obj, string memberName, object[] objParam)
{
return InvokeMember(obj, memberName, BindingFlags.InvokeMethod, objParam);
}
public static object InvokeMember(object obj, string memberName, BindingFlags flag, object[] objParam)
{
try
{
return obj.GetType().InvokeMember(memberName, flag, null, obj, objParam);
}
catch (Exception ex)
{
throw ex;
}
}
public static object InvokeMethod(object obj, string sProperty, object oValue)
{
object[] oParam = new object[1];
oParam[0] = oValue;
return obj.GetType().InvokeMember
(sProperty, BindingFlags.InvokeMethod, null, obj, oParam);
}
public static object GetProperty(object obj, string sProperty, object oValue)
{
object[] oParam = new object[1];
oParam[0] = oValue;
return obj.GetType().InvokeMember
(sProperty, BindingFlags.GetProperty, null, obj, oParam);
}
public static object GetProperty(object obj, string sProperty)
{
return obj.GetType().InvokeMember
(sProperty, BindingFlags.GetProperty, null, obj, null);
}
static void Main(string[] args)
{
Application oWord = new Application();
try
{
Document oDoc;
oWord.Visible = true;
object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
Range wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
object outerTables = GetProperty(wrdRng, "Tables");
object oTable = InvokeMethod(outerTables, "Add", new object[] { wrdRng, 3, 3, oMissing, oMissing });
var outerBorders = GetProperty(oTable, "Borders");
SetProperty(outerBorders, "OutsideLineStyle", 1);//WdLineStyle.wdLineStyleSingle = 1
SetProperty(outerBorders, "OutsideColor", 0); //WdColor.wdColorBlack = 0
SetProperty(oTable, "TableDirection",1 );//WdTableDirection.wdTableDirectionLtr=1
SetProperty(outerBorders, "InsideLineStyle", 1);//WdLineStyle.wdLineStyleSingle=1
SetProperty(outerBorders, "InsideColor", 0);//WdColor.wdColorBlack = 0
object oDocTables = oDoc.Tables;
object oOuterTable = InvokeMethod(oDocTables, "Add", new object[] { wrdRng, 3, 3, oMissing, oMissing });
object objCell = InvokeMethod(oTable, "Cell", new object[] { 2, 2 });
object objTR = GetProperty(objCell, "Range", null);
object Start = GetProperty(objTR, "Start", null);
InvokeMethod(wrdRng, "SetRange", new object[] { Start, Start });
object innerTables = GetProperty(oOuterTable, "Tables", null);
object iTable = InvokeMethod(innerTables, "Add", new object[] { wrdRng, 2, 2, oMissing, oMissing });
var innerBorders = GetProperty(iTable, "Borders");
SetProperty(innerBorders, "OutsideLineStyle", 1);
SetProperty(innerBorders, "OutsideColor", 0);
SetProperty(iTable, "TableDirection", 1);
SetProperty(innerBorders, "InsideLineStyle", 1);
SetProperty(innerBorders, "InsideColor", 0);
}
catch (Exception ex)
{
throw ex;
}
finally
{
// oWord.Quit();
}
}
}
导致:
答案 1 :(得分:1)
嗯,除了获取文档对象之外,以下是使用PInvoke的所有内容,因为您没有告诉我们您是如何做到的。
我注意到您的代码以及Xavave的代码使用Word对象模型的成员而不是PInvoke中的对象作为插入表的目标Range。我知道你想要PInvoke中的所有东西......并且你明确要求获得Range的方法。
我也不知道如何在另一个答案中插入额外的行 - 据我所知,该代码并不存在。但同样清楚的是,这是在较新版本的Word中运行,可能没有限制。
无论如何,我的代码在整个过程中都使用了PInvoke ...我省略了设置边框的代码,因为它与你如何将表行添加到新创建的嵌套表并分散主题的问题无关。
object oDoc = (object) doc;
object[] oArgs = null;
object[] oArgsItem = {1};
object oDocTables = oDoc.GetType().InvokeMember("Tables", BindingFlags.GetProperty, null, oDoc, oArgs);
object oOuterTable = InvokeMethod(oDocTables, "Item", oArgsItem);
object oTargetCell = InvokeMethod(oOuterTable, "Cell", new object[] { 2, 2 });
object oCellRange = InvokeMember(oTargetCell, "Range", BindingFlags.GetProperty, oArgs);
object oNestedTable = InvokeMethod(oDocTables, "Add", new object[] {oCellRange, 3, 3, missing, missing} );
object oTableRows = InvokeMember(oNestedTable, "Rows", BindingFlags.GetProperty, oArgs);
InvokeMethod(oTableRows, "Add", new object[] { });