Jump to content

How to execute a script upon saving ?


Recommended Posts

I'm pretty new to visual basic... :unsure: I've done script / batch writing in AutoIt and MSDOS.

I've written a macro in Winword 2003 that works manually and I want it to be executed when a user executes SaveAs.

The macro has to be executed exactly between the time that the person has entered the filename and the closing of the file.

I'll explain this in more detail:

The footer of the Winword file I am working on contains a table with fields linked to custom docProperties: Machine number, Filename and Version. In our template, these custom docProperties just have xxx placeholders in them.

We usually take the time to enter the machine number, version and file type in the docProperties before we close the file because certain fields in the document are linked to the docProperties.

The filename that we use to save the file itself already consists of the filetype, machine number and version.

I want to save some of our colleagues some time by generating the footer automatically.

I did manage to get all this done in a macro HOWEVER the macro works fine in my file when I execute it manually, but I want it to be executed when the user either changes the filename or saves it for the very first time, so that it really is generated automatically, every time someone changes the name of the file or saves a new file.

Can someone explain shortly or in depth, how to execute a macro during the saving of a file?

I'd really appreciate it.

Thanks.

The script below the following line need not be read. It's just for those people curious about the script and probably has little to do with how to execute script during the execution of SaveAs.

_____________________________________________________________________________________________

Sub SetMachNo()

' ****************************************************************************************************

********

'***** PRIVATE DATA CENSORED *****

' This SubRoutine passes the Custom Property Name "MachNo", Value, and Property Type to the'

' SetProp subroutine. Parts of the routine were taken from Microsoft at support.microsoft.com/kb/212618/en-us'

' ****************************************************************************************************

********

' Generates the custom property "MachNo (hereafter Maschinennummer in German)" out of the file name.'

' The msoPropertyTypeString constant specifies the type of property, and must be included.'

SetProp "Maschinennummer", Mid(ActiveDocument.Name, 3, 6), _

msoPropertyTypeString

End Sub

Sub SetVersion()

' ****************************************************************************************************

********

'***** PRIVATE DATA CENSORED *****

' This SubRoutine passes the Custom Property Name "Version", Value, and Property Type to the

' SetProp subroutine.'Parts of the routine were taken from Microsoft© at support.microsoft.com/kb/212618/en-us'

' ****************************************************************************************************

********

' Generates the custom property "Version" out of the file name.'

' The msoPropertyTypeString constant specifies the type of property, and must be included.'

Dim Version As String

VersionFirstPart = Mid(ActiveDocument.Name, 9, 1)

'Defines the first character of "Version" to be the ninth character of the file name'

VersionSecondPart = Mid(ActiveDocument.Name, 10, 1)

'Defines the third character of "Version" to be the tenth character of the file name'

ThisVersion = Mid(ActiveDocument.Name, 9, 1) & "." & Mid(ActiveDocument.Name, 10, 1)

'Combines VersionFirstPart with VersionSecondPart - but with the "." (period) in the middle'

SetProp "Version", ThisVersion, _

msoPropertyTypeString

End Sub

Sub SetProp(CDPName As String, CDPValue As Variant, Optional _

CDPType As Long)

' **********************************************************************************'

' This routine was published by Microsoft© and retrieved by *****CENSORED*****'

' For further information, see http://support.microsoft.com/kb/212618/en-us'

' The SetProp routine checks to see whether the Custom Document Property pre-exists.'

' If it exists, then it adds the new value. If it does not exist, it creates the new'

' property and adds the new value.'

' **********************************************************************************'

' Make sure the optional argument CDPType is set.

' If it is missing, make it a string value.

Dim oCDP, oProp, msg

If IsMissing(CDPType) Then

CDPType = msoPropertyTypeString

End If

Set oCDP = ActiveDocument.CustomDocumentProperties

' Compare each custom document property to the

' property you want to create to see whether it exists.

For Each oProp In oCDP

' If the Custom Property exists...

If oProp.Name = CDPName Then

With oProp

' ...the custom property Type you are setting

' must match the pre-existing custom property.

If .Type <> CDPType Then

msg = "The custom property types do not match."

msg = msg + " Custom property not set."

MsgBox msg

' End the routine.

Exit Sub

End If

.LinkToContent = False

' Set the new value.

.Value = CDPValue

End With

' A match was found, so exit the routine.

Exit Sub

End If

Next oProp

' No match was found. Create a new property and value.

oCDP.Add Name:=CDPName, Value:=CDPValue, Type:=CDPType, _

LinkToContent:=False

End Sub

Edited by Dislocated Time
Link to comment
Share on other sites


Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...