HI all,
I'm using a macro to create two published versions of a workbook. The workbook I use has a SAS add-in reference that my customers do not have. To prevent them from receiving reference library errors, I publish the workbooks for their use without macros. Since I'm using a macro to publish the workbook, I create a duplicate copy (SaveCopyAs) then open and modify the duplicate.
Because the duplicate copy is initially a macro-enabled workbook, there's a prompt to enable macros. The problem I run into is that the enable macros prompt shows up in the ribbon unless I have VBA editor open. If VBA editor is open, I get a pop-up which allows me to enable the macros in the duplicate workbook. If VBA editor is not open, I can't enable the macros and the code stops.
I don't want to disable the user's automation security via VBA, and am hoping it is just a matter of code sequencing in order to allow the user to enable macros when the VBA editor is not open.
Here's my code; any suggestions are greatly appreciated.
I'm using a macro to create two published versions of a workbook. The workbook I use has a SAS add-in reference that my customers do not have. To prevent them from receiving reference library errors, I publish the workbooks for their use without macros. Since I'm using a macro to publish the workbook, I create a duplicate copy (SaveCopyAs) then open and modify the duplicate.
Because the duplicate copy is initially a macro-enabled workbook, there's a prompt to enable macros. The problem I run into is that the enable macros prompt shows up in the ribbon unless I have VBA editor open. If VBA editor is open, I get a pop-up which allows me to enable the macros in the duplicate workbook. If VBA editor is not open, I can't enable the macros and the code stops.
I don't want to disable the user's automation security via VBA, and am hoping it is just a matter of code sequencing in order to allow the user to enable macros when the VBA editor is not open.
Here's my code; any suggestions are greatly appreciated.
VBA Code:
Sub Publish_Report()
Dim strDir As String
Dim strTempSave As String
Dim strFolder As String
Dim strDataDate As String
Dim strFolderDate As String
Dim dtMax As Date
Dim oSheet As Worksheet
Dim bkNew As Workbook
Dim strSecurity As String
strDir = "file path name"
dtMax = ThisWorkbook.Sheets("Injury").Range("R6").Value
strDataDate = Format(dtMax, "yyyy-mm")
strFolderDate = Format(Application.WorksheetFunction.EoMonth(dtMax, 1), "yyyy-mm")
strTempSave = "C:\Users\" & Environ("UserName") & "\Documents\PFR_Temp.xlsm"
strFolder = Dir(strDir & "PD Discussion Documents\" & strFolderDate, vbDirectory)
If strFolder = "" Then MkDir (strDir & "PD Discussion Documents\" & strFolderDate)
If MsgBox("Saving the document, saving to the 5Q folder, and saving to the Discussion Documents folder.", vbOKCancel) = vbOK Then
ThisWorkbook.Save
' Save a temporary copy to allow saving as a non-macro enabled workbook
ThisWorkbook.SaveCopyAs strTempSave
' Open the temporary copy.
Workbooks.Open strTempSave
Set bkNew = ActiveWorkbook
With bkNew
' Create and save managers copy
.Sheets(Array("VPO_5Q", "Track_Record", "Notes")).Delete
.Application.DisplayFormulaBar = False
For Each oSheet In .Sheets
Select Case oSheet.Name
Case Is = "Source", "Casualty", "Injury", "Injury_CM_5Q"
oSheet.Activate
oSheet.Range("A7").Activate
ActiveWindow.DisplayHeadings = False
ActiveWindow.DisplayGridlines = False
oSheet.Protect DrawingObjects:=False, AllowFiltering:=True
Case Else
oSheet.Visible = xlSheetHidden
End Select
Next oSheet
UpdateSlicer sCache:=.SlicerCaches("Slicer_View"), strCompare:="All DP & VPO", bShow:=True
.SaveAs _
Filename:=strDir & "CM 5Q Folder\PFR Metrics.xlsx", _
FileFormat:=51, _
ConflictResolution:=xlLocalSessionChanges
' create and save OVP copy
.Sheets("Injury_CM_5Q").Delete
UpdateSlicer sCache:=.SlicerCaches("Slicer_View"), strCompare:="All VPO Only", bShow:=True
.SaveAs _
Filename:=strDir & "PD Discussion Documents\" & strFolderDate & "\" & strDataDate & " PFR Metrics.xlsx", _
FileFormat:=51, _
ConflictResolution:=xlLocalSessionChanges
.Close
On Error GoTo ErrorMsg
End With
' delete the temporary workbook
Kill strTempSave
End If
Application.DisplayAlerts = True
Exit Sub
ErrorMsg:
MsgBox "Not able to save to the PD Discussion Document folder!"
End Sub