Option Explicit Dim Doc As New Document Dim Visi As Boolean'拼写检查 Private Sub Command1_Click() Form1.Caption = "拼写检查" Doc.Range.Text = Text1 '确定范围 Doc.Application.Visible = True '将Word 97变为可见 AppActivate Doc.Application.Caption '激活Word 97 Call CheckSpell(Text1.Text) '拼写检查 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 SubPrivate 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 SubFunction CheckSpell(IncorrectText As String) As String Dim Word As Object, retText$ On Error Resume Next '建立对象并打开 WORD Set Word = CreateObject("Word.Basic") '把需要检查的 STRING 放到 WORD Word.AppShow Word.FileNew Word.Insert IncorrectText '运行 WORD 拼写检查 Word.ToolsSpelling Word.EditSelectAll '取返回值 retText = Word.Selection$() CheckSpell = Left$(retText, Len(retText) - 1) '关闭文件并回到 VB 应用 Word.FileClose 2 Show Set Word = Nothing End Function Private Sub Command1_Click() Dim xObject As Object '调用Excel 97 Set xObject = CreateObject("Excel.Sheet") Set xObject = xObject.Application.ActiveWorkbook.ActiveSheet xObject.Range("A1").Value = Text1.Text '将数值送入Excel 97的A1单元格 xObject.Range("A2").Value = Text2.Text '将数值送入Excel 97的A2单元格 xObject.Range("A3").Formula = "=MAX(A1,A2)" '将最大值送入A3单元格 xObject.Range("A4").Formula = "=ATAN(A1/A2)*180/PI()" '将反正切值送入A4单元格 '显示计算的结果 Label1.Caption = xObject.Range("A3").Value Label2.Caption = xObject.Range("A4").Value Set xObject = Nothing End SubPrivate Sub Form_Load() Text1.Text = "" Text2.Text = "" Label1.Caption = "" Label2.Caption = "" End Sub
如果要操作Word,Excel,一般是引用相应的库,然后定义对象,通过对象提供的属性,方法操作即可
Dim Doc As New Document
Dim Visi As Boolean'拼写检查
Private Sub Command1_Click()
Form1.Caption = "拼写检查"
Doc.Range.Text = Text1
'确定范围
Doc.Application.Visible = True '将Word 97变为可见
AppActivate Doc.Application.Caption
'激活Word 97
Call CheckSpell(Text1.Text) '拼写检查
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 SubPrivate 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 SubFunction CheckSpell(IncorrectText As String) As String
Dim Word As Object, retText$
On Error Resume Next
'建立对象并打开 WORD
Set Word = CreateObject("Word.Basic")
'把需要检查的 STRING 放到 WORD
Word.AppShow
Word.FileNew
Word.Insert IncorrectText
'运行 WORD 拼写检查
Word.ToolsSpelling
Word.EditSelectAll
'取返回值
retText = Word.Selection$()
CheckSpell = Left$(retText, Len(retText) - 1)
'关闭文件并回到 VB 应用
Word.FileClose 2
Show
Set Word = Nothing
End Function
Private Sub Command1_Click()
Dim xObject As Object
'调用Excel 97
Set xObject = CreateObject("Excel.Sheet")
Set xObject = xObject.Application.ActiveWorkbook.ActiveSheet
xObject.Range("A1").Value = Text1.Text
'将数值送入Excel 97的A1单元格
xObject.Range("A2").Value = Text2.Text
'将数值送入Excel 97的A2单元格
xObject.Range("A3").Formula = "=MAX(A1,A2)"
'将最大值送入A3单元格
xObject.Range("A4").Formula = "=ATAN(A1/A2)*180/PI()"
'将反正切值送入A4单元格
'显示计算的结果
Label1.Caption = xObject.Range("A3").Value
Label2.Caption = xObject.Range("A4").Value
Set xObject = Nothing
End SubPrivate Sub Form_Load()
Text1.Text = ""
Text2.Text = ""
Label1.Caption = ""
Label2.Caption = ""
End Sub