It seems the CDP content does not update once inserted into a
workbook, when you change the CDP afterwards. Here is how I tested: I
used Macro1 to add a CDP called "VAR1"; then I used Macro2 and Macro2
to insert the CDP into one cell and the footer; then I manually
changed CDP "VAR1" to something else, but the workbook does not update
to reflect the change. Btw, I tried saving the workbook. What did I
miss? Thank you!
Sub Macro1()
Dim dp As DocumentProperties
Set dp = ActiveWorkbook.CustomDocumentProperties
dp.Add "VAR1", False, msoPropertyTypeString, "RED & BLUE"
End Sub
Sub Macro2()
Range("A1").Select
ActiveCell.FormulaR1C1 = "BEFORE " &
ActiveWorkbook.CustomDocumentProperties("VAR1") & " AFTER"
End Sub
Sub Macro3()
Dim wkSht As Worksheet
For Each wkSht In ActiveWorkbook.Worksheets
wkSht.PageSetup.RightFooter =
ActiveWorkbook.CustomDocumentProperties("VAR1")
Next wkSht
End Sub
Good afternoon,
I had the same problem in the past of having created my own functions
to customize my eXcel sheets. The solution seems to be (at least it
worked for me) to have the instruction "Application.Volatile" at the
beginning of the function. Then, each time the sheet is recalculated
(at opening or activating the sheet or printing) then the function is
recalculated.
Example:
In the VBA environment attached to the "ThisWorkbook" space I write
this little sub:
Sub Workbook_BeforePrint(Cancel As Boolean)
Application.Volatile
Dim wkSht As Worksheet
MsgBox "entering sub ***Workbook_BeforePrint(Cancel)"
MsgBox myPageCenterHeader
'For Each wkSht In ThisWorkbook.Worksheets
' wkSht.PageSetup.CenterHeader = myPageCenterHeader
'Next wkSht
Cancel = True
MsgBox "exiting sub ***Workbook_BeforePrint, 'Cancel' set to true,
so printing is canceled!"
End Sub
In the VBA environment I create (if not already done) a "Module":
right click on the VBA project name (if your file is toto.xls your VBA
project should be named "VBAProject (toto.xls)") option "Insert/
Module". This creates a VBA folder "Modules" into the VBA project and
then a module "Module1" into that VBA folder.
Double click on that "Module1" name and VBA opens the code window.
In that window just type a function like this one if you like:
' This function to be called by Sub "Workbook_BeforePrint(Cancel As
Boolean)"
' Workbook_BeforePrint() sub is to be placed into the "ThisWorkbook"
VBA space
' so that it is called each time the print event is generated (when
print action
' is invoked).
Function myPageCenterHeader() As String
Application.Volatile
Dim s As String
With ThisWorkbook.CustomDocumentProperties
s = "Registered Version: " & .Item("Fortis_ICT_DocVersion") &
_
" -- Registered version date: " & _
Format(.Item("Fortis_ICT_DocDate"), "dddd, dd mmmm yyyy",
vbMonday)
End With
myPageCenterHeader = s
End Function
Because the code contains the instruction "Application.Volatile" it
should recalculate each time needed.
I hope it will help you.
Gilles