VBA CreateObject

前端 未结 2 843
闹比i
闹比i 2021-02-06 12:02

I am stranded on this code line since 10th of January where i got it in an email and i found out i had to learn class modules so i did and returned to ask on a new basis now. Th

2条回答
  •  闹比i
    闹比i (楼主)
    2021-02-06 12:46

    Dim WinHttpReq As Object
    Set WinHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")
    

    is almost same as

    Dim WinHttpReq As WinHttpRequest
    Set WinHttpReq = New WinHttpRequest
    

    The difference is that with first approach you do not have to include the library in the "References" list, but as a price to pay you will not have intllisense hints in the IDE because your reference is generic Object.

    Second form is preferable. It allows VB to check object types for compatibility as you assing them or pass them as parameters. It also give you hints which methods and properties the object has as you type its name.

提交回复
热议问题