c# 如何打开word文档,及判断是否已经打开

首先添加引用

Microsoft Office 14.0 Object Library
Microsoft Word 14.0 Object Library 

添加命名空间

using MSWord = Microsoft.Office.Interop.Word; 

添加定义

private MSWord.Application m_word;

添加事件

            m_word = new MSWord.Application();
            Object filename = "文档.docx";
            Object filefullname = System.Windows.Forms.Application.StartupPath + "文档.docx";
            Object confirmConversions = Type.Missing;
            Object readOnly = Type.Missing;
            Object addToRecentFiles = Type.Missing;
            Object passwordDocument = Type.Missing;
            Object passwordTemplate = Type.Missing;
            Object revert = Type.Missing;
            Object writePasswordDocument = Type.Missing;
            Object writePasswordTemplate = Type.Missing;
            Object format = Type.Missing;
            Object encoding = Type.Missing;
            Object visible = Type.Missing;
            Object openConflictDocument = Type.Missing;
            Object openAndRepair = Type.Missing;
            Object documentDirection = Type.Missing;
            Object noEncodingDialog = Type.Missing;
 
           //判断文档是否已经打开

            bool inUse = true;
            string fileName1 = filefullname.ToString();
            if (File.Exists(fileName1))
            {
                FileStream fs = null;
                try
                {
                    fs = new FileStream(fileName1, FileMode.Open, FileAccess.Read, FileShare.None);
                    inUse = false;
                }
                catch (Exception ex)
                {

                    MessageBox.Show("帮助文档已打开", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);       
                    return;
                }

                finally
                {
                    if (fs != null)
                    {
                        fs.Close();
                    }
                }
           
            }
            else
            {
                MessageBox.Show("帮助文档不存在", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            /////////////////

            try
            {
                 if(inUse == false)
                {
                    m_word.Documents.Open(ref filefullname,
                   ref confirmConversions, readOnly=true, ref addToRecentFiles,
                   ref passwordDocument, ref passwordTemplate, ref revert,
                   ref writePasswordDocument, ref writePasswordTemplate,
                   ref format, ref encoding, ref visible, ref openConflictDocument,
                   ref openAndRepair, ref documentDirection, ref noEncodingDialog
                   );

                    m_word.Visible = true;
                }
               
               
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("打开文档异常", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

**************************************************************************************************

在打开文档时,有很多参数Documents.Open(,,,),这些参数的具体含义是什么呢?

参数

参数
名称必需/可选数据类型说明
FileName必需Variant文档名(可包含路径)。
ConfirmConversions可选VariantTrue 显示 转换文件对话框中,如果该文件不是 Microsoft Word 格式。
ReadOnly可选Variant为 True,则以只读方式打开文档。 此参数不会覆盖已保存文档的只读推荐设置。 例如,如果在打开只读推荐设置的情况下保存文档,则将 ReadOnly 参数设置为 False 将不会导致文件以读/写方式打开。
AddToRecentFiles可选Variant 要将文件名添加到列表中最近使用的文件在 文件菜单的底部。
PasswordDocument可选Variant打开文档时所需的密码。
PasswordTemplate可选Variant打开模板时所需的密码。
Revert可选Variant控制如果 FileName 是打开文档的名称会进行什么操作。 为 True,则放弃对打开文档的任何未保存更改并重新打开文件。 为 False,则激活打开的文档。
WritePasswordDocument可选Variant用于保存文档更改的密码。
WritePasswordTemplate可选Variant用于保存模板更改的密码。
Format可选Variant用于打开文档的文件转换器。 可为以下 WdOpenFormat 常量之一。 默认值为 wdOpenFormatAuto。 若要指定外部文件格式,请将 OpenFormat 属性应用于 FileConverter 对象,以确定要与此参数一起使用的值。
Encoding可选Variant当你查看保存的文档时 Microsoft Word 所使用的文档编码(代码页或字符集)。 可以是任何有效的 MsoEncoding 常量。 要查看有效 MsoEncoding 常量的列表,请参阅“Visual Basic 编辑器”中的“对象浏览器”。 默认值是系统代码页。
Visible可选Variant如此 如果在可见窗口中打开文档。 默认值为 True 。
OpenConflictDocument可选Variant指定是否打开具有脱机冲突的文档的冲突文件。
OpenAndRepair可选Variant如果该属性为 True ,则修复文档,以防止文档毁坏。
DocumentDirection可选WdDocumentDirection表示文档中的横排文字。 默认值为 wdLeftToRight
NoEncodingDialog可选Variant为 True,如果无法识别文本编码,则跳过显示 Word 所显示的“编码”对话框。 默认值为 False

也可以参考https://docs.microsoft.com/zh-cn/office/vba/api/word.documents.open,上面有详细介绍。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值