Solved

Run macro from normal.dot

Posted on 2009-04-01
16
577 Views
Last Modified: 2013-12-20
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.
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

Open in new window

0
Comment
Question by:sbsbats
  • 8
  • 8
16 Comments
 
LVL 18

Expert Comment

by:Rartemass
ID: 24044732
You can open word with a blank document.
Add this macro to it.
Hold shift then select File > Save all
0
 

Author Comment

by:sbsbats
ID: 24045324
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 -
0
 
LVL 18

Expert Comment

by:Rartemass
ID: 24045565
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
0
 

Author Comment

by:sbsbats
ID: 24073233
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
0
 
LVL 18

Expert Comment

by:Rartemass
ID: 24073379
Basically if you have a macro called:
Sub Macro1()

Rename it to Sub AutoNew()
0
 

Author Comment

by:sbsbats
ID: 24073484
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?
0
 
LVL 18

Expert Comment

by:Rartemass
ID: 24073522
Create a new macro:
Sub AutoNew()

Copy your code into it.

At the bottom ensure End Sub is present.

Sub AutoNew()

' Copy your code below
 

End Sub

Open in new window

0
 

Author Comment

by:sbsbats
ID: 24073817
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?

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 18

Expert Comment

by:Rartemass
ID: 24073976
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

0
 

Author Comment

by:sbsbats
ID: 24074009
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(ByVal 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:="Category", sValue:="Non-Secure")
Unload UserForm1
End Sub

Private Sub SecureButton_Click()
Call WriteProp(sPropName:="Category", 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.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

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.Version, _
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

0
 
LVL 18

Expert Comment

by:Rartemass
ID: 24074155
Yes. It looks like you would use the class 1 module as you mentioned above.
0
 

Author Comment

by:sbsbats
ID: 24074201
HI,

I created the AutoNew macro and pasted in the following code -
Sub AutoNew()

Private Sub App_DocumentBeforeSave(ByVal 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)
0
 
LVL 18

Expert Comment

by:Rartemass
ID: 24074247
Where is the rest of your code saved?
0
 

Author Comment

by:sbsbats
ID: 24074289
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?
0
 
LVL 18

Accepted Solution

by:
Rartemass earned 500 total points
ID: 24074382
I don't think that will work.
Keep the other code in their separate subs, but copy them from 'prop testing.dot' to normal.dot.

If you open Word the normal.dot is opened. You can see all the macros loaded. You need to add in your individual macros here. Then add in the AutoNew() macro that loads the first part of your code as you did above.

I think it failed to do anything before because the rest of the code wasn't in the template.
0
 

Author Closing Comment

by:sbsbats
ID: 31565558
HI and cheers for your advice - I went the addin way instead in the end
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now