Program Excel cells to launch RDP session when double clicked

This macro will cause any cell in certain columns to launch MSTSC when you double-click the cell.
To use this, paste into the worksheet module code and edit the column headers to match your worksheet. This will mean it gets deleted whenever you delete the worksheet, though.
You can also use the 2nd technique below to put the major code into the workbook and call it from the worksheet.

Private Sub worksheet_beforedoubleclick(ByVal ThisTarget As Range, Cancel As Boolean)
Select Case Cells(1, ActiveCell.Column)
    Case "DNS HostnameSorted Up", "NetBIOS Hostname", "IP"
        Call Shell("\windows\system32\mstsc.exe /v:" & ThisTarget, vbNormalFocus)
    End Select
    Cancel = True
End Sub

Using a 2-step process has the advantage that the major code is in the workbook, and if you delete the worksheet you only lose the minor code stub.
Place this into each worksheet:

Private Sub worksheet_beforedoubleclick(ByVal ThisTarget As Range, Cancel As Boolean)
    ActiveWorkbook.MyWorkbook_beforedoubleclick ThisTarget, Cancel
End Sub

This code goes into the workbook

Public Sub MyWorkbook_beforedoubleclick(ByVal ThisTarget As Range, Cancel As Boolean)
Select Case Cells(1, ActiveCell.Column)
    ' These are the names of the headers for the desired columns
    Case "DNS HostnameSorted Up", "NetBIOS Hostname", "IP"
        Call Shell("\windows\system32\mstsc.exe /v:" & ThisTarget, vbNormalFocus)
    'any other double-click behaviors could be added here, such as ping, nslookup, etc
    End Select
    Cancel = True
End Sub


This gets better all the time!

Public Sub MyWorkbook_beforedoubleclick(ByVal ThisTarget As Range, Cancel As Boolean)
' ThisTarget holds the cell contents, such as the computer name or IP address
Select Case Cells(1, ActiveCell.Column)
    ' These are the names of the headers for the desired columns
    Case "DNS HostnameSorted Up", "NetBIOS Hostname"
        Call Shell("\windows\system32\mstsc.exe /v:" & ThisTarget, vbNormalFocus)
    Case "IP"
        strHighlight = "==================="
        strCommand = "echo " & strHighlight _
                                & "&echo   Ping: " & ThisTarget _
                                & "&ping " & ThisTarget _
                                & "&echo " & strHighlight _
                                & "&Echo Net View " & ThisTarget _
                                & "&net view \\" & ThisTarget _
                                & "&Pause"
        Call Shell("cmd /c " & strCommand, vbNormalFocus)
    'any other double-click behaviors could be added here, such as ping, nslookup, etc
    End Select
    Cancel = True
End Sub
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s