c#在word中插入bookmarks后如何用它来存贮数据,比如文字的word中的位置
2个回答
展开全部
1、在Word文档中插入一个书签,书签名称为“tl”;
2、新建一个C#项目,然后在引用中添加Word类库;由于我使用的是Office2007,因此选择的是"Microsoft Word 12.0 Object Library",如果你使用的是Office2003,就应该选择11.0;
3、在代码顶部添加对Word类库的引用;
using Word = Microsoft.Office.Interop.Word;
4、打开Word文档
object missingValue = System.Reflection.Missing.Value;
object myTrue = false; //不显示Word窗口
object fileName = @"F:\Doc1.doc";
Word._Application oWord = new Word.ApplicationClass();
Word._Document oDoc;
oDoc = oWord.Documents.Open(ref fileName, ref missingValue,
ref myTrue, ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue);
5、找到刚才添加的书签(注释部分为跳转至标签位置并添加文本的三种方法,做其他参考)
/*方法一:使用Word应用程序变量,使用这种方法,wordApp.Documents.Open()中确保isVisible的值为true
if (wordApp.ActiveDocument.Bookmarks.Exists("BM_TEST"))
{
wordApp.ActiveDocument.Bookmarks.get_Item(ref bk).Select();
wordApp.Selection.TypeText("insert text"); // 插入文本
}
方法二:使用Word文档变量
if (wordDoc.Bookmarks.Exists("BM_TEST"))
{
wordDoc.Bookmarks.get_Item(ref bk).Range.Text = "insert text"; // 插入文本
}
方法三:使用Goto函数,跳转到指定书签
object BookMarkName = "BM_TEST";
object what = MSWord.WdGoToItem.wdGoToBookmark;
wordDoc.ActiveWindow.Selection.GoTo(ref what, ref Nothing, ref Nothing, ref BookMarkName);
wordDoc.ActiveWindow.Selection.TypeText("Hello!");
*/
object tmp = "t1";
Word.Range startRange = oWord.ActiveDocument.Bookmarks.get_Item(ref tmp).Range;
6、删除在该位置的表格
Word.Table tbl = startRange.Tables[1];
tbl.Delete();
如果书签所在的位置并没有插入表格,程序并不会删除该位置下面的表格,而是会抛出异常,报错。
7、插入表格,并划线
//添加表格
oDoc.Tables.Add(startRange, 5, 4, ref missingValue, ref missingValue);
//为表格划线
startRange.Tables[1].Borders[WdBorderType.wdBorderTop].LineStyle = WdLineStyle.wdLineStyleSingle;
startRange.Tables[1].Borders[WdBorderType.wdBorderLeft].LineStyle = WdLineStyle.wdLineStyleSingle;
startRange.Tables[1].Borders[WdBorderType.wdBorderRight].LineStyle = WdLineStyle.wdLineStyleSingle;
startRange.Tables[1].Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleSingle;
startRange.Tables[1].Borders[WdBorderType.wdBorderHorizontal].LineStyle = WdLineStyle.wdLineStyleSingle;
startRange.Tables[1].Borders[WdBorderType.wdBorderVertical].LineStyle = WdLineStyle.wdLineStyleSingle;
全部的代码如下:
object missingValue = System.Reflection.Missing.Value;
object myTrue = false; //不显示Word窗口
object fileName = @"F:\Doc1.doc";
Word._Application oWord = new Word.ApplicationClass();
Word._Document oDoc;
oDoc = oWord.Documents.Open(ref fileName, ref missingValue,
ref myTrue, ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue);
try
{
object tmp = "t1";
Word.Range startRange = oWord.ActiveDocument.Bookmarks.get_Item(ref tmp).Range;
//删除指定书签位置后的第一个表格
Word.Table tbl = startRange.Tables[1];
tbl.Delete();
//添加表格
oDoc.Tables.Add(startRange, 5, 4, ref missingValue, ref missingValue);
//为表格划线
startRange.Tables[1].Borders[WdBorderType.wdBorderTop].LineStyle = WdLineStyle.wdLineStyleSingle;
startRange.Tables[1].Borders[WdBorderType.wdBorderLeft].LineStyle = WdLineStyle.wdLineStyleSingle;
startRange.Tables[1].Borders[WdBorderType.wdBorderRight].LineStyle = WdLineStyle.wdLineStyleSingle;
startRange.Tables[1].Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleSingle;
startRange.Tables[1].Borders[WdBorderType.wdBorderHorizontal].LineStyle = WdLineStyle.wdLineStyleSingle;
startRange.Tables[1].Borders[WdBorderType.wdBorderVertical].LineStyle = WdLineStyle.wdLineStyleSingle;
}
catch
{
//异常处理
}
object bSaveChange = true;
oDoc.Close(ref bSaveChange, ref missingValue, ref missingValue);
oDoc = null;
oWord = null;
2、新建一个C#项目,然后在引用中添加Word类库;由于我使用的是Office2007,因此选择的是"Microsoft Word 12.0 Object Library",如果你使用的是Office2003,就应该选择11.0;
3、在代码顶部添加对Word类库的引用;
using Word = Microsoft.Office.Interop.Word;
4、打开Word文档
object missingValue = System.Reflection.Missing.Value;
object myTrue = false; //不显示Word窗口
object fileName = @"F:\Doc1.doc";
Word._Application oWord = new Word.ApplicationClass();
Word._Document oDoc;
oDoc = oWord.Documents.Open(ref fileName, ref missingValue,
ref myTrue, ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue);
5、找到刚才添加的书签(注释部分为跳转至标签位置并添加文本的三种方法,做其他参考)
/*方法一:使用Word应用程序变量,使用这种方法,wordApp.Documents.Open()中确保isVisible的值为true
if (wordApp.ActiveDocument.Bookmarks.Exists("BM_TEST"))
{
wordApp.ActiveDocument.Bookmarks.get_Item(ref bk).Select();
wordApp.Selection.TypeText("insert text"); // 插入文本
}
方法二:使用Word文档变量
if (wordDoc.Bookmarks.Exists("BM_TEST"))
{
wordDoc.Bookmarks.get_Item(ref bk).Range.Text = "insert text"; // 插入文本
}
方法三:使用Goto函数,跳转到指定书签
object BookMarkName = "BM_TEST";
object what = MSWord.WdGoToItem.wdGoToBookmark;
wordDoc.ActiveWindow.Selection.GoTo(ref what, ref Nothing, ref Nothing, ref BookMarkName);
wordDoc.ActiveWindow.Selection.TypeText("Hello!");
*/
object tmp = "t1";
Word.Range startRange = oWord.ActiveDocument.Bookmarks.get_Item(ref tmp).Range;
6、删除在该位置的表格
Word.Table tbl = startRange.Tables[1];
tbl.Delete();
如果书签所在的位置并没有插入表格,程序并不会删除该位置下面的表格,而是会抛出异常,报错。
7、插入表格,并划线
//添加表格
oDoc.Tables.Add(startRange, 5, 4, ref missingValue, ref missingValue);
//为表格划线
startRange.Tables[1].Borders[WdBorderType.wdBorderTop].LineStyle = WdLineStyle.wdLineStyleSingle;
startRange.Tables[1].Borders[WdBorderType.wdBorderLeft].LineStyle = WdLineStyle.wdLineStyleSingle;
startRange.Tables[1].Borders[WdBorderType.wdBorderRight].LineStyle = WdLineStyle.wdLineStyleSingle;
startRange.Tables[1].Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleSingle;
startRange.Tables[1].Borders[WdBorderType.wdBorderHorizontal].LineStyle = WdLineStyle.wdLineStyleSingle;
startRange.Tables[1].Borders[WdBorderType.wdBorderVertical].LineStyle = WdLineStyle.wdLineStyleSingle;
全部的代码如下:
object missingValue = System.Reflection.Missing.Value;
object myTrue = false; //不显示Word窗口
object fileName = @"F:\Doc1.doc";
Word._Application oWord = new Word.ApplicationClass();
Word._Document oDoc;
oDoc = oWord.Documents.Open(ref fileName, ref missingValue,
ref myTrue, ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue);
try
{
object tmp = "t1";
Word.Range startRange = oWord.ActiveDocument.Bookmarks.get_Item(ref tmp).Range;
//删除指定书签位置后的第一个表格
Word.Table tbl = startRange.Tables[1];
tbl.Delete();
//添加表格
oDoc.Tables.Add(startRange, 5, 4, ref missingValue, ref missingValue);
//为表格划线
startRange.Tables[1].Borders[WdBorderType.wdBorderTop].LineStyle = WdLineStyle.wdLineStyleSingle;
startRange.Tables[1].Borders[WdBorderType.wdBorderLeft].LineStyle = WdLineStyle.wdLineStyleSingle;
startRange.Tables[1].Borders[WdBorderType.wdBorderRight].LineStyle = WdLineStyle.wdLineStyleSingle;
startRange.Tables[1].Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleSingle;
startRange.Tables[1].Borders[WdBorderType.wdBorderHorizontal].LineStyle = WdLineStyle.wdLineStyleSingle;
startRange.Tables[1].Borders[WdBorderType.wdBorderVertical].LineStyle = WdLineStyle.wdLineStyleSingle;
}
catch
{
//异常处理
}
object bSaveChange = true;
oDoc.Close(ref bSaveChange, ref missingValue, ref missingValue);
oDoc = null;
oWord = null;
展开全部
27.C28.B29.A 30.B31.C32.C33.C34.B35.D36.A37.D38.C39.B40.D41.B42.C43.B44.D45.B46.D47.D48.B49.A50.B51.C53.D54.D55.B56.D57.D58.C59.D60.D61.D62.D63.A64.A65.C66.B67.B68.A69.A70.A71.D72.D73.C74.A75.D76.C77.B78.B79.D80.B81.A82.C83.B84.A85.B86.D87.C88.A89.B90.B91.C92.D93.A---------
我刚读中专..
不过除了办公软件外``其他都是在没读中专前就已经会了
我刚读中专..
不过除了办公软件外``其他都是在没读中专前就已经会了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询