| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 706 人关注过本帖
标题:How to insert data into a specific cell in a table?
只看楼主 加入收藏
vincent90152
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2007-7-24
收藏
 问题点数:0 回复次数:4 
How to insert data into a specific cell in a table?

I always get an error message. "Object reference not set to an instance of an object"
Can anyone how to solve this?
Here is my code.
Many thanks for your replying.

private void CreateNewWordFile(string sourceFileName,string destinationFileName,string stratBookmarkName,string endBookmarkName)
{
// Declaring the object variables we will need later
object varFileName = fileName;
object varFalseValue = false;
object varTrueValue = true;
object varMissing = Type.Missing;
// Create a reference to MS Word application
Microsoft.Office.Interop.Word.Application varWord = new Microsoft.Office.Interop.Word.Application();

// Creates a reference to a word document
Microsoft.Office.Interop.Word.Document varDoc = varWord.Documents.Open(ref varFileName, ref varMissing, ref varFalseValue, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing);
// Activate the document
varDoc.Activate();

string test = varDoc.Bookmarks.Count.ToString();

//BOOK MARK FOR START OF SELECTION
object oBookmarkStart = "bookmark1";
object oRngoBookMarkStart = varDoc.Bookmarks.get_Item(ref oBookmarkStart).Range.Start;

//BOOK MARK FOR END OF SELECTION
object oBookmarkEnd = "bookmark2";
object oRngoBookMarkEnd = varDoc.Bookmarks.get_Item(ref oBookmarkEnd).Range.Start;

//SETTING THE RANGE ON THE BOOKMARK BETWEEN TWO BOOKMARKS
Word.Range rngBKMarkSelection = varDoc.Range(ref oRngoBookMarkStart, ref oRngoBookMarkEnd);

//SELECTING THE TEXT
rngBKMarkSelection.Select();
rngBKMarkSelection.Copy();

CreateNewDocument(destinationFileName);
InsertTableIntoNewDocument(destinationFileName);
InsertDataIntoTable(destinationFileName, rngBKMarkSelection);

//CLOSING THE FILE
varDoc.Close(ref varFalseValue, ref varMissing, ref varMissing);
//QUITTING THE APPLICATION
varWord.Quit(ref varMissing, ref varMissing, ref varMissing);
}
private void InsertDataIntoTable(string destinationFileName, Word.Range rngBKMarkSelection)
{
try
{
// Declaring the object variables we will need later
object varFileName = destinationFileName;
object varFalseValue = false;
object varTrueValue = true;
object varMissing = Type.Missing;
// Create a reference to MS Word application
Microsoft.Office.Interop.Word.Application varWord = new Microsoft.Office.Interop.Word.Application();

// Creates a reference to a word document
Microsoft.Office.Interop.Word.Document varDoc = varWord.Documents.Open(ref varFileName, ref varMissing, ref varFalseValue, ref varMissing,
ref varMissing, ref varMissing, ref varMissing, ref varMissing,
ref varMissing, ref varMissing, ref varMissing, ref varMissing,
ref varMissing, ref varMissing, ref varMissing, ref varMissing);
// Activate the document
varDoc.Activate();

//error code here
object oUpperHeadingLevel = "1";
object oLowerHeadingLevel = "1";
object oTOCTableID = varDoc.Tables[1].ID.ToString();
varDoc.TablesOfContents.Add(rngBKMarkSelection, ref varTrueValue, ref oUpperHeadingLevel,
ref oLowerHeadingLevel, ref varMissing, ref oTOCTableID, ref varTrueValue,
ref varTrueValue, ref varMissing, ref varTrueValue, ref varTrueValue, ref varTrueValue);
//error code here

//THE LOCATION WHERE THE FILE NEEDS TO BE SAVED
object oSaveAsFile = destinationFileName;
varDoc.SaveAs(
ref oSaveAsFile, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing);

//CLOSING THE FILE
varDoc.Close(ref varFalseValue, ref varMissing, ref varMissing);
//QUITTING THE APPLICATION
varWord.Quit(ref varMissing, ref varMissing, ref varMissing);
}
catch (Exception varE)
{
MessageBox.Show("Error:\n" + varE.Message, "Error message");
}
}

搜索更多相关主题的帖子: specific cell insert table data 
2007-07-24 11:33
立志成佛
Rank: 1
等 级:新手上路
威 望:2
帖 子:314
专家分:0
注 册:2006-11-1
收藏
得分:0 

