【发布时间】:2014-07-03 19:41:55
【问题描述】:
我的程序的目的是制作一个模板 Word 文档的副本,其中包含一个表格,并将文本数据输入到所述表格的单元格中。我的问题是,每当我运行程序时,它都不会在单元格中输入任何文本,我已经在网上搜索过,据我所知,我将文本输入到单元格中的方式应该没有任何问题.
这里是相关代码。
try
{
//create filepaths for template and the soon to be created file
object oMissing = System.Reflection.Missing.Value;
object notReadOnly = false;
object oldDocPath = (object)@"Desktop:\testDoc.docx";
object newDocPath = (object)@"Desktop:\testDoc2.docx";
//start up word doc
Word.Application app = new Word.Application();
//open template in word
Word.Document oldDoc = app.Documents.Open(ref oldDocPath,
ref oMissing, ref notReadOnly, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
//save template under new name to make a copy
app.ActiveDocument.SaveAs2(ref newDocPath,
ref oMissing, ref notReadOnly, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
//close template and open the new document
object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
((_Document)oldDoc).Close(ref doNotSaveChanges, ref oMissing, ref oMissing);
Marshal.FinalReleaseComObject(oldDoc);
((_Application)app).Quit(ref oMissing, ref oMissing, ref oMissing);
Marshal.FinalReleaseComObject(app);
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oDoc = oWord.Documents.Add(ref newDocPath, ref oMissing, ref oMissing, ref oMissing);
//populate the table
Word.Table tbl = oDoc.Tables[1];
tbl.Cell(1,1).Range.Text = "Test";
//Close the last Word doc
((_Document)oDoc).Close(ref doNotSaveChanges, ref oMissing, ref oMissing);
Marshal.FinalReleaseComObject(oDoc);
((_Application)oWord).Quit(ref oMissing, ref oMissing, ref oMissing);
Marshal.FinalReleaseComObject(oWord);
}
catch( Exception ex )
{
MessageBox.Show("Exception Caught: " + ex.Message);
}
好的;我的最佳猜测是 tbl.Cell(1,1).Range.Text = "Test"; 可能没有与我设置其余代码的方式正确集成,所以任何帮助都会得到帮助
【问题讨论】: