Code to login to a protected file


New Member
Mar 27, 2020
Office Version
  1. 365
  1. Windows

The excel file I am currently working on uses Vlookups in the worksheet to automatically pull about 70% of the information into a userform, the rest is input manually by a Control Room Operator. Unfortunately the folder, lets call it Folder C, that houses the information that the vlookups pull data from is password protected which is making me run into an issue. All of these files are on a company server and administration can access Folder C if they have it mapped but those who input data from our control room can not. I worked on a code that plugged in the username and password as soon as the document was opened so we didn't have to give out the login info. This caused a popup to say that we could not login in twice when it was opened on a computer with Folder C already mapped. So I'm thinking an If Then statement code could solve this.

This is what I have so far but I run into a "runtime error '424' Object Required" which highlights the line with the bold red underlined text. Any ideas how to fix this issue? Thanks so much in advance!!

Sub Auto_Open()
'''start of trial

Dim NetworkObject As Object
Dim FSO As Object
Dim Directory As Object
Dim Filename As Object
Dim ServerShare As String
Dim UserName As String
Dim Password As String

ServerShare = "Redacted"
UserName = "redacted"
Password = "redacted"

Set NetworkObject = CreateObject("WScript.Network")
Set FSO = CreateObject("Scripting.FileSystemObject")

If Not objFSO.FolderExists(ServerShare) Then
NetworkObject.MapNetworkDrive "", ServerShare, False, UserName, Password
End If

Set Directory = FSO.GetFolder(ServerShare)
For Each Filename In Directory.Files
Debug.Print Filename.Name

Set Filename = Nothing
Set Directory = Nothing
Set FSO = Nothing

NetworkObject.RemoveNetworkDrive ServerShare, True, False

Set NetworkObject = Nothing


Some videos you may like

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.

Watch MrExcel Video

Forum statistics

Latest member