我们知道所有Office 97 应用程序都提供了ActiveX对象,我们可以在Visual Basic 5.0中使用它们。
调用Word 97
要想在VB中调用Word 97,首先要打开VB的“工程”菜单中的“引用”项目,并在该项目对话框中选定“Microsoft Word 8.0 Object Library”就可以了。
下面我们举例说明调用Word 97中的“拼写检查”和“单词统计”功能。首先新建一个工程并在窗体上放置一个TextBox控件和两个CommandButton控件,然后添入如下的代码:
Option Explicit
Dim Doc As New Document
Dim Visi As Boolean
图1 调用Word 97
′拼写检查
Private Sub Command1-Click()
Form1.Caption = “拼写检查"
Doc.Range.Text = Text1
′确定范围
Doc.Application.Visible = True ′将Word 97变为可见
AppActivate Doc.Application.Caption
′激活Word 97
Doc.Range.CheckSpelling
′拼写检查
Text1 = Doc.Range.Text
Text1 = Left(Text1, Len(Text1) - 1)
AppActivate Caption
End Sub
′统计单词数
Private Sub Command2-Click()
Dim Dlg As Word.Dialog
Doc.Range = Text1.Text
Set Dlg = Doc.Application.Dialogs
(wdDialogDocumentStatistics)
Dlg.Execute ′统计单词和字符
Form1.Caption =“单词数:" & Str(Dlg.Words) & “词"
& Str(Dlg.Characters) & “字符" ′显示统计结果
End Sub
Private Sub Form-Load()
Form1.Caption =“调用Word 97"
Text1.Text = “"
Command1.Caption = “拼写检查"
Command2.Caption = “统计单词"
′使应用程序可见
Visi = Doc.Application.Visible
End Sub
′关闭应用程序
Private Sub Form-Unload(Cancel As Integer)
If Visi Then ′关闭文件
Doc.Close savechanges:=False
Else
Doc.Application.Quit savechanges:=False ′关闭 Word 97
End If
End Sub
上一篇: 利用OLE自动化解决ACESS97中文版报表生成器直线不能往下顺延的缺陷
下一篇: 使用VB实现Excel自动获取外部数据
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




