VBA错误AcvtiveX组件无法创建对象

时间:2018-02-05 17:25:41

标签: excel vba excel-vba activexobject

我在尝试从random.org获取随机数时遇到上述错误。 错误发生在"设置objXMLHTTP = CreateObject(" MSXML2.ServerXMLHTTP.4.0")"。我没有很多从vba连接到网站的经验,所以我不确定我做错了什么。

Sub pick_random()
Dim x As Long
Dim randomInt As Integer

x = Worksheets(ActiveSheet.Name).Range("A:A").Cells.SpecialCells(xlCellTypeConstants).Count
randomInt = GetRndNos(1, 1, x)
randomStudentFirst = ThisWorkbook.Sheets(ActiveSheet.Name).Cells(randomInt, 1).Value
randomStudentLast = ThisWorkbook.Sheets(ActiveSheet.Name).Cells(randomInt, 2).Value
randomStudent = randomStudentFirst + " " + randomStudentLast

MsgBox "Selected: " & randomStudent
End Sub


Function GetRndNos(NUM As Long, MIN As Long, MAX As Long) As Variant
Dim objXMLHTTP As Object
Dim strURL As String
Dim strResp
Dim I As Long
strURL = "http://www.random.org/integers/"
strURL = strURL & "?num=" & NUM & "&min=" & MIN & "&max=" & MAX & "&col=1&base=10&format=plain&rnd=plain"
Debug.Print strURL
Set objXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP.4.0")

With objXMLHTTP
    .Open "GET", strURL, False
    .send

    strResp = .responseText

    GetRndNos = Split(strResp, Chr(10))

End With

Set objXMLHTTP = Nothing
End Function

感谢任何帮助,谢谢!

0 个答案:

没有答案