打开窗体即可 OLE自动添加 你也可以把自动添加的代码去掉 使用你自己添加的OLE Private WithEvents Ole1 As OLE Private WithEvents Ole2 As OLE Private WithEvents Command1 As CommandButton Private WithEvents Command2 As CommandButton Dim fname$ Private Sub Form_Load() Set Ole1 = Controls.Add("vb.ole", "Ole1") Set Command1 = Controls.Add("VB.CommandButton", "command1") Set Command2 = Controls.Add("VB.CommandButton", "command2") Command1.Visible = True Command2.Visible = True Me.Move (Screen.Width - Me.Width) / 2, (Screen.Height - Me.Height) / 2 Command1.Move (Me.ScaleWidth - Command1.Width - 100 - Command2.Width) \ 2, (Me.ScaleHeight - Command1.Height) Command2.Move Command1.Left + Command1.Width + 100, Command1.Top Command1.Caption = "打开 Word" Command2.Caption = "打开 Excel" End SubPrivate Sub Command1_Click() On Error GoTo errhandler fname = "c:\tt.doc" If Dir(fname) <> "" Then Ole1.SourceDoc = fname Ole1.Action = 1 '联结 Ole1.DoVerb '打开 Else MsgBox fname & " 不存在!!" End If errhandler: If Err > 0 Then MsgBox "没安装 Word" End Sub
Private Sub Command2_Click() On Error GoTo errhandler fname = "c:\tt.xls" If Dir(fname) <> "" Then Ole1.SourceDoc = fname Ole1.Action = 1 '联结 Ole1.DoVerb '打开 Else MsgBox fname & " 不存在!!" End If errhandler: If Err > 0 Then MsgBox "没安装 Excel" End Sub
这个问题我已经解决。 代码如下,供有同样问题的朋友参考。 Case 3 '保存数据 FileNum = FreeFile '打开要保存的文件。 Open "TEST.OLE" For Binary As #FileNum '保存文件。 OLE1.SaveToFile FileNum '关闭文件。 Close #FileNum MsgBox "文件保存成功!" Case 4 '导入数据 '取文件号。 FileNum = FreeFile '打开文件。 Open "TEST.OLE" For Binary As #FileNum '读文件。 OLE1.ReadFromFile FileNum '关闭二进制文件。 Close #FileNum OLE1.DoVerb Set newxls = OLE1.object.Application '导入数据后,newxls、newsheet重新“绑定” Set newsheet = newxls.Worksheets(1)
这个问题我已经解决。代码供有同样问题的朋友参考: Case 3 '保存数据 FileNum = FreeFile '打开要保存的文件。 Open "TEST.OLE" For Binary As #FileNum '保存文件。 OLE1.SaveToFile FileNum '关闭文件。 Close #FileNum MsgBox "文件保存成功!" Case 4 '导入数据 '取文件号。 FileNum = FreeFile '打开文件。 Open "TEST.OLE" For Binary As #FileNum '读文件。 OLE1.ReadFromFile FileNum '关闭二进制文件。 Close #FileNum OLE1.DoVerb Set newxls = OLE1.object.Application '导入数据后,newxls、newsheet重新“绑定” Set newsheet = newxls.Worksheets(1)
Private WithEvents Ole1 As OLE
Private WithEvents Ole2 As OLE
Private WithEvents Command1 As CommandButton
Private WithEvents Command2 As CommandButton
Dim fname$
Private Sub Form_Load()
Set Ole1 = Controls.Add("vb.ole", "Ole1")
Set Command1 = Controls.Add("VB.CommandButton", "command1")
Set Command2 = Controls.Add("VB.CommandButton", "command2")
Command1.Visible = True
Command2.Visible = True
Me.Move (Screen.Width - Me.Width) / 2, (Screen.Height - Me.Height) / 2
Command1.Move (Me.ScaleWidth - Command1.Width - 100 - Command2.Width) \ 2, (Me.ScaleHeight - Command1.Height)
Command2.Move Command1.Left + Command1.Width + 100, Command1.Top
Command1.Caption = "打开 Word"
Command2.Caption = "打开 Excel"
End SubPrivate Sub Command1_Click()
On Error GoTo errhandler
fname = "c:\tt.doc"
If Dir(fname) <> "" Then
Ole1.SourceDoc = fname
Ole1.Action = 1 '联结
Ole1.DoVerb '打开
Else
MsgBox fname & " 不存在!!"
End If
errhandler:
If Err > 0 Then MsgBox "没安装 Word"
End Sub
Private Sub Command2_Click()
On Error GoTo errhandler
fname = "c:\tt.xls"
If Dir(fname) <> "" Then
Ole1.SourceDoc = fname
Ole1.Action = 1 '联结
Ole1.DoVerb '打开
Else
MsgBox fname & " 不存在!!"
End If
errhandler:
If Err > 0 Then MsgBox "没安装 Excel"
End Sub
代码如下,供有同样问题的朋友参考。
Case 3 '保存数据
FileNum = FreeFile
'打开要保存的文件。
Open "TEST.OLE" For Binary As #FileNum
'保存文件。
OLE1.SaveToFile FileNum
'关闭文件。
Close #FileNum
MsgBox "文件保存成功!"
Case 4 '导入数据
'取文件号。
FileNum = FreeFile
'打开文件。
Open "TEST.OLE" For Binary As #FileNum
'读文件。
OLE1.ReadFromFile FileNum
'关闭二进制文件。
Close #FileNum
OLE1.DoVerb
Set newxls = OLE1.object.Application '导入数据后,newxls、newsheet重新“绑定”
Set newsheet = newxls.Worksheets(1)
Case 3 '保存数据
FileNum = FreeFile
'打开要保存的文件。
Open "TEST.OLE" For Binary As #FileNum
'保存文件。
OLE1.SaveToFile FileNum
'关闭文件。
Close #FileNum
MsgBox "文件保存成功!"
Case 4 '导入数据
'取文件号。
FileNum = FreeFile
'打开文件。
Open "TEST.OLE" For Binary As #FileNum
'读文件。
OLE1.ReadFromFile FileNum
'关闭二进制文件。
Close #FileNum
OLE1.DoVerb
Set newxls = OLE1.object.Application '导入数据后,newxls、newsheet重新“绑定”
Set newsheet = newxls.Worksheets(1)