Code to login to a protected file

tnapele

New Member
Joined
Mar 27, 2020
Messages
14
Office Version
  1. 365
Platform
  1. Windows
Hello!

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
Next

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

NetworkObject.RemoveNetworkDrive ServerShare, True, False

Set NetworkObject = Nothing

ActiveWorkbook.RefreshAll[/CODE][/CODE]
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

Forum statistics

Threads
1,214,943
Messages
6,122,370
Members
449,080
Latest member
Armadillos

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top