Send Dos Commands

  • Simple but goodie. Send DOS Commands from VBA. This examples assumes you have the net send service running. SP2 disables this service by default. Obviously you can send any DOS command that you like, this is just a simple example.

    Sub Send_CMD_DOS()
    'DOS SHELL SEND TO VBA*******************
    strDOSCMD = "net send your_network_id hello world"
    sCmdLine = strDOSCMD
    dblRetVal = Shell(sCmdLine, vbHide) 'VBHIDE HIDES SHELL WINDOW (BLACK DOS WINDOW)
    End Sub

    [COLOR="DarkRed"][H1]Have an Excel-lent Day![/H1][/COLOR]

  • Re: Send Dos Commands

    Well, this is not really true.

    Shell <program name> executes a program, not a command line command. So


    executes that program, but it won't execute any command line instructions. Try


    and see what happens.

    Why you can launch net.exe without specifying the full path is because the containing folder %winpath%\system32\ is defined in your PATH environment variable. Same goes for all other Windows command line tools.

    However to send a command line instruction, you need to run cmd.exe with your instructions as parameter /c. For instance the following will create a text file contents.txt with the output of folder C: directory listings:

    Dim cmd As String 
    Dim ret As Double
    cmd =  "cmd.exe /c dir C: > C:\contents.txt"
    ret = Shell(cmd, vbHide)

    The cmd.exe will close itself after executing the command, so reading the returned pId will not really do you any good, so you could just as well disgard the return value

  • Re: Send Dos Commands

    I have been pawned. Your Geek power exceeds mine. LOL (In good fun)

    [COLOR="DarkRed"][H1]Have an Excel-lent Day![/H1][/COLOR]

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!