A shell is an interface in Windows that executes commands and helps with the use of programs. That is why it is also called a command line interface (CLI). In Windows this is also called a command prompt.

You can also open the command line by typing cmd in the Run dialog box after pressing the Windows key on your keyboard.

In Windows 10 we can type it directly into the search box next to the Windows key.

alt=Pin it! data-ezsrc=/utilcave_com/social/pin_it.png />

alt=Facebook share data-ezsrc=/utilcave_com/social/fb_share.png />

Some examples of commands

Below you will find some assignments and results that may be of interest to you. If you are already familiar with the available command shell commands, you can skip the next section to learn how to call the shell from VBA.

Internal controls

The above commands are already part of the command line, so they are called internal commands.

Deere

This command displays the contents of the folder.

How to Open the Windows Shell from VBA, With Use Cases

alt=Pin it! data-ezsrc=/utilcave_com/social/pin_it.png />

alt=Facebook share data-ezsrc=/utilcave_com/social/fb_share.png />

Ip configuration

This command provides information about the network.

How to Open the Windows Shell from VBA, With Use Cases

alt=Pin it! data-ezsrc=/utilcave_com/social/pin_it.png />

alt=Facebook share data-ezsrc=/utilcave_com/social/fb_share.png />

System information

Provides all information about the system hardware.

How to Open the Windows Shell from VBA, With Use Cases

alt=Pin it! data-ezsrc=/utilcave_com/social/pin_it.png />

alt=Facebook share data-ezsrc=/utilcave_com/social/fb_share.png />

Ping

Use this command to check if the server is available.

How to Open the Windows Shell from VBA, With Use Cases

alt=Pin it! data-ezsrc=/utilcave_com/social/pin_it.png />

alt=Facebook share data-ezsrc=/utilcave_com/social/fb_share.png />

Work plan

This command provides an overview of all tasks in the system that are currently being performed.

How to Open the Windows Shell from VBA, With Use Cases

alt=Pin it! data-ezsrc=/utilcave_com/social/pin_it.png />

alt=Facebook share data-ezsrc=/utilcave_com/social/fb_share.png />

External controls

There are various programs in Windows that may or may not be included with the operating system. For example Notepad, MS Word, MS Excel and any other software we install specifically after the operating system has been installed.

All this can be triggered by external commands.

Block

How to Open the Windows Shell from VBA, With Use Cases

alt=Pin it! data-ezsrc=/utilcave_com/social/pin_it.png />

alt=Facebook share data-ezsrc=/utilcave_com/social/fb_share.png />

issued

How to Open the Windows Shell from VBA, With Use Cases

alt=Pin it! data-ezsrc=/utilcave_com/social/pin_it.png />

alt=Facebook share data-ezsrc=/utilcave_com/social/fb_share.png />

Changing a folder and opening a specific document from this folder

How to Open the Windows Shell from VBA, With Use Cases

alt=Pin it! data-ezsrc=/utilcave_com/social/pin_it.png />

alt=Facebook share data-ezsrc=/utilcave_com/social/fb_share.png />

Spinning other scripts with theshell

Below are other scripting languages that can be executed from the command line of Windows or the shell.

  1. Powershell
  2. Batch files with the .bat extension
  3. PERL
  4. Python
  5. Javascript

And so on and so on…

VBA shell function

VBA has a built-in feature called Shell that allows you to use the Windows shell and easily run any program.

Syntax

Pid=Shell ( , [ ] )

Where

  1. The name of the program is the name of an internal or external command or script.
  2. The window style describes the behavior of the next display window. This is an optional argument that may be one of the values in the following table (which are constants).
  3. Pid is a variable that contains the process identifier of the program started by the shell function. This is the value of the type of data returned in duplicate by the shell function. It can be used later to complete the program.

Pay attention: If the function is executed unsuccessfully, i.e. if the program is not initialized, the value 0 is returned and stored in the variable Pid.

