Close command window

esskaykay

Registered User.
Local time
Today, 13:11
Joined
Mar 8, 2003
Messages
267
Hello,

Is there a VBA command/code to close a command window? I have a line that maps a network drive then performs some functions and disconnects the mapped drive (I tried coding via UNC path but that bombs).

Shell "net use V: \\ce_laptop2\sidewalks /y"

A temporary command window opens and drive is connected. It works fine as long as the laptop is connected. However, if it’s not connected, the command window does not close. I can manually close it but would like a way to automatically do so.

Any suggestions?

Thanks,
SKK
 
Hello,

Is there a VBA command/code to close a command window? I have a line that maps a network drive then performs some functions and disconnects the mapped drive (I tried coding via UNC path but that bombs).

Shell "net use V: \\ce_laptop2\sidewalks /y"

A temporary command window opens and drive is connected. It works fine as long as the laptop is connected. However, if it’s not connected, the command window does not close. I can manually close it but would like a way to automatically do so.

Any suggestions?

Thanks,
SKK


Sounds like you need some error handling.

Maybe you could try to test to see if the resource exists before trying to map it.
 
Instead of the Shell command, you can use the Network object to map and disconnect the drive. By this means you can also trap any errors. Example:
Code:
[COLOR="Navy"]Dim[/COLOR] objNetwork [COLOR="navy"]As Object

Set[/COLOR] objNetwork = CreateObject("Wscript.Network")

[COLOR="navy"]On Error Resume Next[/COLOR]
objNetwork.MapNetworkDrive "V:", "\\ce_laptop2\sidewalks"
[COLOR="navy"]If[/COLOR] Err.Number = 0 [COLOR="navy"]Then[/COLOR]

    [COLOR="DarkGreen"]' ---------------------
    ' Insert your code here
    ' ---------------------[/COLOR]

    objNetwork.RemoveNetworkDrive "V:"
[COLOR="navy"]End If

Set[/COLOR] objNetwork = [COLOR="navy"]Nothing[/COLOR]
 
Last edited:
I'm not quite there yet but I think you sent me in the right direction. I'll look at it a bit more tomorrow and let you know what happens.

Thank you for your suggestion.
SKK
 
I’m still having a problem running the code if the laptop is not connected (works great when connected). It hangs for about 70 seconds then
“Run-time error – The specified network name is no longer available”
END - DEBUG - HELP

Here’s where I’d rather have a message appear MsgBox “Laptop not connected”
I remarked out the On Error line to see where it was bombing – on line objNetwork.MapNetworkDrive.

Thanks,
SKK
 
The code I provided should be sufficient to trap the errors. If you could post your code here, we could better determine why it is failing.
 
Last edited:
It’s still not doing what I want. I remarked out the On Error just to see what was happening -- not permanently removing it. I placed some test messages in the “Insert Code” area (see below). As long as the laptop is connected it works fine. My problem is when the laptops not there the routine does nothing (i.e., I don’t see my NOPE message). If I wait about a minute I receive “Run-time error – The specified network name is no longer available.” or sometime “Run-time error – Network path not found.”. Debug takes me to the objNetwork.MapNetworkDrive line.

I did notice that at the end, it’s not removing the mapped drive (I changed the drive to K). With the On Error remarked out, I could see it bombed on objNetwork.RemoveNetworkDrive. As a test, if I place the code < Shell "net use K: /delete /y" > at the end, it did remove the mapped drive.

I think you understand what I’m trying to do. If the CE_Laptop2 is connected to the network, run a routine otherwise tell me it’s not available.


I placed the following code to a command button:

Dim objNetwork As Object
Set objNetwork = CreateObject("Wscript.Network")
On Error Resume Next
objNetwork.MapNetworkDrive "K:", "\\ce_laptop2\sidewalks"
If Err.Number = 0 Then

' ---------------------
' Insert your code here
' This is just my test

If Dir ("K:\swk_inspect.mdb") = "" Then
MsgBox "NOPE - Can't find the Laptop..."
Else
MsgBox "YUP - Laptop's there..."
End If
' ---------------------

objNetwork.RemoveNetworkDrive "K:"
End If
Set objNetwork = Nothing

Thanks again,
SKK
 
I may have answered my problem. Here's what I've got now.

Dim objNetwork As Object
Set objNetwork = CreateObject("Wscript.Network")

On Error GoTo Err_Handler
objNetwork.MapNetworkDrive "K:", "\\ce_laptop2\sidewalks"

' ---------------------
MsgBox "YUP - Laptop's there..."
GoTo GoodBye
' ---------------------

Err_Handler:
MsgBox "Could not locate file(s) - please verify network connection..."

GoodBye:
Shell "net use K: /delete /y"
 
Try:
Code:
[COLOR="Navy"]Dim[/COLOR] objNetwork [COLOR="navy"]As Object

Set[/COLOR] objNetwork = CreateObject("Wscript.Network")

[COLOR="navy"]On Error Resume Next[/COLOR]
objNetwork.MapNetworkDrive "K:", "\\ce_laptop2\sidewalks"
[COLOR="navy"]If[/COLOR] Err.Number = 0 [COLOR="navy"]Then[/COLOR]

    [COLOR="DarkGreen"]' ---------------------
    ' Insert your code here
    ' This is just my test[/COLOR]

    [COLOR="Navy"]If[/COLOR] Dir("K:\", vbDirectory) = "" [COLOR="navy"]Then[/COLOR]
        MsgBox "NOPE - Can't find the Laptop..."
    [COLOR="navy"]Else[/COLOR]
        MsgBox "YUP - Laptop's there..."
    [COLOR="navy"]End If[/COLOR]
    [COLOR="DarkGreen"]' ---------------------[/COLOR]

    objNetwork.RemoveNetworkDrive "K:", [COLOR="Navy"]True
    [COLOR="DarkGreen"]' second parameter set to True ensures that the
    ' drive-mapping is removed from the User Profile[/COLOR]
Else[/COLOR]
    MsgBox "NOPE - Can't find the Laptop..."
[COLOR="navy"]End If

Set[/COLOR] objNetwork = [COLOR="navy"]Nothing[/COLOR]
 
Thank you very much. I have another app I can use this on. Will look at tommorow.

Greatly appreciated,
SKK
 

Users who are viewing this thread

Back
Top Bottom