help with add in
Hi all,
I have created a beforesave macro that works fine. It is a simple macro that adds a footer to the document when saving. I tried to make an add in of it but it did not work.
Below code is put in the workbook module ThisWorkbook
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
ARFModule.ARFM
End Sub
Below code is put in a general VBA module.
Option Base 1
Sub ARFM()
Dim SecurityLevel()
SecurityLevel = Array("Secret", "Confidential", "Proprietary", "Public")
CurFoot = Worksheets("Sheet1").PageSetup.LeftFooter
If CurFoot = Application.UserName & ", " & "&D" & Chr(10) & "Security Class <" & SecurityLevel(1) & ">" Then Exit Sub
If CurFoot = Application.UserName & ", " & "&D" & Chr(10) & "Security Class <" & SecurityLevel(2) & ">" Then Exit Sub
If CurFoot = Application.UserName & ", " & "&D" & Chr(10) & "Security Class <" & SecurityLevel(3) & ">" Then Exit Sub
If CurFoot = Application.UserName & ", " & "&D" & Chr(10) & "Security Class <" & SecurityLevel(4) & ">" Then Exit Sub
On Error Resume Next
Do Until Level = 1 Or Level = 2 Or Level = 3 Or Level = 4
Level = InputBox("To AFR secure this document type in security level, otherwise cancel.1 = Secret, 2 = Confidential, 3 = Proprietary and 4 = Public.", "Update the date")
If Level = "" Then
Exit Sub ''' Cancel pressed
End If
Loop
Worksheets("Sheet1").PageSetup.LeftFooter = Application.UserName & ", " & "&D" & Chr(10) & "Security Class <" & SecurityLevel(Level) & ">"
End Sub
I have saved the macro as an add-in .xla file and made it active in the Add-ins dialog box.
What have I missed? I wouuld guess it can have something to do with positioning the first code in ThisWorkbook.
Thankful for help on this.
Regards
Jonas
[2388 byte] By [
JS] at [2008-1-7]
Hi,
The problem is your code will only run when your addin workbook triggers the BeforeSave event. I would imagine you want the code to run for any workbook. If so you need to write code that will capture the BeforeSave of any open workbook.
To do this you need to create a class with application level events.
For more information on the topic of Application event handling see Chip Pearson's page. He also has pages on addins which you should find useful.
http://www.cpearson.com/excel/AppEvent.htm
Thanks Andy,
I dowloaded an example that worked but not for the function I want it to. I would like to activate the macro with the Beforesave event but for some how it doesn't work with this. I have tried to activate the macro with sheetactivate and sheetchange event and then it works perfect.
Any ideas why it won't work with Beforesave?
In ThisWorkbook:
Option Explicit
Dim AppClass As New EventClass
Private Sub Workbook_Open()
Set AppClass.App = Application
End Sub
In a class module:
Public WithEvents App As Application
Private Sub App_SheetActivate(ByVal Sh As Object) '''WORKS
'Private Sub App_WorkbookBeforeSave(ByVal Wb As Workbook, ByVal SaveAsUI As Boolean, Cancel As Boolean) '''FALES
Dim SecurityLevel()
Dim CurFoot
Dim Level As Integer
Level = 0
SecurityLevel = Array("Secret", "Confidential", "Proprietary", "Public")
CurFoot = Worksheets("Sheet1").PageSetup.LeftFooter
'If CurFoot = Application.UserName & ", " & "&D" & Chr(10) & "Security Class <" & SecurityLevel(1) & ">" Then Exit Sub
'If CurFoot = Application.UserName & ", " & "&D" & Chr(10) & "Security Class <" & SecurityLevel(2) & ">" Then Exit Sub
'If CurFoot = Application.UserName & ", " & "&D" & Chr(10) & "Security Class <" & SecurityLevel(3) & ">" Then Exit Sub
'If CurFoot = Application.UserName & ", " & "&D" & Chr(10) & "Security Class <" & SecurityLevel(4) & ">" Then Exit Sub
On Error Resume Next
Do Until Level = 1 Or Level = 2 Or Level = 3 Or Level = 4
Level = InputBox("To AFR secure this document type in security level, otherwise cancel.1 = Secret, 2 = Confidential, 3 = Proprietary and 4 = Public.", "Update the date")
If Level = 0 Then
Exit Sub ''' Cancel pressed
End If
Loop
Worksheets("Sheet1").PageSetup.LeftFooter = Application.UserName & ", " & "&D" & Chr(10) & "Security Class <" & SecurityLevel(Level) & ">"
End Sub
Cheers
Jonas
JS at 2007-10-2 >

THISWORKBOOK OBJECTOption Explicit
Private Sub Workbook_Open()
Set AppClass = New EventClass
Set AppClass.App = Application
End Sub
STANDARD CODE MODULE
Option Explicit
Public AppClass As EventClass
CLASS EVENTCLASS
Public WithEvents App As Application
Private Sub App_WorkbookBeforeSave(ByVal Wb As Workbook, ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim SecurityLevel()
Dim CurFoot
Dim Level As Integer
Level = 0
SecurityLevel = Array("Secret", "Confidential", "Proprietary", "Public")
CurFoot = Wb.Worksheets("Sheet1").PageSetup.LeftFooter
'If CurFoot = Application.UserName & ", " & "&D" & Chr(10) & "Security Class <" & SecurityLevel(1) & ">" Then Exit Sub
'If CurFoot = Application.UserName & ", " & "&D" & Chr(10) & "Security Class <" & SecurityLevel(2) & ">" Then Exit Sub
'If CurFoot = Application.UserName & ", " & "&D" & Chr(10) & "Security Class <" & SecurityLevel(3) & ">" Then Exit Sub
'If CurFoot = Application.UserName & ", " & "&D" & Chr(10) & "Security Class <" & SecurityLevel(4) & ">" Then Exit Sub
On Error Resume Next
Do Until Level = 1 Or Level = 2 Or Level = 3 Or Level = 4
Level = InputBox("To AFR secure this document type in security level, otherwise cancel.1 = Secret, 2 = Confidential, 3 = Proprietary and 4 = Public.", "Update the date")
If Level = 0 Then
Exit Sub ''' Cancel pressed
End If
Loop
Wb.Worksheets("Sheet1").PageSetup.LeftFooter = Application.UserName & ", " & "&D" & Chr(10) & "Security Class <" & SecurityLevel(Level) & ">"
End Sub
Thanks Andy for taking your time with this.
The code however still won't work. But the problem seems to be in the last line,
Wb.Worksheets("Sheet1").PageSetup.LeftFooter = "test"
The code below works for the green marked alternative at the end but not for the red marked. It is kind of strange since it sometimes works with the PageSetup.leftfooter choice but not always. I have tried to run the code in a regular macro that is trigged by the play macro command and then it works! Do you know why PageSetup.leftfoot won't work when trigged by the BeforeSave event?
Public WithEvents App As Application
Private Sub App_WorkbookBeforeSave(ByVal Wb As Workbook, ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim SecurityLevel()
Dim CurFoot
Dim Level
Level = 0
SecurityLevel = Array("Secret", "Confidential", "Proprietary", "Public")
CurFoot = Worksheets("Sheet1").PageSetup.LeftFooter
On Error Resume Next
Do Until Level = 1 Or Level = 2 Or Level = 3 Or Level = 4
Level = InputBox("To AFR secure this document type in security level, otherwise cancel.1 = Secret, 2 = Confidential, 3 = Proprietary and 4 = Public.")
If Level = 0 Then
Exit Sub ''' Cancel pressed
End If
Loop
Wb.Worksheets("Sheet1").Range("A3") = "test"
Wb.Worksheets("Sheet1").PageSetup.LeftFooter = "test"
End Sub
Cheers
Jonas
JS at 2007-10-2 >

Does it error or just not set the text?
If no error I assume you are getting the Input box request?
Yes that is correct the input request works. It is just the PageSetup function that seems to fail. No error mesages.
JS at 2007-10-2 >

For me the code works execpt in a couple of situtations.
Nothing on Sheet1 to be printed.
Sheet1 does not exist.
Strange for me never. I tried with workbookbeforeclose and that worked!? But not workbookbeforesave. I will try to solve it in an other way. Thanks for your support.
Cheers
Jonas
JS at 2007-10-2 >

What version of excel are you using?
If you want to email your file, andy at andypope dot info, I will have a look but for me the code posted here works in a addin I have created using xl2003 SP2
I'm using the same as you 2003 sp2.
I have sent my file to your box.
Cheers
JS at 2007-10-2 >