Permanent Value Description
vbHide 0 The window is open and hidden. The focus is transferred to the hidden window.
vbNormalFocus 1 The window opens at the last position and size. The widow is visible and focuses on
vbMinimizedFocus 2 The window opens, but remains less sharp.
vbMaximizedFocus 3 The window is maximized by focusing.
vbNormalNoFocus 4 The window opens at the last position and size. The widow is visible, but not concentrated. Only the programme currently running retains its focus.
 
vbMinimizedNoFocus 6 The window opens and remains minimized. The current program retains its focus.

Wherever we send keystrokes, the focus remains, and all keystrokes that are pressed or sent go into the window with the focus. Si z. B. The notepad is focused, anything entered using the keyboard or the send keys is transferred to the notepad window.

Examples

1. Open the notebook window with normal focus and close it with the program ID.

Sub shell_demo()

Enter a variable with the process identifier and return
Dim Pr_id as a double number.

Use the shell function to open Notepad with normal focus
Pr_id = Shell(Notepad, vbNormalFocus)

Leave or close the notepad with the same process number as the one that was returned.
Pr_id = Shell(TaskKill /F /PID & Pr_id, vbHide)

Last Subsection

How to Open the Windows Shell from VBA, With Use Cases

alt=Pin it! data-ezsrc=/utilcave_com/social/pin_it.png />

alt=Facebook share data-ezsrc=/utilcave_com/social/fb_share.png />

2. The following code opens a notepad file that already contains some data. Therefore, the path to the file must also be specified in the parameters. The notebook opens with the maximum focus as specified.

Sub shell_demo2()

Enter a variable with the process identifier and return
Dim Pr_id as a double number.

Use the shell function to open the notepad with maximum focus
Pr_id = Shell (Notepad C:UsersLAKSHMI RAMAKRISHNANOneDriveDocumentssai invite.txt, vbMaximizedFocus).

Leave or close the notepad with the same process number as the one that was returned.
Pr_id = Shell(TaskKill /F /PID & Pr_id, vbHide)

Last Subsection

In some cases, Windows does not understand spaces between file names or file paths. In this case, double quotes must be used for the parameter.

Pr_id = Shell(notepad C:UsersLAKSHMI RAMAKRISHNANONDriveDocumentssai invite.txt, vbMaximizedFocus)

This is usually not a problem with Windows 10.

How to Open the Windows Shell from VBA, With Use Cases

alt=Pin it! data-ezsrc=/utilcave_com/social/pin_it.png />

alt=Facebook share data-ezsrc=/utilcave_com/social/fb_share.png />

Fault! File name not specified. 3. Call programmes with various extensions

Sub shell_demo3()

Enter the variables needed to store the returned process IDs
Dim Pr_id_none, Pr_id_word, Pr_id_acc, Pr_id1, Pr_id2 as double.

Open a simple notepad with the shell function
Pr_id_not = Shell(Notepad, vbMaximizedFocus)

Open a simple notepad with the shell function
Pr_id_word = Shell(winword, vbMaximizedFocus)

Open a simple notepad with the shell function
Pr_id_acc = Shell(MsAccess, vbMaximizedFocus)

Use the shell function to open the folder with maximum focus
Pr_id1 = Shell(Explorer.exe C:UsersLAKSHMI RAMAKRISHNANOneDriveDocumentsTraining, vbMaximizedFocus).

Use the shell function to open a non-Excel file with normal focus
Pr_id2 = Shell(Explorer.exe C:UsersLAKSHMI RAMAKRISHNANOneDriveDocuments5.png, vbNormalFocus)

Debug.Print Pr_id_not
Debug.Print Pr_id_word
Debug.Print Pr_id_acc
Debug.Print Pr_id1
Debug.Print Pr_id2

Last Subsection

How to Open the Windows Shell from VBA, With Use Cases

alt=Pin it! data-ezsrc=/utilcave_com/social/pin_it.png />

alt=Facebook share data-ezsrc=/utilcave_com/social/fb_share.png />

4. Call a batch file to start a text file with the shell function.

Sub shell_demo3()

a variable with the process ID returned by
Dim Pr_id_new to be declared as double

Use the shell function to open the notebook with normal focus
Pr_id_new = Shell(C:Lakshmiprocessme.bat C:Lakshmitextfile_run.bat, vbMaximizedFocus)

Leave or close the notepad with the same process number as the one that was returned.
Pr_id_new = Shell(TaskKill /F /PID & Pr_id_new, vbHide)

Last Subsection

Yeah, that’s a lot of quotes in the shell function parameter. Let me explain.

  1. A double quotation mark at the end of each , marks the beginning and end of a line referring to the name or path of the program to be executed.

(C:Lakshmiprocessme.bat C:Lakshmitextfile_run.bat)

  • The rest of the parameter (with a double quote at the beginning and one at the end) consists of 2 lines. One is the path to the batch file and the other is the path to the text file to be executed. As you may know, you must use double quotes twice in a row (to avoid the quotes).

(C:Lakshmiprocessme.bat C:Lakshmitextfile_run.bat)

The last line transferred to the shell:

C:Lakshmiprocessme.bat C:Lakshmitextfile_run.bat

Asynchronous version

The VBA code continues to function without waiting for the end of the assignment. So, if we need to check that the command is complete before executing the next command, we need to use the VBA sleep or pause instructions.

All the way to the z. B. Type something in Notepad, we can just open and type the shell function. However, if we need a list of the files in the directory, we must absolutely check that the execution is complete and then export the list to Excel or CSV. In this case, we can use the standby or standby operator to ensure that the job is fully executed.

Here is an example of a program that explains how maintenance should be performed.

Sub shell_demo_4()

Declaration of objects and variables
Dim sh_obj as object
Dim counter as integer

Assign all values
Set i.e._pr_id = None
Counter = 0
Set sh_obj = Create object (shell application)
Counter = sh_obj.Windows.Count
i.e. = Shell(C:ProgramInternet Explorerxplore.exe -nosessionmerging , -noframmerging, vbNormalFocus)

Wait until the window opens
Do Tot sh_obj.Windows.Count = count + 1
Loop

Set new process ID
: ie_pr_id = sh_obj.Windows(sh_obj.Windows.Count – 1)

Output of the new process ID
Debug.Print i.e. _pr_id

Last Subsection

It shows the program in the nearest window:

Internet Explorer

Completion

Built-in VBA shell function to execute commands in the same way as executing commands from the Windows command line. You can use it to open any window while running VBA macros, for example. For example for automation, web scraping or file comparison.

vba shell multiple commands,run cmd from excel,vba shell command not working,vba run shell command and wait,excel vba open command line,vba run command and get output,vba set focus to another application,excel vba switch between applications,vba bring window to front,vba appactivate workbook,appactivate excel file,vba alt-tab,shellexecute excel vba,shellexecute minimized,vb6 shellexecute command line arguments,apishellexecute,vba shell open file,shellexecute example,wscript.shell object,shell.application shellexecute,vba shell.application namespace,shell.application properties,objshell createobject shell application,vba shellapp namespace,vba shell invalid procedure call,vba run batch file and wait,excel vba run batch file,vba wait for process to finish,vba run batch file with parameters,vba shellandwait,method 'run' of object 'iwshshell3' failed,vba shell command,excel vba run external program,vba shell command return value,vba shellexecute,wscript.shell vba

You May Also Like

How To Fix Thumbnails Not Showing In Windows 10

The Windows thumbnail function makes it easy to recognize photos and movies…

A Complete Guide On Resolving The Issue of Phone Overheating

The phone can sometimes even get hot, whether it’s during a game…

How To Fix Windows 10 File Explorer Dark Theme Not Working

When it comes to customisation options, Windows 10 offers users a considerable…

Apex Legends Crashing PC Fix 2020

Various problems can occur during gambling. If you’re playing the new royal…