如何C#操作word模板生成word文档并打开?
本人用C#操作word模板并在书签处插入数据,然后生成word文档并打开,本人参考了delphi代码,但以下几句不知道用C#怎么写,请教高人指点:delphi代码如下:m...
本人用C#操作word模板并在书签处插入数据,然后生成word文档并打开,本人参考了delphi代码,但以下几句不知道用C#怎么写,请教高人指点:
delphi代码如下:
m_WordApp.WindowState := wdWindowStateMaximize;
m_WordApp.Visible := true;
{m_WordDoc.PrintOut;<br> m_WordDoc.Disconnect;<br> m_WordApp.Disconnect;<br> m_WordDoc.Free;<br> m_wordApp.Free;}
最后四行好像是断开word相关链接的语句,用C#怎么断开,谢谢 展开
delphi代码如下:
m_WordApp.WindowState := wdWindowStateMaximize;
m_WordApp.Visible := true;
{m_WordDoc.PrintOut;<br> m_WordDoc.Disconnect;<br> m_WordApp.Disconnect;<br> m_WordDoc.Free;<br> m_wordApp.Free;}
最后四行好像是断开word相关链接的语句,用C#怎么断开,谢谢 展开
2个回答
2013-08-22
展开全部
最后四句在C#里相当于这两句 (Dispose 里会自己调用Close方法)m_WordDoc.Dispose();m_WordApp.Dispose();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
博思aippt
2024-07-20 广告
2024-07-20 广告
作为深圳市博思云创科技有限公司的工作人员,对于Word文档生成PPT的操作,我们有以下建议:1. 使用另存为功能:在Word中编辑完文档后,点击文件->另存为,选择PowerPoint演示文稿(*.pptx)格式,即可将文档内容转换为PPT...
点击进入详情页
本回答由博思aippt提供
2013-08-22
展开全部
codeproject上有很多操作word的C#代码:
顺便给你几个链接:
http://www.codeproject.com/KB/office/Word_Automation.aspx
http://www.codeproject.com/KB/office/Word2007Automation.aspx
http://www.codeproject.com/KB/office/csautomateword.aspx
private void met_word_automation()
{
try
{
// Declaring the object variables we will need later
object varFileName = "c:\\temp\\doc.docx";
object varFalseValue = false;
object varTrueValue = true;
object varMissing = Type.Missing;
string varText;
// Create a reference to Microsoft 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();
// Change the 1st sentence of the document
varText = "Altered sentence nr. 1";
varDoc.Sentences[0].Text = varText;
// Change the 3rd sentence of the document
varText = "Altered sentence nr. 3";
varDoc.Sentences[2].Text = varText;
// Save the document
varDoc.Save();
// Show the Microsoft Word window with our document on it
varWord.Visible = true;
// Call the print dialog in Word
Microsoft.Office.Interop.Word.Dialog varDlg =
varWord.Application.Dialogs[
Microsoft.Office.Interop.Word.WdWordDialog.wdDialogFilePrint];
varDlg.Show(ref varMissing);
// Print the document
varDoc.PrintOut(ref varTrueValue, ref varFalseValue, ref varMissing,
ref varMissing, ref varMissing, ref varMissing,
ref varMissing, ref varMissing, ref varMissing, ref varMissing,
ref varFalseValue, ref varMissing, ref varMissing,
ref varMissing, ref varMissing, ref varMissing, ref varMissing,
ref varMissing);
// Send mail with this document as an attachment
varDoc.SendMail();
}
catch (Exception varE)
{
MessageBox.Show("Error:\
" + varE.Message, "Error message");
}
}
You need to create references to this .NET namespace in order to use the above code:
Microsoft.Office.Interop.Word
顺便给你几个链接:
http://www.codeproject.com/KB/office/Word_Automation.aspx
http://www.codeproject.com/KB/office/Word2007Automation.aspx
http://www.codeproject.com/KB/office/csautomateword.aspx
private void met_word_automation()
{
try
{
// Declaring the object variables we will need later
object varFileName = "c:\\temp\\doc.docx";
object varFalseValue = false;
object varTrueValue = true;
object varMissing = Type.Missing;
string varText;
// Create a reference to Microsoft 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();
// Change the 1st sentence of the document
varText = "Altered sentence nr. 1";
varDoc.Sentences[0].Text = varText;
// Change the 3rd sentence of the document
varText = "Altered sentence nr. 3";
varDoc.Sentences[2].Text = varText;
// Save the document
varDoc.Save();
// Show the Microsoft Word window with our document on it
varWord.Visible = true;
// Call the print dialog in Word
Microsoft.Office.Interop.Word.Dialog varDlg =
varWord.Application.Dialogs[
Microsoft.Office.Interop.Word.WdWordDialog.wdDialogFilePrint];
varDlg.Show(ref varMissing);
// Print the document
varDoc.PrintOut(ref varTrueValue, ref varFalseValue, ref varMissing,
ref varMissing, ref varMissing, ref varMissing,
ref varMissing, ref varMissing, ref varMissing, ref varMissing,
ref varFalseValue, ref varMissing, ref varMissing,
ref varMissing, ref varMissing, ref varMissing, ref varMissing,
ref varMissing);
// Send mail with this document as an attachment
varDoc.SendMail();
}
catch (Exception varE)
{
MessageBox.Show("Error:\
" + varE.Message, "Error message");
}
}
You need to create references to this .NET namespace in order to use the above code:
Microsoft.Office.Interop.Word
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |