sbsbats
asked on
Creating an AutoExec() macro from existing macro
HI,
I have an existing macro and little VB experiance - What i am trying to achieve is the following - The macro run's whenever the save button is clicked on a Word document - this macro opens a dialog box and forces the user to select one of two buttons (close button has been disabled, the 2 buttons add a property to the document when clicked)
What I now want to do is store this macro as an add-in, maybe if needed renaming to AutoExec()? as I want this macro to run everytime the save button is clicked regardless of the document.
my code is in a number of places and I do not know how to rename my macro, or how to combine this code into a new macro - I would rather it ran as an add-in rather than a macro stored in normal.dot
Please remeber that my VB experiance is limited so please use examples if possible
I have an existing macro and little VB experiance - What i am trying to achieve is the following - The macro run's whenever the save button is clicked on a Word document - this macro opens a dialog box and forces the user to select one of two buttons (close button has been disabled, the 2 buttons add a property to the document when clicked)
What I now want to do is store this macro as an add-in, maybe if needed renaming to AutoExec()? as I want this macro to run everytime the save button is clicked regardless of the document.
my code is in a number of places and I do not know how to rename my macro, or how to combine this code into a new macro - I would rather it ran as an add-in rather than a macro stored in normal.dot
Please remeber that my VB experiance is limited so please use examples if possible
4 Parts to the macro:
"ThisDocument" code:
Private Sub Document_New()
Call Register_Event_Handler
End Sub
Private Sub Document_Open()
Call Register_Event_Handler
End Sub
"UserForm1" code:
(disable close button)
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
(button click code to add the property)
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
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
"Module1" code:
Dim X As New Class1
Public Sub Register_Event_Handler()
Set X.App = Word.Application
End Sub
"Class 1" code:
Public WithEvents App As Word.Application
Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
UserForm1.Show
End 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
Hello,
I have tried the above - this is my code thast almost working - it dosn't crash but when you click save it says "compile error in hidden module: module2" - code =
Sub FileSave()
Private Sub App_DocumentBeforeSave(ByV al Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
UserForm1.Show
End Sub
I have tried the above - this is my code thast almost working - it dosn't crash but when you click save it says "compile error in hidden module: module2" - code =
Sub FileSave()
Private Sub App_DocumentBeforeSave(ByV
UserForm1.Show
End Sub
ASKER
Hello again,
The problem is related to the DocumentBeforeSave sub but this is essential as I need the save dialog box to, load after the form selection has been made
The problem is related to the DocumentBeforeSave sub but this is essential as I need the save dialog box to, load after the form selection has been made
I was just refering to a way to create a Word addin. By creating a Word template containing macro's within the Word Startup folder, it will be detected by Word as an Addin. This removes the need of your macro code to be within normal.dot.
The macro's I posted just have to be inside a module within that addin. They may be in module1, they may be in module2, they may be in module 'MyModule'. What ever pleases your ordering needs. The only requirement is, that they must be publicly available. A module is public by default. So I won't go further in to details on this.
The nature of those macro names, is that they are recognised by Word as AutoExec macro's. So when your macro's are in place, and you save your document, these macro's will be called. This means you can customise the way Word saves. These macro's remove the need for the eventhandler.
In the attached code are 2 samples this time. The 1st is how Word saves the normal way, but then written in macrocode. The 2nd is the FileSaveAs macro the way I believe you need it. You have to put or call your own on the right positions in this. Since I don't know much about window handles and all.
I hope it's more clear this time. Please let me know.
The macro's I posted just have to be inside a module within that addin. They may be in module1, they may be in module2, they may be in module 'MyModule'. What ever pleases your ordering needs. The only requirement is, that they must be publicly available. A module is public by default. So I won't go further in to details on this.
The nature of those macro names, is that they are recognised by Word as AutoExec macro's. So when your macro's are in place, and you save your document, these macro's will be called. This means you can customise the way Word saves. These macro's remove the need for the eventhandler.
In the attached code are 2 samples this time. The 1st is how Word saves the normal way, but then written in macrocode. The 2nd is the FileSaveAs macro the way I believe you need it. You have to put or call your own on the right positions in this. Since I don't know much about window handles and all.
I hope it's more clear this time. Please let me know.
' The way word saves documents by default
Sub FileSave()
ActiveDocument.Save
End Sub
Sub FileSaveAs()
Dialogs(wdDialogFileSaveAs).Show
End Sub
' SaveAs Alternative
Sub FileSaveAs()
Dim d As Dialog
Set d = Dialogs(wdDialogFileSaveAs)
Load d
' Save form is loaded now, you can disable your close button here
If d.Show = vbOK Then
' OK button pressed
Else
' Close or Cancel button pressed
End If
Unload d
End Sub
I'm really sorry, a screwup on my part. Built-in forms can't be Loaded the it's done in my macro. You have to remove that part. See attached code.
I hope it's still useful for you without the load.
I hope it's still useful for you without the load.
' SaveAs Alternative
Sub FileSaveAs()
Dim d As Dialog
Set d = Dialogs(wdDialogFileSaveAs)
If d.Show = vbOK Then
' OK button pressed
Else
' Close or Cancel button pressed
End If
Unload d
End Sub
ASKER
Cheers, I managed to stumble around and get this going - thanks for steering me in the right direction
ASKER
What code would I use for the FileSave sub? - the class displays the form before a document is saved - is this the code to use?
Public WithEvents App As Word.Application
Private Sub App_DocumentBeforeSave(ByV
UserForm1.Show
End Sub