调用Word97
要想在VB中调用Word97,首先要打开VB的“工程”菜单中的“引用”项目,并在该项目对话框中选定“MicrosoftWord8.0ObjectLibrary”就可以了。
下面我们举例说明调用Word97中的“拼写检查”和“单词统计”功能。首先新建一个工程并在窗体上放置一个TextBox控件和两个CommandButton控件,然后添入如下的代码:
Option Explicit
Dim Doc As NewDocument
Dim Visi As Boolean
′拼写检查
Private Sub Command1-Click()
Form1.Caption=“拼写检查"
Doc.Range.Text=Text1
′确定范围
Doc.Application.Visible=True′将Word97变为可见
AppActivateDoc.Application.Caption
′激活Word97
Doc.Range.CheckSpelling
′拼写检查
Text1=Doc.Range.Text
Text1=Left(Text1,Len(Text1)-1)
AppActivateCaption
End Sub
′统计单词数
Private Sub Command2-Click()
Dim Dlg As Word.Dialog
Doc.Range=Text1.Text
SetDlg=Doc.Application.Dialogs
(wdDialogDocumentStatistics)
Dlg.Execute′统计单词和字符
Form1.Caption=“单词数:"&Str(Dlg.Words)&“词"
&Str(Dlg.Characters)&“字符"′显示统计结果
End Sub
Private Sub Form-Load()
Form1.Caption=“调用Word97"
Text1.Text=“"
Command1.Caption=“拼写检查"
Command2.Caption=“统计单词"
′使应用程序可见
Visi=Doc.Application.Visible
End Sub
′关闭应用程序
Private Sub Form-Unload(CancelAsInteger)
If Visi Then′关闭文件
Doc.Closesavechanges:=False
Else
Doc.Application.Quitsavechanges:=False′关闭Word97
End If
End Sub
调用Excel97
使用ExcelSheet对象的技术可以处理可变数量的数据,并将结果最后送到你的VisualBasic应用程序中。例子如下:
新建一个工程并在新建的窗体上放置两个TextBox控件和一个CommandButton控件及六个Label控件。建设中的窗体如图2所示。
程序代码如下:
Option Explicit
Private Sub Command1-Click()
Dim xObject As Object
′调用Excel97
SetxObject=CreateObject(“Excel.Sheet")
SetxObject=xObject.Application.ActiveWorkbook.ActiveSheet
xObject.Range(“A1").Value=Text1.Text′将数值送入Excel97的A1单元格
xObject.Range(“A2").Value=Text2.Text′将数值送入Excel97的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
SetxObject=Nothing
End Sub
Private Sub Form_Load()
Text1.Text=“"
Text2.Text=“"
Label1.Caption=""
Label2.Caption=""
End Sub
该程序的作用是:首先在两个文本框(Text1和Text2)输入数值并分别将输入的数值送入Excel97的“A1”和“A2”单元格中,然后用Excel97中的“Max()”函数将计算的结果送入“A3”单元格中用反正切函数“ATAN()”进行计算并将结果送入“A4”单元格中,最后输出
评论 {{userinfo.comments}}
{{child.content}}
{{question.question}}
提交