Pass String To Dos Environment

This tip uses the clipboard to pass String from Visual Basic to Dos environment, like if the user has typed the string in the Command Prompt and pressed enter.


Add 2 Command Buttons to your form.

Press the first button to Open a new DOS window.
Press the second to pass the string to the DOS environment.

Form Code

Private Sub Command1_Click()
' will open a new maximized DOS window
    Shell (""), vbMaximizedFocus
End Sub

Private Sub Command2_Click()
' clear the clipboard
' copy the string you want to pass to the clipboard, including
' the Enter key (Chr$(13) = Enter Key)
' replace the "Dir *.*" below with the string you want to pass.

    Clipboard.SetText "Dir *.*" + Chr$(13)
' set the focus to the Dos window. "MS-DOS Prompt" is the title
' of the DOS window. If your Dos window has other Title (The title
' is what written in the DOS window's title bar), replace the "MS-DOS Prompt"
' below with your DOS window's title.

    AppActivate "MS-DOS Prompt"
' use the "SendKeys" function to send the string from the clipboard to
' the DOS environment

    SendKeys "% ep", 1
End Sub

Go Back