[CODE] //error code here
object oUpperHeadingLevel = "1";
object oLowerHeadingLevel = "1";
object oTOCTableID = varDoc.Tables[1].ID.ToString();
varDoc.TablesOfContents.Add(rngBKMarkSelection, ref varTrueValue, ref oUpperHeadingLevel,
ref oLowerHeadingLevel, ref varMissing, ref oTOCTableID, ref varTrueValue,
ref varTrueValue, ref varMissing, ref varTrueValue, ref varTrueValue, ref varTrueValue);
//error code here[/CODE]

楼主确定红色标注的那个TABLE可以正常取到值?
楼主不妨设置断点单步跟踪调试一下

"Object reference not set to an instance of an object"
很明显就是“对象未实例化”。


曾经的曾经已不在
2007-07-24 13:32
vincent90152
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2007-7-24
收藏
得分:0 
回复:(立志成佛)[CODE] //error co...

Many thanks for your replying.
I just find a solution.
Here is the source code.
Hope it help.

private void SelectWantedData(string fileName, string startBookmark, string endBookmark)
{
try
{
// Declaring the object variables we will need later
object varFileName = fileName;
object varFalseValue = false;
object varTrueValue = true;
object varMissing = Type.Missing;
// Create a reference to MS Word application
Microsoft.Office.Interop.Word.Application varWord = new Microsoft.Office.Interop.Word.Application();

// Creates a reference to a word document
Microsoft.Office.Interop.Word.Document varDoc = varWord.Documents.Open(
ref varFileName, ref varMissing, ref varFalseValue, ref varMissing, ref varMissing,
ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
ref varMissing);
// Activate the document
varDoc.Activate();

string test = varDoc.Bookmarks.Count.ToString();

//BOOK MARK FOR START OF SELECTION
object oBookmarkStart = startBookmark;
object oRngoBookMarkStart = varDoc.Bookmarks.get_Item(ref oBookmarkStart).Range.End;

//BOOK MARK FOR END OF SELECTION
object oBookmarkEnd = endBookmark;
object oRngoBookMarkEnd = varDoc.Bookmarks.get_Item(ref oBookmarkEnd).Range.Start;

//SETTING THE RANGE ON THE BOOKMARK BETWEEN TWO BOOKMARKS
Word.Range rngBKMarkSelection = varDoc.Range(ref oRngoBookMarkStart, ref oRngoBookMarkEnd);

//SELECTING THE TEXT
rngBKMarkSelection.Select();
rngBKMarkSelection.Copy();

//CLOSING THE FILE
varDoc.Close(ref varFalseValue, ref varMissing, ref varMissing);
//QUITTING THE APPLICATION
varWord.Quit(ref varMissing, ref varMissing, ref varMissing);
}
catch (Exception varE)
{
MessageBox.Show("Error:\n" + varE.Message, "Error message");
}
}
private void InsertDataIntoTable(string destinationFileName)
{
try
{
// Declaring the object variables we will need later
object varFileName = destinationFileName;
object varFalseValue = false;
object varTrueValue = true;
object varMissing = Type.Missing;
// Create a reference to MS Word application
Microsoft.Office.Interop.Word.Application varWord = new Microsoft.Office.Interop.Word.Application();

// Creates a reference to a word document
Microsoft.Office.Interop.Word.Document varDoc = varWord.Documents.Open(
ref varFileName, ref varMissing, ref varFalseValue, ref varMissing, ref varMissing,
ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
ref varMissing);
// Activate the document
varDoc.Activate();

//paste data
Word.Cell cell = varDoc.Tables[1].Cell(1, 1);
cell.Range.Paste();

//THE LOCATION WHERE THE FILE NEEDS TO BE SAVED
object oSaveAsFile = destinationFileName;
varDoc.SaveAs(
ref oSaveAsFile, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing);

//CLOSING THE FILE
varDoc.Close(ref varFalseValue, ref varMissing, ref varMissing);
//QUITTING THE APPLICATION
varWord.Quit(ref varMissing, ref varMissing, ref varMissing);
}
catch (Exception varE)
{
MessageBox.Show("Error:\n" + varE.Message, "Error message");
}
}

2007-07-24 15:28
冰彩虹
Rank: 4
来 自:上海
等 级:贵宾
威 望:14
帖 子:806
专家分:44
注 册:2007-6-28
收藏
得分:0 
thank you very much for your reply
I just found a solution.
Here is the source code.
Hope it can help you

come on ,man!

Flying without wings
2007-07-24 15:58
立志成佛
Rank: 1
等 级:新手上路
威 望:2
帖 子:314
专家分:0
注 册:2006-11-1
收藏
得分:0 


I just say so complex, so inexplicable.

曾经的曾经已不在
2007-07-24 16:32
快速回复:How to insert data into a specific cell in a table?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020088 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved