Home > database >  How do I make VBA code open local CMD rather than one note?
How do I make VBA code open local CMD rather than one note?

Time:04-06

I'm using the following vba to simply click on a button and open a command prompt window, however when I do so, it opens it up in onenote instead of locally. Why is this the default behavior? I was under the impression shell opened it up locally.

Private Sub CMD_PW2_Click()

Set oShell = CreateObject("Shell.Application")
oShell.ShellExecute "cmd.exe"

End Sub

The result of the above is that it opens up CMD pointing from onenote and not local C:\

CodePudding user response:

Can't you just use /k to process the request to change location?

  oShell.ShellExecute "cmd.exe /k C:", vbNormalFocus

The /k switch specifies that cmd should not terminate after dir has finished. You can terminate cmd later

CodePudding user response:

or you could do something like:

Private Sub CMD_PW2_Click()
    ChDrive "C"
    ChDir "C:\"
    CreateObject("Shell.Application").ShellExecute "cmd.exe"
End Sub

CodePudding user response:

I figured it out, I had to change it to a shortcut. If anyone ever deals with this, here is what I did:

Sub OpenFile()

  Dim FileName As Variant
  Dim FolderItem As Object
  Dim FolderPath As Variant
  Dim oFolder As Object
  Dim oShell As Object
  
    FolderPath = "C:\Documents and Settings\Admin.ADMINS\Desktop"
    FileName = "Solitaire.lnk"
    
      Set oShell = CreateObject("Shell.Application")
      Set FolderItem = oShell.Namespace(FolderPath).ParseName(FileName)
    
      FolderItem.Verbs.Item(0).DoIt
    
End Sub

This let me define the exact command prompt I wanted to open. I used Environ("UserName") to make it useable on other users' PCs since this shortcut path is global, just changes by user.

  • Related