0
我的程序的目的是製作一個模板Word文檔的副本,其中包含一個表格,並將文本數據輸入到所述表格的單元格中。我的問題是,無論什麼時候我運行該程序,它都不會輸入任何文本到單元格中,我已經在線搜索,並且據我所知,如何將文本輸入到單元格中應該沒有任何問題。將值輸入MS 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」;可能不與我建立了我的代碼,所以任何幫助將appriciated
我承認應該保存這些改變,但一定忘記改變它。感謝您的幫助,我根據您的反饋進行了一些更改,現在情況似乎正常。 – user3799505