Bypass prompts in loop code

dturgel

Board Regular
Joined
Aug 6, 2015
Messages
58
Hi,

I've got really good code from AlphaFrog below. Every file this tries to loop through gives me the same two prompts each time:
1. This workbook contains links to one or more external sources that could be unsafe. If you trust the links, update them to get the latest date. Otherwise, you can keep working with the data you have.
2. 'xyz.docx' is protected. Password:

My question is - how can I get around these two prompts? I imagine getting around the first one may negate the second one - is that correct? If so, how do you do it?


Code:
Sub Accounts_PricingLoopNorthDakota()
    Dim FSO As Object, fsoFile As Object, fsoSubfolder As Object
    Dim strPath As String
    
    MsgBox "Please choose the Main Accounts folder."
    Application.DisplayAlerts = False
    ChDrive "G"
    With Application.FileDialog(msoFileDialogFolderPicker)
        .InitialFileName = "G:\"
        .AllowMultiSelect = False
        If .Show <> -1 Then Exit Sub
        strPath = .SelectedItems(1) & "\"
    End With
    
    Application.ScreenUpdating = False
    Set FSO = CreateObject("Scripting.FileSystemObject")
    'Loop through each subfolder in main path
    For Each fsoSubfolder In FSO.GetFolder(strPath).Subfolders
        'Test if "Pricing" subfolder exists
        If FSO.FolderExists(fsoSubfolder.Path & "\Pricing\") Then
            'Loop through each Excel file in subfolder\Pricing\
            For Each fsoFile In FSO.GetFolder(fsoSubfolder.Path & "\Pricing\").Files
                
                'Open workbook
                With Workbooks.Open(Filename:=fsoFile.Path)
                    'Change First Worksheet Mkt Curve Cell Q63 to 320M (North Dakota Premium)
                    .Worksheets(1).Range("Q63") = 320000000
                    'Save and Close Workbook
                    .Close SaveChanges:=True
                End With
                    
            Next fsoFile
        End If
    Next fsoSubfolder
    Application.ScreenUpdating = True
    Set FSO = Nothing
    
End Sub
[end code/]
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
You can use: application.DisplayAlerts = False to get past the first one. I think. For the second one, if you know the password you can use something similar to this:
Code:
Dim WB As Workbook
  Set WB = Application.Workbooks.Open("C:\MyFiles\MrExcel\Password Protected.xlsx", Password:="Jeff")

Of course, the password would be visible to anyone looking at your VBA code, so you may want to protect the VBA project with a password.

Jeff
 
Upvote 0

Forum statistics

Threads
1,214,793
Messages
6,121,614
Members
449,039
Latest member
Mbone Mathonsi

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