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
 
Code:
Sub main()
  Dim sPatt         As String

  sPatt = "Fakename*"
  If EditProtected(sPatt) Then
    MsgBox sPatt & " is open for editing"
  Else
    MsgBox sPatt & " is not amoung protected-view windows"
  End If
End Sub

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

  sPatt = LCase(sPatt)

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

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
A tweak:

Code:
Sub main()
  Dim sPatt         As String
  Dim sFile         As String

  sPatt = "Fakename*"
  If EditProtected(sPatt, sFile) Then
    MsgBox sFile & " is open for editing"
  Else
    MsgBox "No file like " & sPatt & " in protected-view windows"
  End If
End Sub

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

  sPatt = LCase(sPatt)

  With Application
    For i = 1 To .ProtectedViewWindows.Count
      .ProtectedViewWindows(i).Activate
      If LCase(.ActiveProtectedViewWindow.SourceName) Like sPatt Then
        .ActiveProtectedViewWindow.Edit
        sFile = ActiveWorkbook.Name
        EditProtected = True
        Exit For
      End If
    Next i
  End With
End Function
 
Upvote 0
Appreciate your feedback but i have tried everything and it is not working.

I don't understand how i can incorporate this code in my macro... Like i said i use a command button and it doesn't look like Sub main() is read when launching Sub TD_Click()...
 
Upvote 0
I had this problem a few weeks ago. so long as your macro has


If Application.ProtectedViewWindows.Count > 0 Then
Application.ActiveProtectedViewWindow.Edit
End If
and is stored in an unprotected file
run it from the protected file and off you go!
Cheers,
Jerry
 
Upvote 0
Hey Jerry,

Thanks for your feedback,I actually thought about this... I felt like i needed to have command button because i have two different files to run but maybe i'll try a short cut with CTRL+ a different letter for each file.

Do i enter this right as the beginning of the macro?
 
Upvote 0
I had this problem a few weeks ago. so long as your macro has


If Application.ProtectedViewWindows.Count > 0 Then
Application.ActiveProtectedViewWindow.Edit
End If
and is stored in an unprotected file
run it from the protected file and off you go!
Cheers,
Jerry

What about if the user enable editing before running macro?
 
Upvote 0
Don't need to. that's what the code does. You should head your query " Macro cannot see protected view file but protected view file CAN see macro"
Cheers,
Jerry
 
Upvote 0
Don't need to. that's what the code does. You should head your query " Macro cannot see protected view file but protected view file CAN see macro"
Cheers,
Jerry

Thanks Jerry but what about if when opening the file, the user click on enable editing by mistake, is it still going to run. i am trying to test and i see different results...
 
Upvote 0

Forum statistics

Threads
1,215,326
Messages
6,124,270
Members
449,149
Latest member
mwdbActuary

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