Macro cannot "see" protected view file

sadoul

New Member
Joined
Apr 5, 2019
Messages
10
Hello guys,

So i created a command button macro that locate a file with a specific name but it seems that if the file is in "protected view", the macro cannot locate the file...
If i enable editing, it does work... If i don't it can't locate the file
I feel like i tried everything

Application.ActiveProtectedViewWindow.Edit
or
If Application.ProtectedViewWindows.Count > 0 Then
Application.ActiveProtectedViewWindow.Edit
End If
or
Unprotect



See macro below... Let me know what you think, i always get MsgBox ("There is no file currently open.") if i don't click on EDIT
Thanks for your help

Sub TD_Click()


Dim UtilWb As Workbook, Wb As Workbook




For Each Wb In Application.Workbooks
If Wb.Name Like "Fake name*" Then
Set UtilWb = Wb
Exit For
End If
Next Wb


If UtilWb Is Nothing Then
MsgBox ("There is no file currently open.")
'Ends macro if there is no util file
End
End If


'UtilWb.ActiveProtectedViewWindow.Edit
'UtilWb.Unprotect Password:=""
UtilWb.Activate
'If Application.ProtectedViewWindows.Count > 0 Then
'Application.ActiveProtectedViewWindow.Edit
'End If




End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Code:
Application.ProtectedViewWindows(1).Activate
Application.ActiveProtectedViewWindow.Edit
 
Upvote 0
Not knowing what you're rying to do, maybe ...

Code:
Sub main()
  Dim sFile         As String

  sFile = "Sadoul.xlsx"
  If EditProtected(sFile) Then
    MsgBox sFile & " is open for editing"
  Else
    MsgBox sFile & " is not amoung protected-view windows"
  End If
End Sub

Function EditProtected(ByVal sFile As String) As Boolean
  Dim i             As Long

  sFile = LCase(sFile)

  With Application
    For i = 1 To .ProtectedViewWindows.Count
      .ProtectedViewWindows(i).Activate
      If LCase(.ActiveProtectedViewWindow.SourceName) = sFile Then
        .ActiveProtectedViewWindow.Edit
        EditProtected = True
        Exit For
      End If
    Next i
  End With
End Function
 
Upvote 0
Not knowing what you're rying to do, maybe ...

Code:
Sub main()
  Dim sFile         As String

  sFile = "Sadoul.xlsx"
  If EditProtected(sFile) Then
    MsgBox sFile & " is open for editing"
  Else
    MsgBox sFile & " is not amoung protected-view windows"
  End If
End Sub

Function EditProtected(ByVal sFile As String) As Boolean
  Dim i             As Long

  sFile = LCase(sFile)

  With Application
    For i = 1 To .ProtectedViewWindows.Count
      .ProtectedViewWindows(i).Activate
      If LCase(.ActiveProtectedViewWindow.SourceName) = sFile Then
        .ActiveProtectedViewWindow.Edit
        EditProtected = True
        Exit For
      End If
    Next i
  End With
End Function

I am trying to have this xl file, enable for editing but macro doesn't see the file because it is in protected view
 
Upvote 0
So -- did you try the posted code?

There's no reason to quote my posts back to me.
 
Upvote 0
Sorry about that...
Issue with your macro is Sfile variable as String, my file doesn't have same name all the time it starts with for example "Fake file*"
So i add to decalre the file Utilwb as a workbook
How can i declare same file with two different variable?

Code:
[COLOR=#333333]Sub TD_Click()[/COLOR]

[COLOR=#333333]Dim UtilWb As Workbook, Wb As Workbook[/COLOR]

[COLOR=#333333]For Each Wb In Application.Workbooks[/COLOR]
[COLOR=#333333]If Wb.Name Like "Fake name*" Then[/COLOR]
[COLOR=#333333]Set UtilWb = Wb[/COLOR]
[COLOR=#333333]Exit For[/COLOR]
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]Next Wb[/COLOR]

[COLOR=#333333]If UtilWb Is Nothing Then[/COLOR]
[COLOR=#333333]MsgBox ("There is no file currently open.")[/COLOR]
[COLOR=#333333]'Ends macro if there is no util file[/COLOR]
[COLOR=#333333]End[/COLOR]
[COLOR=#333333]End If[/COLOR]

[COLOR=#333333]UtilWb.Activate[/COLOR]

[COLOR=#333333]End Sub[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,428
Members
448,961
Latest member
nzskater

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