I created a template for an invoice with a macro to set the invoice number on printout. I signed the macro with a certificate I created with selfcert.exe.
The macro runs well. But when I set the macro security level to high the following happens:
1. When creating a new document based on the invoice template the macro is activated.
2. When I save the document and open it again the macro is not activated.
Any suggestions how to manage that the macro works after saving the document and opening it again?
I'm using office XP.
Last edited by zeneri; Oct 15th, 2004 at 04:18 PM.
When I look in the project explorer, I can see the macro code.
When I set the macro security level to medium and open the created document, WORD asks if I want to activate the macros. When I activate them everything works well.
I only got a problem when I set the security level to high.
Yes it is. My problem is, I've NO problem with the certificate when using the template. I've the problem, when I create a document based on a template. My macros are disabled when I open this new document again.
Also, Is you Cert. installed in the root certificate store?
Yes it is. Because everything works well, when I open the template, but not when opening a document based on the template, it seems to be that the certification of the macro code is ignored, when opening the document.
I've got no idea how to manage this. May be you could try if you can reproduce the behaviour.
Could you just create a template with the following code and sign the code:
VB Code:
Private Sub Document_New()
MsgBox "new"
End Sub
Private Sub Document_Open()
MsgBox "open"
End Sub
When the macro security level is set to high and I create a new document based on the template, than the "new" appears. When I save the document and open it again I would expect the "open" to appear. But it does'nt. When I now set the security level to medium and open then document, allow macros to be executed, then the "open" appears.
Ok, I got it to work. What you need to do is set the "Always trust
macros from publisher"...
Modify the list of trusted publishers for macros
Security Because macros can contain viruses, be careful about
running them. Take the following precautions: run up-to-date
antivirus software on your computer; set your macro security
level to high; clear the Trust all installed add-ins and templates
check box; use digital signatures; maintain a list of trusted
publishers.
To perform these procedures, you must have Microsoft Internet
Explorer version 4.0 or later installed on your computer.
Do one of the following:
Add a macro developer to the list of trusted publishers
If you haven't already done so, set the macro security level to
Medium or High.
How?
On the Tools menu, click Options.
Click the Security tab.
Under Macro Security, click Macro Security.
Click the Security Level tab, and then select the security level you
want to use.
Open the file or load the add-in that contains macros certified by
the macro developer that you want to add to the list.
In the Security Warning box, select the Always trust macros from
this publisher check box.
Note If the Security Warning box does not display the Always
trust macros from this publisher check box, the macros are not
digitally signed. You cannot add this macro developer to the list of
trusted sources without a digital signature.
Remove a macro developer from the list of trusted publishers
On the Tools menu, click Options, and then click the Security tab.
Under Macro Security, click Macro Security.
Click the Trusted Publishers tab.
Click the source you want to remove from the list.
Click Remove.
If you dont get the warning message you can not enable the
macros to run.
Whenever I create a new document from the template with the
macros in it, I get the new message box.
HTH
Last edited by RobDog888; Oct 11th, 2004 at 11:51 PM.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Allright so far. I've done all the steps you mentioned. I get the "new" message box every time I create a new document based on the template. But I never get the "open" message box when I saved one off these documents and opened it again.
No prob. Now that I think about it, no I didnt get the open message
box. Maybe because this is based off of a template and not an
actual document we may need to add so kind of class to attact to
the open event? I know I have a couple of event demos around
the forum for both Word and Excel. Try a search.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
I get the event (and the "open" message box) when I set the macro security level to medium. I think the problem is, that the macro in the saved document is'nt signed anymore. And I don't know how to ensure that not only the macro in the template but also the macros of the documents based on the template are digitaly signed.
Yes, that is the new problem. The new doc based off of the
signed template is not being saved with the signing so the macro
security can be left at high and still run. Maybe there is a way to
programmatically sign the new doc. Let me look into it and test
later today. Getting ready to go to meeting on new potential
project.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Ok, figured it out.
When you create a template, sign it, save it, and then create a
new document off of the signed template, the new document will
run the macros in the template without any issues because the
new document has a reference to your template which is signed.
The problem appears when you add any code to the spawned
document it breaks the trust and the template's macro code will
no longer run. I dont know if you are modifing or adding macro(s)
to the actual document, but this is the only way I was able to
break the trust. Otherwise the events in the spawned document
ran just fine.
HTH and see you in two days.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
That was a good hint. I just started from scratch a new template. And that one works as expected. I'm now going to add all the original functionality and hope that will solve my problem.
I created my template with the previous code in this thread. Then
I signed it. Closed it, ran it, did not save the document. Then I
opened Word and browsed to my template and opened it. Made
some changes by adding a textbox and command button. Added
code behind them to msgbox the contents of the textbox when
the button is clicked. Saved it as a "Template" again. Closed
Word completely down. Then double-clicked the template to
create a new Word document off of the template. The msgboxes
all ran and so did my new code. Signature was still there and
security was set at High.
Maybe your not saving it as a Template but as a
regular "Document"?
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Did you saved a created document and opend the document again? I allways only have trouble with the "open" message box after saving a document I created with the template.
You need to set the Macro security to Medium so it will allow the
macros in the referenced template to run. Then the saved
documents that were created from the template will work the
way you would expect. Go figure!
Now unless we created an Add-In so to speak, we may be able
to work around this.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
In a saved document that was created from my template, it would
not run the macros in the referenced template even though the
document was signed just like the template. Thats when I
checked the security and noticed that the macros were disabled.
So I changed them to Meduim and closed Word and re-opened
the saved document and all was fine. So I think the correct
answer to your question is yes.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.