Excel应用与操作
VB5.0调用Office97技巧
2005-02-15 11:46:46
Visual Basic 5.0具有简单、易学等特点,深受初学者的欢迎,但也有一些不如人意的地方。例如,不能像Word 97那样进行拼写检查,不能像Excel 97那样具有很多的函数。如果我们能将Word 97和Exce l97的功能使用在Visual Basic 5.0中,就可以使VB达到锦上添花的目的。  我们知道所有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 VB编程乐园版权所有©,1999-2000注:此法在OFFICE更好版本中也可以使用,只需把工程引用Microsoft Word 8.0 Object Library改为Microsoft Word 9.0 Object Library或更高版本即可摘自:主题花园