C# 报错 This method or property is not available because this
最佳回答
给你个2010调用的例子。研究下吧
//Be sure to add this reference:
//Project>Add Reference>。NET tab>Microsoft。Office。Interop。Word
//open Word App
Microsoft。Office。Interop。Word。Application msWord = new Microsoft。Office。Interop。Word。Application();
//make it visible or it'll stay running in the background
msWord。Visible = true;
//open a new document based on the Word template。
//You shouldn't open the template directly using msWord。Documents。Open(path) unless you want to edit the template itself。
Microsoft。Office。Interop。Word。Document wordDoc = msWord。Documents。Add(@"c:\MyTemplate。dotx");
//find the bookmark
string bookmarkName = "BookmarkToFind";
if (wordDoc。Bookmarks。Exists(bookmarkName))
{
Microsoft。Office。Interop。Word。Bookmark bk = wordDoc。Bookmarks[bookmarkName];
//set the document's range to immediately after the bookmark。
//If you want to add the table *into* the bookmark, it needs to be done differently。
//This page has a good explanation of the differences between adding to the bookmark's range vs adding after the bookmark's range。
//http://gregmaxey。mvps。org/word_tip_pages/insert_text_at_or_in_bookmark。html
//It's a little more hassle because you have to re-add the bookmark after inserting into it,
//so inserting after the bookmark is usually fine less you're going to be inserting text programmatically at the same bookmark a second time。
Microsoft。Office。Interop。Word。Range rng = wordDoc。Range(bk。Range。End, bk。Range。End);
//create a table with 8 rows and 5 columns into the range。
Microsoft。Office。Interop。Word。Table tbl = wordDoc。Tables。Add(rng, 8, 5);
//set the table's borders。
tbl。Borders。InsideLineStyle = Microsoft。Office。Interop。Word。WdLineStyle。wdLineStyleSingle;
tbl。Borders。OutsideLineStyle = Microsoft。Office。Interop。Word。WdLineStyle。wdLineStyleSingle;
//merge the cells in the first row down to 2 columns (Word's cells start at 1, not at 0)。
tbl。Cell(1, 1)。Merge(tbl。Cell(1, 3));
//distribute the columns evenly
tbl。Rows[1]。Select();
msWord。Selection。Cells。DistributeWidth();
//rows 2-5 already have 5 columns so don't touch them。
//merge rows 6-8 into single-columns rows。
for (int x = 6; x < 9; x++)
{
tbl。Cell(x,1)。Merge(tbl。Cell(x,5));
}
//put the cursor in the table's first cell。
rng=wordDoc。Range(tbl。Cell(1,1)。Range。Start, tbl。Cell(1,1)。Range。Start);
rng。Select();
最新回答共有2条回答
-
2026-04-02 06:35:28英勇的酒窝
回复版本问题,是不是你们俩 一个是 office2010 另一个是 2003或者2007?给你个2010调用的例子。研究下吧//Be sure to add this reference://Project>Add Reference>。NET tab>Microsoft。Office。Interop。Word//open Word AppMicrosoft。Office。Interop。Word。Application msWord = new Microsoft。Office。Interop。Word。Application();//make it visible or it'll stay running in the backgroundmsWord。Visible = true;//open a new document based on the Word template。//You shouldn't open the template directly using msWord。Documents。Open(path) unless you want to edit the template itself。Microsoft。Office。Interop。Word。Document wordDoc = msWord。Documents。Add(@"c:\MyTemplate。dotx");//find the bookmarkstring bookmarkName = "BookmarkToFind";if (wordDoc。Bookmarks。Exists(bookmarkName)){ Microsoft。Office。Interop。Word。Bookmark bk = wordDoc。Bookmarks[bookmarkName]; //set the document's range to immediately after the bookmark。 //If you want to add the table *into* the bookmark, it needs to be done differently。 //This page has a good explanation of the differences between adding to the bookmark's range vs adding after the bookmark's range。 //http://gregmaxey。mvps。org/word_tip_pages/insert_text_at_or_in_bookmark。html //It's a little more hassle because you have to re-add the bookmark after inserting into it, //so inserting after the bookmark is usually fine less you're going to be inserting text programmatically at the same bookmark a second time。 Microsoft。Office。Interop。Word。Range rng = wordDoc。Range(bk。Range。End, bk。Range。End); //create a table with 8 rows and 5 columns into the range。 Microsoft。Office。Interop。Word。Table tbl = wordDoc。Tables。Add(rng, 8, 5); //set the table's borders。 tbl。Borders。InsideLineStyle = Microsoft。Office。Interop。Word。WdLineStyle。wdLineStyleSingle; tbl。Borders。OutsideLineStyle = Microsoft。Office。Interop。Word。WdLineStyle。wdLineStyleSingle; //merge the cells in the first row down to 2 columns (Word's cells start at 1, not at 0)。 tbl。Cell(1, 1)。Merge(tbl。Cell(1, 3)); //distribute the columns evenly tbl。Rows[1]。Select(); msWord。Selection。Cells。DistributeWidth(); //rows 2-5 already have 5 columns so don't touch them。 //merge rows 6-8 into single-columns rows。 for (int x = 6; x < 9; x++) { tbl。Cell(x,1)。Merge(tbl。Cell(x,5)); } //put the cursor in the table's first cell。 rng=wordDoc。Range(tbl。Cell(1,1)。Range。Start, tbl。Cell(1,1)。Range。Start); rng。Select();
热门文章
- 康达学院专转本五年制
- 高考一个考场分ab卷吗
- not only but also用法
- 某物体做自由落体运动,从释放开始计时,则物体在前2s内的平均速度为______m/s,物体下落2m时的速度大小为______m/s.
- 三角函数公式大全表格
- 地理中考必背知识点2022
- 2013-2014学年小学六年级科学上学期期末考试试卷及答案
- 人教版2014-2015学年小学五年级英语第二学期期中教学质量检测试卷及答案
- 【Linux驱动开发】设备树详解(二)设备树语法详解
- 别跟客户扯细节
- 在别的城市买房子能落户吗
- 卖房前要把装修贷还完吗
- 高中政治教学提高教学效果的方法探究
- “互联网+”背景下的初中英语课堂教学改革与创新策略研究
- 2022年终止合同范本
- 租房合同范本范文
- 如何挑选土豆
- 如何挑选土鸡
