首先添加引用
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 | 可选 | Variant | True 显示 转换文件对话框中,如果该文件不是 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,上面有详细介绍。