How to access a network excel file with different credentials using vba? -


i've created vba code group of people in our company can "stamp" word document unique number stored in excel sheet on network drive (essentially giving serial#). these people not have access said network drive, i'd them able execute vba code.

i've been reading on various articles on site , others last couple days, , able adapt this post work me. however, if try , execute vba code more once, "multiple connections" error described in original answer. seems, then, mapped drive not being removed. simple syntax issue? or missing in removenetworkdrive statement?

servershare = "\\servername\path" username = "domain\username" password = "password"  set networkobject = createobject("wscript.network") set fso = createobject("scripting.filesystemobject")  networkobject.mapnetworkdrive "", servershare, false, username, password  str_wbpath = servershare & "\mrl number generator.xlsm" set exwb = objexcel.workbooks.open(str_wbpath)  'do stuff in excel   set exwb = nothing set fso = nothing  networkobject.removenetworkdrive servershare, true, false  set networkobject = nothing 

i'll tell worked me. used drive, , removed drive instead.

'this insures find empty drive letter = 67 90     netdrive = chr(i) & ":"     if not fs.driveexists(netdrive) exit next  'mount network drive on above selected drive letter network.mapnetworkdrive netdrive, servershare, false, username, password  'do stuff 'do stuff  'remove drive letter network.removenetworkdrive netdrive, true, true 

i'd put remove on error handling well, otherwise you'll spawn bunch of drives if have error.


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -