2014-10-31 2 views
2

아래 VBA 코드는 파일에 대한 사용자 액세스 권한을 부여하기 위해 REST API를 호출합니다. REST API가 허용을 반환하면 코드가 정상적으로 닫히고 액세스 권한을 부여해야합니다. REST API가 다시 실행되지 않으면 프로그램에서 사용자와 통합 문서를 알려야합니다. 인터넷에 액세스 할 수없는 경우 사용자에게 알림을 보내고 책을 닫아야합니다.Excel VBA 매크로에서 REST API 처리 처리

제 질문은 어떻게 매크로를 올바르게 처리 할 수 ​​있도록 REST API 응답을 처리하여 URL에서 응답을 허용하지 않으므로 정상적으로 종료 할 것인가? 여기

Private Sub Workbook_activate() 

Application.EnableCancelKey = xlDisabled 

' Run the Error handler "ErrHandler" when an error occurs. 

On Error GoTo Errhandler 


ActiveWorkbook.FollowHyperlink Address:="https://mysite.com/licensing/getstatus.php?", NewWindow:=True 

' If response is allow 

' Exit the macro so that the error handler is not executed. 

****what goes here??**** 

     Exit Sub 

' If response is disallow 

****what goes here??**** 

     MsgBox "Your license key is not valid. Please check your key or contact customer service." 

     ActiveWorkbook.Close SaveChanges:=False 


Errhandler: 

     ' If no Internet Access, display a message and end the macro. 
     MsgBox "An error has occurred. You need Internet access to open the software." 

     ActiveWorkbook.Close SaveChanges:=False 

End Sub 
+0

.FollowHyperlink() 메서드로 작업해야한다고 생각하지 않습니다. 아마도 웹 요청을 VBA 코드에서 분리 할 것이기 때문입니다. HttpWebRequest 및 HttpWebResponse 객체를 조사해보십시오 ... – Recipe

답변

2

간단한 HttpWebRequest를위한 예 :

: 프록시 뒤에있는 경우

Dim oRequest As Object 
Set oRequest = CreateObject("WinHttp.WinHttpRequest.5.1") 
oRequest.Open "GET", "https://mysite.com/licensing/getstatus.php?" 
oRequest.Send 
MsgBox oRequest.ResponseText 

은이 같은 것을 사용할 수 있습니다 여기에

지금까지 VBA 코드
Const HTTPREQUEST_PROXYSETTING_PROXY = 2 
Dim oRequest As Object 
Set oRequest = CreateObject("WinHttp.WinHttpRequest.5.1") 
oRequest.setProxy HTTPREQUEST_PROXYSETTING_PROXY, "http://proxy.intern:8080" 
oRequest.Open "GET", "https://mysite.com/licensing/getstatus.php?" 
oRequest.Send 
MsgBox oRequest.ResponseText 

그리고 GET 메서드 대신 POST를 사용하여 웹 서버에 일부 값을 전달하려는 경우에는 s :

Dim oRequest As Object 
Set oRequest = CreateObject("WinHttp.WinHttpRequest.5.1") 
oRequest.Open "POST", "https://mysite.com/licensing/getstatus.php" 
oRequest.SetRequestHeader "Content-Typ", "application/x-www-form-urlencoded" 
oRequest.Send "var1=123&anothervar=test" 
MsgBox oRequest.ResponseText