.NET操作Word的打开保存时如何隐藏Word

oowgsoo 2015-03-24 06:53:47
在用.Net操作Word的打开时,即使设置了app.Application.Visible = false;Word在打开时仍然是可以看到了,导致操作会闪一下,很不爽,如何才能隐藏Word呢?

.NET Framework版本3.5
引用的Microsoft.Office.Interop.Word版本为12.0.0.0


using Word = Microsoft.Office.Interop.Word;

private void button1_Click(object sender, EventArgs e)
{
try
{
String srcPath = @"d:\a.doc";
String dstPath = @"d:\b.doc";

Object fileFormat = Missing.Value;
Object saveChanges = Missing.Value;
Output(srcPath, dstPath, ref fileFormat, ref saveChanges);

MessageBox.Show("计算书保存成功!");
}
catch (System.Exception ex)
{
}
}

private void Output(String srcPath, String dstPath,
ref Object fileFormat, ref Object saveChanges)
{
Object missing = Missing.Value;
Word.Application app = null;

try
{
app = new Word.Application();

// To Invisible the word document
app.Application.Visible = false;
app.Application.ScreenUpdating = false;

// Minimize the opened word document
app.WindowState = Word.WdWindowState.wdWindowStateMinimize;

Object srcPath_ = srcPath;
Word.Document doc = app.Documents.Open(
ref srcPath_, // FileName
ref missing, // ConfirmConversions
ref missing, // ReadOnly
ref missing, // AddToRecentFiles
ref missing, // PasswordDocument
ref missing, // PasswordTemplate
ref missing, // Revert
ref missing, // WritePasswordDocument
ref missing, // WritePasswordTemplate
ref missing, // Format
ref missing, // Encoding
ref missing, // Visible
ref missing, // OpenAndRepair
ref missing, // DocumentDirection
ref missing, // NoEncodingDialog
ref missing); // XMLTransform

Object savePath_ = dstPath;
doc.SaveAs(
ref savePath_, // FileName
ref fileFormat, // FileFormat
ref missing, // LockComments
ref missing, // Password
ref missing, // AddToRecentFiles
ref missing, // WritePassword
ref missing, // ReadOnlyRecommended
ref missing, // EmbedTrueTypeFonts
ref missing, // SaveNativePictureFormat
ref missing, // SaveFormsData
ref missing, // SaveAsAOCELetter
ref missing, // Encoding
ref missing, // InsertLineBreaks
ref missing, // AllowSubstitutions
ref missing, // LineEnding
ref missing); // AddBiDiMarks
}
finally
{
if (app != null)
{
app.Documents.Close(
ref saveChanges, // SaveChanges
ref missing, // OriginalFormat
ref missing); // RouteDocument

((Word._Application)app).Quit(
ref saveChanges, // SaveChanges
ref missing, // OriginalFormat
ref missing); // RouteDocument

app = null;
}
}
}
...全文
103 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
kevinmartin 2015-03-26
  • 打赏
  • 举报
回复
我提供一个歪道道 尝试用另外一个用户进行word操作,这样就不会在你的界面上打开了。

110,525

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