dr.cornwallis
Goto Top

VB Netzlaufwerk trennen und mappen klappt nicht so richtig

Liebe Gemeinde,


ich habe ein VB Script, diese soll schauen ob ein Laufwerksbuchstabe vorhanden ist, ist dieser vorhanden, dann trenne diesen und mappe den gleichen mit Pfad X.

Code:

'Verbinden eines Netzlaufwerkes  
If Auswahl = 1 Then
	If Filesystem.DriveExists("M:\") Then WshNetwork.RemoveNetworkDrive "M:",True,True  
	WshNetwork.MapNetworkDrive "M:", "\\Fileserver\AL_" & PLZG & "\TL\" & PLZ, True  
End If
If Auswahl = 2 Then

Es funktioniert einfach nicht... und jetzt kommts...
ändere ich den Buchstaben beim mapping (mapNetworkDrive), wird das "alte" NLW getrennt und das neue verbunden, bei gleichen Buchstaben passiert einfach gar nichts, auch keine Fehlermeldung oder ähnliches...

Was ich noch besonders komisch finde:

falls das trennen klappt(Buchstabe geändert) wird das NLW noch immer angezeigt, kann allerdings nicht mehr geöffnet werden, normalerweise sollte es doch einfach aus der GUI verschwinden, fehlt hier irgend ein Parameter? Ich möchte dass das NLW komplett verschwindet, so als ob man das ganze mit der Maus(Computer - Rechtsklick - NLW trennen) machen würde.

Bitte um Hilfe(VB Anfänger)!


DANKE AN ALLE!


Gruß

Dr.

Content-Key: 313072

Url: https://administrator.de/contentid/313072

Printed on: April 20, 2024 at 03:04 o'clock

Mitglied: 129813
129813 Aug 19, 2016 updated at 10:27:58 (UTC)
Goto Top
Place a small break between the disconnect and reconnect. This should solve the race condition of resusing the resources for the drive letter.
falls das trennen klappt(Buchstabe geändert) wird das NLW noch immer angezeigt, kann allerdings nicht mehr geöffnet werden,
This is the case sometimes, explorer simply does not get the change, close the explorer window or logoff then it's gone. Normal behavior.

Regards
Member: Dr.Cornwallis
Dr.Cornwallis Aug 19, 2016 at 10:29:20 (UTC)
Goto Top
Okay, is the "Timeout" command right?


Thanks!


Greets!
Mitglied: 129813
Solution 129813 Aug 19, 2016 updated at 10:33:25 (UTC)
Goto Top
Zitat von @Dr.Cornwallis:
Okay, is the "Timeout" command right?
In batch yes, not in vb, you can use a small sub to wait a number of seconds
Sub pause(t As Integer)
    start = Timer
    Do While Timer < start + t
        DoEvents
    Loop
End Sub
To make break of 1 second, then call:
pause 1
Member: Dr.Cornwallis
Dr.Cornwallis Aug 19, 2016 at 10:35:10 (UTC)
Goto Top
How can I add this between the disconnect and the mapping?

Thx
Member: Dr.Cornwallis
Dr.Cornwallis Aug 19, 2016 at 10:35:50 (UTC)
Goto Top
Sorry i got to go, ill check your replay @the evening.


Greets!
Mitglied: 129813
Solution 129813 Aug 19, 2016 updated at 10:37:04 (UTC)
Goto Top
Zitat von @Dr.Cornwallis:

How can I add this between the disconnect and the mapping?
?? I already wrote it above, place the sub inside your module and place the pause 2 call in between your mapping lines.
Member: Dr.Cornwallis
Dr.Cornwallis Aug 22, 2016 at 08:11:51 (UTC)
Goto Top
Hi,

i got a Little Problem with the Code, it says: expected ')' at the first line of the Sub, got no idea where to place it.

Thanks!


Greets!
Mitglied: 129813
129813 Aug 22, 2016 updated at 08:19:20 (UTC)
Goto Top
We can't see your current code face-sad, so you forgot a closing pharentisis somewhere...
Member: Dr.Cornwallis
Dr.Cornwallis Aug 22, 2016 at 08:24:23 (UTC)
Goto Top
yes it says at the first line of your timer-code, i cant insert the full script because there are many Server names from our Company and stuff, but ill Change the values, so it must be no Problem, gimme a second.
Member: Dr.Cornwallis
Dr.Cornwallis Aug 22, 2016 at 08:29:14 (UTC)
Goto Top
Dim WshNetwork
Dim Filesystem
Dim Programmstart
Dim Server
Dim Link
Set Filesystem = CreateObject("Scripting.FileSystemObject")  
Set WshNetwork = WScript.CreateObject("WScript.Network")  
Set Programmstart = CreateObject("WScript.Shell")  



Antwort = MsgBox("bla!!" & VbCrLf & VbCrLf & "bla???", vbYesNo, "bla?")  
If Antwort = vbYes Then


MeldungPlz = False
Do
	PLZ = InputBox("BLA:", "BLA", 0)  
	PLZ = CInt(PLZ)
	If IsNumeric(PLZ) Then
		If PLZ > 4009 And PLZ < 8991 Then
			MeldungPlz = True
		End If
	End If 
Loop Until MeldungPlz = True

Antwort = MsgBox("blabla" & VbCrLf & VbCrLf & "bla", vbOK, "Vielen Dank!")  
If Antwort = vbOK Then

'Abfrage der Postleitzahl  
MeldungPlzg = False
Do
	PLZG = InputBox("bla", "Zustellbasis", 0)  
	PLZG = CInt(PLZG)
	If IsNumeric(PLZG) Then
		If PLZG > 4009 And PLZG < 8991 Then
			MeldungPlzg = True
		End If
	End If 
Loop Until MeldungPlzg = True

MeldungAuswahl = False
Do
	Auswahl = InputBox("Treffen Sie eine Installationsauswahl:" & VbCrLf & VbCrLf & "1  =  bla " & PLZ & VbCrLf & "2  =  Beenden" & VbCrLf, 0)  
	Auswahl = CInt(Auswahl)
	If IsNumeric(Auswahl) Then
		If Auswahl > 0 And Auswahl < 2 Then
			MeldungAuswahl = True
		End If
	End If 
Loop Until MeldungAuswahl = True
	

'Verbinden eines Netzlaufwerkes  
If Auswahl = 1 Then
	If Filesystem.DriveExists("M:\") Then WshNetwork.RemoveNetworkDrive "M:", True, True   
	pause 1
	WshNetwork.MapNetworkDrive "M:", "\\server\gl_" & PLZG & "\bla\" & PLZ, True  
End If
If Auswahl = 2 Then
	Application.Exit()
End If
MsgBox "Installation der Laufwerke abgeschlossen", vbOK  


Else
MsgBox "Ersteinrichtung abgebrochen", vbOK  
End If


Function Get_User_Name()
	Dim EnvVar
	Dim WshShell
	Set WshShell = CreateObject("WScript.Shell")  
	Set EnvVar = WshShell.Environment("PROCESS")  
	Get_User_Name = EnvVar("USERNAME")  

End Function
End If

Sub pause(t As Integer) 
    start = Timer
    Do While Timer < start + t
        DoEvents
    Loop
End Sub
Mitglied: 129813
129813 Aug 22, 2016 updated at 08:36:00 (UTC)
Goto Top
Line 82, there is a "lonely" End if face-wink and some other non matching if constructs !!

And Sub's are not allow to be placed inside other sub's face-wink if you missed to post the outer frame.
Member: Dr.Cornwallis
Dr.Cornwallis Aug 22, 2016 at 08:36:11 (UTC)
Goto Top
oh, i understand the error @ line 82, but how can i place the Sub into this code? Sorry im a bloody VB beginner
Mitglied: 129813
129813 Aug 22, 2016 updated at 08:39:21 (UTC)
Goto Top
Zitat von @Dr.Cornwallis:

oh, i understand the error @ line 82, but how can i place the Sub into this code? Sorry im a bloody VB beginner
omg... sure ??
You have a primary sub where your main code is placed inside.
Then the other subs are placed as siblings beside the main sub not inside the main sub.

Sub YourMainSub()
'...  
End Sub

function Get_User_Name()
'...  
End Function

Sub pause()
 '....  
End sub
Member: Dr.Cornwallis
Dr.Cornwallis Aug 22, 2016 at 08:39:34 (UTC)
Goto Top
PS: the end If @ 82 isn't lonely face-wink
Mitglied: 129813
129813 Aug 22, 2016 updated at 08:40:27 (UTC)
Goto Top
Zitat von @Dr.Cornwallis:

PS: the end If @ 82 isn't lonely face-wink
You can't place a function inside an if !!!!!!!
See above ...
Member: Dr.Cornwallis
Dr.Cornwallis Aug 22, 2016 at 08:47:55 (UTC)
Goto Top
Okay now i got it like u said, but now ist the same error "expected ')'" at the start of the last Sub(pause)
Member: Dr.Cornwallis
Dr.Cornwallis Aug 22, 2016 at 08:49:58 (UTC)
Goto Top
If you execute the code of the pause alone(new file), then u can see the error message.
Mitglied: 129813
129813 Aug 22, 2016 updated at 08:54:33 (UTC)
Goto Top
Zitat von @Dr.Cornwallis:

If you execute the code of the pause alone(new file), then u can see the error message.
You can't execute this sub standalone, you have to call it with a parameter.
Here it's working without problems.
Where are you executing this ?
You can write
start = Timer()
But the parenthesis are not a must have when there is no additional parameter for it.
Member: Dr.Cornwallis
Dr.Cornwallis Aug 22, 2016 updated at 09:07:05 (UTC)
Goto Top
When i execute this code ill get the error at Line 74:

Sub mapping()

Dim WshNetwork
Dim Filesystem
Dim Programmstart
Dim Server
Dim Link
Set Filesystem = CreateObject("Scripting.FileSystemObject")  
Set WshNetwork = WScript.CreateObject("WScript.Network")  
Set Programmstart = CreateObject("WScript.Shell")  



Antwort = MsgBox("bbb" & VbCrLf & VbCrLf & "bbb", vbYesNo, "bbb")  
If Antwort = vbYes Then


MeldungPlz = False
Do
	PLZ = InputBox("bbb", "BBB", 0)  
	PLZ = CInt(PLZ)
	If IsNumeric(PLZ) Then
		If PLZ > 7009 And PLZ < 8991 Then
			MeldungPlz = True
		End If
	End If 
Loop Until MeldungPlz = True

Antwort = MsgBox("bbb" & VbCrLf & VbCrLf & "ZB.: Gebiet 5 = 4053", vbOK, "Vielen Dank!")  
If Antwort = vbOK Then


MeldungPlzg = False
Do
	PLZG = InputBox("bbbb", "bbb", 0)  
	PLZG = CInt(PLZG)
	If IsNumeric(PLZG) Then
		If PLZG > 7009 And PLZG < 8991 Then
			MeldungPlzg = True
		End If
	End If 
Loop Until MeldungPlzg = True

MeldungAuswahl = False
Do
	Auswahl = InputBox("Treffen Sie eine Installationsauswahl:" & VbCrLf & VbCrLf & "1  =  bbb" & PLZ & VbCrLf & "2  =  Beenden" & VbCrLf, 0)  
	Auswahl = CInt(Auswahl)
	If IsNumeric(Auswahl) Then
		If Auswahl > 0 And Auswahl < 2 Then
			MeldungAuswahl = True
		End If
	End If 
Loop Until MeldungAuswahl = True
	

'Verbinden eines Netzlaufwerkes  
If Auswahl = 1 Then
	If Filesystem.DriveExists("M:\") Then WshNetwork.RemoveNetworkDrive "M:", True, True   
	pause 1
	WshNetwork.MapNetworkDrive "M:", "\\server\gl_" & PLZG & "\bbb\" & PLZ, True  
End If
If Auswahl = 2 Then
	Application.Exit()
End If
MsgBox "Installation der Laufwerke abgeschlossen", vbOK  


Else
MsgBox "Ersteinrichtung abgebrochen", vbOK  
End If
End If
End Sub

Sub pause(t As Integer)
    start = Timer
    Do While Timer < start + t
        DoEvents
    Loop
End Sub
Member: Dr.Cornwallis
Dr.Cornwallis Aug 22, 2016 at 09:40:00 (UTC)
Goto Top
Ive found a way how this script works:

"WScript.sleep 1000" between the Disconnect and mapping and it works face-smile

Thank you for your time!


Greets!
Mitglied: 129813
129813 Aug 22, 2016 updated at 10:42:04 (UTC)
Goto Top
My code works 100%, your are simply making some kind of weird error in your code we cannot see, my friend face-wink
I recommend reading the basics before patching any code together ... http://www.vba-tutorial.de/index.htm