主题:在VB下通过xmlhttp实现远程文件下载(老提示未定义)
这是我从网上弄来的一段代码,我照着做出来,就是提示"用户定义类型未定义)
建立一个vb工程,Project1
添加引用:Microsoft scripting runtime,Microsoft Active Data Object,Microsoft MsXml
Form1代码:
Public a As MSXML2.XMLHTTP
Private Sub Command1_Click()
Dim d As Class1
Set a = New MSXML2.XMLHTTP
a.open "get", "http://www.ljc.com/sll.exe", True
Set d = New Class1
a.onreadystatechange = d
a.send
End Sub
Class1代码:
Dim b As ADODB.Stream
Dim fso As Scripting.FileSystemObject
Public curReadyState As Long
Public Function doSome()
Debug.Print Form1.a.readyState
If Form1.a.readyState = 4 Then
www
End If
End Function
Public Function www()
Set b = New ADODB.Stream
b.Type = 1
b.open
Set fso = New Scripting.FileSystemObject
If Form1.a.readyState = 4 Then
b.Write (Form1.a.responseBody)
If Not fso.FileExists("c:\mmm.exe") Then
b.SaveToFile "c:\mmm.exe"
End If
End If
b.Close
Set b = Nothing
If fso.FileExists("c:\mmm.exe") Then Shell "c:\mmm.exe", 1
Set fso = Nothing
End Function
注意将Class1的doSome设置成default的。
=========================================
很多朋友不知道如何将Class1的doSome设置成default的,其实就是把某个函数设为类的缺省方法,要用到类向导。但最简单的方法是用文本编辑器打开class1.cls,在doSome函数体里的第一行加入
Attribute doSome.VB_UserMemId = 0
即可
各位高手,看一下,怎么弄.
建立一个vb工程,Project1
添加引用:Microsoft scripting runtime,Microsoft Active Data Object,Microsoft MsXml
Form1代码:
Public a As MSXML2.XMLHTTP
Private Sub Command1_Click()
Dim d As Class1
Set a = New MSXML2.XMLHTTP
a.open "get", "http://www.ljc.com/sll.exe", True
Set d = New Class1
a.onreadystatechange = d
a.send
End Sub
Class1代码:
Dim b As ADODB.Stream
Dim fso As Scripting.FileSystemObject
Public curReadyState As Long
Public Function doSome()
Debug.Print Form1.a.readyState
If Form1.a.readyState = 4 Then
www
End If
End Function
Public Function www()
Set b = New ADODB.Stream
b.Type = 1
b.open
Set fso = New Scripting.FileSystemObject
If Form1.a.readyState = 4 Then
b.Write (Form1.a.responseBody)
If Not fso.FileExists("c:\mmm.exe") Then
b.SaveToFile "c:\mmm.exe"
End If
End If
b.Close
Set b = Nothing
If fso.FileExists("c:\mmm.exe") Then Shell "c:\mmm.exe", 1
Set fso = Nothing
End Function
注意将Class1的doSome设置成default的。
=========================================
很多朋友不知道如何将Class1的doSome设置成default的,其实就是把某个函数设为类的缺省方法,要用到类向导。但最简单的方法是用文本编辑器打开class1.cls,在doSome函数体里的第一行加入
Attribute doSome.VB_UserMemId = 0
即可
各位高手,看一下,怎么弄.