这里给大家介绍下如何运用VBA来操作word文档内容,打开关闭word文档。
一、什么是VBA
Visual Basic for Applications(VBA)是Visual Basic的一种宏语言,是微软开发出来在其桌面应用程序中执行通用的自动化(OLE)任务的编程语言。主要能用来扩展Windows的应用程式功能,特别是Microsoft Office软件。也可说是一种应用程式视觉化的Basic 脚本。最近要做一个计算机一级考试系统,需要进行对操作题进行判分。
二、C#打开word操作
//定义word对象
Word.Application W = new Word.Application();
Word.Document D = new Word.Document();
//找到文件
Object filename = "1.docx";
Object filefullname = @"F:\work\计算机一级考试系统\1\1.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;
for (int i = 1; i <= W.Documents.Count; i++)
{
String str = W.Documents[i].FullName.ToString();
if (str == filefullname.ToString())
{
MessageBox.Show("请勿重复打开该文档");
return;
}
}
try
{
W.Documents.Open(ref filefullname);
W.Visible = true;
}
catch (System.Exception ex)
{
MessageBox.Show("打开Word文档出错");
}
通过以上过程用C#将word类型的文档打开,以上就是全部操作。