110,539
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 打开Word文档,
/// </summary>
/// <param name="openFileName">完整的文件名</param>
/// <param name="onlyRead">指定文件是否只读</param>
/// <param name="docIsVisible">确定新建的word文档是否可视</param>
public static void OpenWordFile(string openFileName, bool onlyRead, bool docIsVisible)
{
try
{
if (appWord == null)
{
appWord = new ApplicationClass();
}
if (File.Exists(openFileName))
{
appWord.Visible = true;
WordVisible = true;
object visible = true;
object fileName = openFileName;
object confirmConversions = false;//允许转换
object readOnly = onlyRead;//只读
object addToRecentFiles = false;//添加到最近打开的文档
object passWordDocument = System.Reflection.Missing.Value;
object passWordTemplate = System.Reflection.Missing.Value;
object revert = System.Reflection.Missing.Value;
object writePasswordDocument = System.Reflection.Missing.Value;
object writePasswordTemplate = System.Reflection.Missing.Value;
object format = System.Reflection.Missing.Value;
object encoding = System.Reflection.Missing.Value;
object Visible = docIsVisible;
object openAndRepair = System.Reflection.Missing.Value;
object documentDirection = System.Reflection.Missing.Value;
object noEncodingDialog = System.Reflection.Missing.Value;
object xmlTransform = System.Reflection.Missing.Value;
activeDoc = appWord.Documents.Open(ref fileName, ref confirmConversions, ref readOnly, ref addToRecentFiles, ref passWordDocument,
ref passWordTemplate, ref revert, ref writePasswordDocument, ref writePasswordTemplate, ref format, ref encoding,
ref Visible, ref openAndRepair, ref documentDirection, ref noEncodingDialog, ref xmlTransform);
activeDoc.Activate();
}
else
{
NewWordFile(openFileName, docIsVisible, true);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// 新建Word文档
/// </summary>
/// <param name="newFileName">完整的文件名</param>
/// <param name="docIsVisible">确定新建的word文档是否可视</param>
/// <param name="wordIsVisible">确定word应用程序是否可视</param>
public static void NewWordFile(string newFileName, bool docIsVisible, bool wordIsVisible)
{
try
{
if (appWord == null)
{
appWord = new ApplicationClass();
}
appWord.Visible = wordIsVisible;
WordVisible = wordIsVisible;
if (File.Exists(newFileName))
{//判断文件是否存在,若存在,则删除
File.Delete(newFileName);
}
object Nothing = System.Reflection.Missing.Value;
object documentType = Microsoft.Office.Interop.Word.WdNewDocumentType.wdNewBlankDocument;
object visible = docIsVisible;
activeDoc = appWord.Documents.Add(ref Nothing, ref Nothing, ref documentType, ref visible);
object fileName = newFileName;
object fileFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument;
object lockComments = false;
object passWord = System.Reflection.Missing.Value;
object addToRecentFiles = true;
object writePassWord = System.Reflection.Missing.Value;
object readOnlyRecommended = false;
object embedTrueTypeFonts = false;
object saveNativePictiureFormat = false;
object saveFormsData = false;
object saveAsAOCELetter = false;
object unknown = System.Reflection.Missing.Value;
//保存文档
activeDoc.SaveAs(ref fileName, ref fileFormat, ref lockComments, ref passWord, ref addToRecentFiles, ref writePassWord,
ref readOnlyRecommended, ref embedTrueTypeFonts, ref saveNativePictiureFormat, ref saveFormsData, ref saveAsAOCELetter,
ref unknown, ref unknown, ref unknown, ref unknown, ref unknown);
activeDoc.Activate();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}