B
Barb Reinhardt
OK, what am I missing folks. I'm using this to open a workbook as ReadOnly
and so I can edit it at a different time. The workbook I'm trying to open
has VBA code in it and I'm trying to open as Read Only. I've recently had
to add the automation security lines so I don't get a message to enable
macros when I open a workbook that contains them. I don't have the caps
lock key pressed either. If myReadOnly = FALSE, it opens the workbook and
execution ends.
I've done this this way in another workbook and it worked fine.
FWIW, I'm also having issues saving a powerPoint presentation that I used to
be able to save programmatically. Could my laptop have gremlins?
Thanks,
Barb Reinhardt
Option Explicit
Sub OpenWorkbook(myWB As Excel.Workbook, myReadOnly As Boolean)
Dim sFile As String
Dim ShortName As String
Dim autoSecurity As MsoAutomationSecurity
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Filters.Clear
.Filters.Add "Excel Files", myXLFilter
.FilterIndex = 1
.Title = "Please Select File to open"
If .Show = False Then Exit Sub
sFile = .SelectedItems(1)
End With
ShortName = Right(sFile, Len(sFile) - InStrRev(sFile, "\"))
Set myWB = Nothing
On Error Resume Next
Set myWB = Workbooks(ShortName)
On Error GoTo 0
If myWB Is Nothing Then
autoSecurity = Application.AutomationSecurity
If myReadOnly Then
Application.AutomationSecurity = msoAutomationSecurityForceDisable
Set myWB = Workbooks.Open(sFile, ReadOnly:=True)
Application.AutomationSecurity = autoSecurity
Else
Application.AutomationSecurity = msoAutomationSecurityForceDisable
Set myWB = Workbooks.Open(sFile)
Application.AutomationSecurity = autoSecurity
End If
Debug.Print myWB.Name
Else
'No action
End If
End Sub
and so I can edit it at a different time. The workbook I'm trying to open
has VBA code in it and I'm trying to open as Read Only. I've recently had
to add the automation security lines so I don't get a message to enable
macros when I open a workbook that contains them. I don't have the caps
lock key pressed either. If myReadOnly = FALSE, it opens the workbook and
execution ends.
I've done this this way in another workbook and it worked fine.
FWIW, I'm also having issues saving a powerPoint presentation that I used to
be able to save programmatically. Could my laptop have gremlins?
Thanks,
Barb Reinhardt
Option Explicit
Sub OpenWorkbook(myWB As Excel.Workbook, myReadOnly As Boolean)
Dim sFile As String
Dim ShortName As String
Dim autoSecurity As MsoAutomationSecurity
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Filters.Clear
.Filters.Add "Excel Files", myXLFilter
.FilterIndex = 1
.Title = "Please Select File to open"
If .Show = False Then Exit Sub
sFile = .SelectedItems(1)
End With
ShortName = Right(sFile, Len(sFile) - InStrRev(sFile, "\"))
Set myWB = Nothing
On Error Resume Next
Set myWB = Workbooks(ShortName)
On Error GoTo 0
If myWB Is Nothing Then
autoSecurity = Application.AutomationSecurity
If myReadOnly Then
Application.AutomationSecurity = msoAutomationSecurityForceDisable
Set myWB = Workbooks.Open(sFile, ReadOnly:=True)
Application.AutomationSecurity = autoSecurity
Else
Application.AutomationSecurity = msoAutomationSecurityForceDisable
Set myWB = Workbooks.Open(sFile)
Application.AutomationSecurity = autoSecurity
End If
Debug.Print myWB.Name
Else
'No action
End If
End Sub