Oh! Yes I do! It's down near the bottom half somewhere:
Option Explicit
Const prnPrev As Boolean = False ' Change to False if no print preview desired.
Dim response As Integer, x As Integer
Private Sub selPages()
Application.EnableEvents = False
On Error GoTo erHandle
If Not nPages > 0 Then
GoTo erHandle
Else
ActiveWindow.SelectedSheets.PrintOut from:=1, _
To:=nPages, copies:=1, preview:=prnPrev, Collate:=True
GoTo endSub
End If
erHandle:
ActiveWindow.SelectedSheets.PrintOut preview:=prnPrev
endSub:
On Error GoTo 0
Application.EnableEvents = True
End Sub
Private Sub Workbook_BeforePrint(Cancel As Boolean)
Dim response, toPg As Integer
x = 1
If ActiveSheet.Name = "Inspection Report" Then
If IsNumeric(Range("X1").Value) And Range("X1") <> "" Then
toPg = Range("X1").Value
Else
toPg = 0
End If
response = Application.Dialogs(xlDialogPrint).Show(2, 1, toPg)
If x > 1 Then
Cancel = True
End If
x = x + 1
ElseIf ActiveSheet.Name = "Inspection Report Attachment" Then
If IsNumeric(Range("P1").Value) And Range("P1") <> "" Then
toPg = Range("P1").Value
Else
toPg = 0
End If
response = Application.Dialogs(xlDialogPrint).Show(2, 1, toPg)
If x > 1 Then
Cancel = True
End If
x = x + 1
End If
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim strSaveName As String, strPart As String, strRev As String
Dim varData
On Error GoTo err_handler
' cancel default save
Cancel = True
' prevent this event being triggered by saves in the code
Application.EnableEvents = False
' check if copy being saved
If SaveAsUI Then
strSaveName = Application.GetSaveAsFilename(filefilter:="Microsoft Excel Macro enabled workbook (*.xlsm), *.xlsm", Title:="Save file")
If strSaveName <> "False" Then
If IsValidFileName(strSaveName) Then
' populate cells and save
AddPartInformation strSaveName
ThisWorkbook.SaveAs strSaveName
ElseIf IsTemplateFile(strSaveName) Then
' template, so just save
ThisWorkbook.SaveAs strSaveName
Else
MsgBox "File name is invalid - must be <part number>Rev<revision number>"
Exit Sub
End If
End If
Else
' check it's not the template being saved
If IsTemplateFile(ThisWorkbook.Name) Then
' it's the template so just save.
ThisWorkbook.Save
Else
' populate cells and save
AddPartInformation ThisWorkbook.Name
ThisWorkbook.Save
End If
End If ' SaveAsUI
clean_up:
Application.EnableEvents = True
Exit Sub
err_handler:
MsgBox Err.Description
Resume clean_up
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim lngResp As Long
If Not ThisWorkbook.Saved Then
ThisWorkbook.Saved = True
lngResp = MsgBox("Do you wish to save the workbook before closing?", vbYesNo)
If lngResp = vbYes Then
With ThisWorkbook
.Save
.Saved = True
End With
End If
End If
End Sub
Bookmarks