sbsbats
asked on
Run macro from normal.dot
HI,
I have created a macro that opens a custom form when the user clicks save on the template - this form loads before save and has the close button disabled so you have to click one of 2 buttons before you can save the document (its adds a property to the document in the categories field, either secure or non-secure).
What I am trying to do now and have never tried before is I want to be able to add this macro to normal.dot so the macro runs for all documents.
I want to use the Document_New() procedure but am not sure what part of my code needs to be transfered to this procedure so the macro runs when a new document is created.
I have created a macro that opens a custom form when the user clicks save on the template - this form loads before save and has the close button disabled so you have to click one of 2 buttons before you can save the document (its adds a property to the document in the categories field, either secure or non-secure).
What I am trying to do now and have never tried before is I want to be able to add this macro to normal.dot so the macro runs for all documents.
I want to use the Document_New() procedure but am not sure what part of my code needs to be transfered to this procedure so the macro runs when a new document is created.
Code that runs from the 2 buttons -
Private Sub nonsecureButton_Click()
Call WriteProp(sPropName:="Category", sValue:="Non-Secure")
Unload UserForm1
End Sub
Private Sub SecureButton_Click()
Call WriteProp(sPropName:="Category", sValue:="Secure")
Unload UserForm1
End Sub
WriteProp procedure -
Public Sub WriteProp(sPropName As String, sValue As String, Optional lType As Long = msoPropertyTypeString)
Dim bCustom As Boolean
On Error GoTo ErrHandlerWriteProp
ActiveDocument.BuiltInDocumentProperties(sPropName).Value = sValue
Exit Sub
Proceed:
bCustom = True
Custom:
ActiveDocument.CustomDocumentProperties(sPropName).Value = sValue
Exit Sub
AddProp:
On Error Resume Next
ActiveDocument.CustomDocumentProperties.Add Name:=sPropName, LinkToContent:=False, Type:=lType, Value:=sValue
If Err Then
Debug.Print "The Property " & Chr(34) & sPropName & Chr(34) & " couldn't be written, because " & Chr(34) & sValue & Chr(34) & " is not a valid value for the property type"
End If
Exit Sub
ErrHandlerWriteProp:
Select Case Err
Case Else
Err.Clear
If Not bCustom Then
Resume Proceed
Else
Resume AddProp
End If
End Select
End Sub
Current Document_New() procedure -
Private Sub Document_New()
Call Register_Event_Handler
End Sub
Event Handler -
Dim X As New Class1
Public Sub Register_Event_Handler()
Set X.App = Word.Application
End Sub
ASKER
HI,
I have not explained myself well here - all I want to do id run this macro from the DocumentNew() procedure and was looking for the syntax I need to do this -
I have not explained myself well here - all I want to do id run this macro from the DocumentNew() procedure and was looking for the syntax I need to do this -
You need to name the macro AutoNew and save it in a template (eg normal.dot).
Then when a new document is created from that template the macro will run.
This should help with specifics:
http://word.mvps.org/faqs/macrosvba/documenteventscontent.htm
Then when a new document is created from that template the macro will run.
This should help with specifics:
http://word.mvps.org/faqs/macrosvba/documenteventscontent.htm
ASKER
HI,
I keep getting told to rename the macro AutoNew - what do I rename? - my macro has 1 class module, 1 module, 1 documentNew and a userform - what do I rename? and how
I keep getting told to rename the macro AutoNew - what do I rename? - my macro has 1 class module, 1 module, 1 documentNew and a userform - what do I rename? and how
Basically if you have a macro called:
Sub Macro1()
Rename it to Sub AutoNew()
Sub Macro1()
Rename it to Sub AutoNew()
ASKER
HI,
I think this is were the communication is breaking down - you say to remane my macro but I don't know what my macros called or how to find that out - my macro consists of a number of forms, modules and classes - what exactly do I rename?
I think this is were the communication is breaking down - you say to remane my macro but I don't know what my macros called or how to find that out - my macro consists of a number of forms, modules and classes - what exactly do I rename?
Create a new macro:
Sub AutoNew()
Copy your code into it.
At the bottom ensure End Sub is present.
Sub AutoNew()
Copy your code into it.
At the bottom ensure End Sub is present.
Sub AutoNew()
' Copy your code below
End Sub
ASKER
Hello,
I think you may be overestimating my VB skills - my programming experiance is Java and I am struggling with the VB setup - you asked me to create a new macro - do you mean in my existing project?
my macro has code all over the place - I have a class, a form that has two buttons that run events and code in my main module - what exactly would I paste into this new macro?
I think you may be overestimating my VB skills - my programming experiance is Java and I am struggling with the VB setup - you asked me to create a new macro - do you mean in my existing project?
my macro has code all over the place - I have a class, a form that has two buttons that run events and code in my main module - what exactly would I paste into this new macro?
Where do you have the forms, buttons, classes, etc saved?
Are they part of an existing document?
Open Normal.dot.
Go into Macros (Tools >Macros in Word 2003. Developer ribbon in 2007)
Create a new Macro. It should automatically call it Macro1 and put the open and close lines in.
Replace the name Macro1 with AutoNew
Inside this call the first part of your code that starts the process (the form??).
Save as normal.dot
Are they part of an existing document?
Open Normal.dot.
Go into Macros (Tools >Macros in Word 2003. Developer ribbon in 2007)
Create a new Macro. It should automatically call it Macro1 and put the open and close lines in.
Replace the name Macro1 with AutoNew
Inside this call the first part of your code that starts the process (the form??).
Save as normal.dot
ASKER
Hi,
The above sounds good - the issue now is what to call in my AutoNew() - the form is loaded from the Class 1 module:
Public WithEvents App As Word.Application
Private Sub App_DocumentBeforeSave(ByV al Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
UserForm1.Show
End Sub
Use this code?
The following is the UserForm code:
Private Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Declare Function GetWindowLong Lib "user32" _
Alias "GetWindowLongA" (ByVal hWnd As Long, _
ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" _
Alias "SetWindowLongA" (ByVal hWnd As Long, _
ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Const GWL_STYLE = (-16)
Const WS_SYSMENU = &H80000
Private Sub nonsecureButton_Click()
Call WriteProp(sPropName:="Cate gory", sValue:="Non-Secure")
Unload UserForm1
End Sub
Private Sub SecureButton_Click()
Call WriteProp(sPropName:="Cate gory", sValue:="Secure")
Unload UserForm1
End Sub
Private Sub UserForm_Click()
End Sub
Public Sub WriteProp(sPropName As String, sValue As String, Optional lType As Long = msoPropertyTypeString)
Dim bCustom As Boolean
On Error GoTo ErrHandlerWriteProp
ActiveDocument.BuiltInDocu mentProper ties(sProp Name).Valu e = sValue
Exit Sub
Proceed:
bCustom = True
Custom:
ActiveDocument.CustomDocum entPropert ies(sPropN ame).Value = sValue
Exit Sub
AddProp:
On Error Resume Next
ActiveDocument.CustomDocum entPropert ies.Add Name:=sPropName, LinkToContent:=False, Type:=lType, Value:=sValue
If Err Then
Debug.Print "The Property " & Chr(34) & sPropName & Chr(34) & " couldn't be written, because " & Chr(34) & sValue & Chr(34) & " is not a valid value for the property type"
End If
Exit Sub
ErrHandlerWriteProp:
Select Case Err
Case Else
Err.Clear
If Not bCustom Then
Resume Proceed
Else
Resume AddProp
End If
End Select
End Sub
Private Sub UserForm_Initialize()
'this hides the X on the caption line
Dim hWnd As Long, a As Long
Dim V As Integer
V = CInt(Left(Application.Vers ion, _
InStr(Application.Version, ".")))
'If V = 8 this is Word 97
If V = 8 Then
hWnd = FindWindow("ThunderXFrame" , Me.Caption)
Else
hWnd = FindWindow("ThunderDFrame" , Me.Caption)
End If
a = GetWindowLong(hWnd, GWL_STYLE)
SetWindowLong hWnd, GWL_STYLE, a And Not WS_SYSMENU
End Sub
The above sounds good - the issue now is what to call in my AutoNew() - the form is loaded from the Class 1 module:
Public WithEvents App As Word.Application
Private Sub App_DocumentBeforeSave(ByV
UserForm1.Show
End Sub
Use this code?
The following is the UserForm code:
Private Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Declare Function GetWindowLong Lib "user32" _
Alias "GetWindowLongA" (ByVal hWnd As Long, _
ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" _
Alias "SetWindowLongA" (ByVal hWnd As Long, _
ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Const GWL_STYLE = (-16)
Const WS_SYSMENU = &H80000
Private Sub nonsecureButton_Click()
Call WriteProp(sPropName:="Cate
Unload UserForm1
End Sub
Private Sub SecureButton_Click()
Call WriteProp(sPropName:="Cate
Unload UserForm1
End Sub
Private Sub UserForm_Click()
End Sub
Public Sub WriteProp(sPropName As String, sValue As String, Optional lType As Long = msoPropertyTypeString)
Dim bCustom As Boolean
On Error GoTo ErrHandlerWriteProp
ActiveDocument.BuiltInDocu
Exit Sub
Proceed:
bCustom = True
Custom:
ActiveDocument.CustomDocum
Exit Sub
AddProp:
On Error Resume Next
ActiveDocument.CustomDocum
If Err Then
Debug.Print "The Property " & Chr(34) & sPropName & Chr(34) & " couldn't be written, because " & Chr(34) & sValue & Chr(34) & " is not a valid value for the property type"
End If
Exit Sub
ErrHandlerWriteProp:
Select Case Err
Case Else
Err.Clear
If Not bCustom Then
Resume Proceed
Else
Resume AddProp
End If
End Select
End Sub
Private Sub UserForm_Initialize()
'this hides the X on the caption line
Dim hWnd As Long, a As Long
Dim V As Integer
V = CInt(Left(Application.Vers
InStr(Application.Version,
'If V = 8 this is Word 97
If V = 8 Then
hWnd = FindWindow("ThunderXFrame"
Else
hWnd = FindWindow("ThunderDFrame"
End If
a = GetWindowLong(hWnd, GWL_STYLE)
SetWindowLong hWnd, GWL_STYLE, a And Not WS_SYSMENU
End Sub
Yes. It looks like you would use the class 1 module as you mentioned above.
ASKER
HI,
I created the AutoNew macro and pasted in the following code -
Sub AutoNew()
Private Sub App_DocumentBeforeSave(ByV al Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
UserForm1.Show
End Sub
End Sub
Still no go (no error just dosn't load the form when the user clicks the save button)
I created the AutoNew macro and pasted in the following code -
Sub AutoNew()
Private Sub App_DocumentBeforeSave(ByV
UserForm1.Show
End Sub
End Sub
Still no go (no error just dosn't load the form when the user clicks the save button)
Where is the rest of your code saved?
ASKER
HI,
The buttons, forms and modules are stored in a template called "prop testing.dot" - I need to transfer this code to the new AutoNew macro but am unsure where to paste this code - do I gather all the code and just paste it inot the AutoNew sub?
The buttons, forms and modules are stored in a template called "prop testing.dot" - I need to transfer this code to the new AutoNew macro but am unsure where to paste this code - do I gather all the code and just paste it inot the AutoNew sub?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
HI and cheers for your advice - I went the addin way instead in the end
Add this macro to it.
Hold shift then select File > Save all