top of page

110. Auto-Save Macros

Description

Word and WordPerfect macros for automatically saving your document after assembly.

Code

Explanation

The macros below will automatically save a document after assembly. The macros will work "as-is" (as-are?). Simply save the appropriate macro to the macro directory (WordPerfect) or to a global add-in template (Word). The WordPerfect macro is also available for download.

Quick Overview: These macros are all based on the same concept. The user is prompted for the document's filename by HotDocs during assembly. This is just a HotDocs text variable, called TextVar in the explanations below. This text variable is placed somewhere in the template surrounded by a bookmark. When assembly is complete, the macro looks for the bookmark, extracts the value of TextVar from within it, and saves the document using this value as the filename.

Directory: You should be able to add directory-selecting functionality fairly easily. One option is to create a HotDocs multiple choice variable for the directory and include that variable within the bookmark with TextVar:

[Bookmark]«Directory»«TextVar»[Bookmark]

The second Word macro below demonstrates how to have your macro determine the directory, and additionally how to add an entry for the document in a Lawbase database.

Invoking the Macro: The macros are all invoked in the same manner, regardless of whether you use Word or WordPerfect. To invoke the macro you must use a PLAY instruction at the end of the document. In other words, make sure that the PLAY is the very last thing in the document. Otherwise, the saved document will be incomplete. To create a PLAY instruction, follow the directions found in Computation #0108: Running a Macro During Assembly.


Code
Sub SaveDoc()

Dim DocNameStr As String
Dim DocPathStr As String
Dim DocNameChoppedStr As String
Dim DocDriveLtr
Dim nextSerial

' Find the bookmark called DocumentName to get the document's save name
Selection.GoTo What:=wdGoToBookmark, Name:="DocumentName"

' Select all of the text in the DocumentName line
Selection.EndKey Unit:=wdLine, Extend:=wdExtend

' Dump the searched text into a string for use in saving the file
DocNameStr = Selection.Text
DocNameChoppedStr = Selection.Text

' Use the last name of the person to whom the document is created to
' pick the directory where it will be stored
DocDriveLtr = Left(DocNameChoppedStr, 1)

' Set the string of the drive and path to where the document will be saved
' !! MODIFY THIS TO MATCH YOUR DRIVE CONFIGURATION !!
DocPathStr = "//SERVERNAME/SomePath/" & DocDriveLtr & "/"

' Delete the bookmark from the document
Selection.Delete

' Save the file using the directory that was specified above and the name
' that is designated by DocNameStr
With Dialogs(wdDialogFileSaveAs)
.Name = DocPathStr & DocNameStr & "_"
.Show
End With

' Begin section to set office link and throw the link to the document
' in there as well
Selection.GoTo What:=wdGoToBookmark, Name:="DocuSerial"

' Get the saved documents name and path
DocNameStr = ""
DocPathStr = ""
DocNameStr = ActiveDocument.Name
DocPathStr = ActiveDocument.Path

Dim SerialNum
Dim DBLawbase
Dim dsSerialNum
Dim dsAddOfficeLink

SerialNum = Trim(Selection)

' Create connection object to connect to the database
Set DBLawbase = CreateObject("ADODB.Connection")
DBLawbase.connectionstring = "DSN=Lawbase;uid=sa;pwd=sa;"
DBLawbase.Open

' Get last serial number so we can add one to it
Set dsSerialNum = CreateObject("adodb.Recordset")
nextSerial = "select max(serial) + 1 as newserial from office_link"
dsSerialNum.Open nextSerial, DBLawbase, 3, 3

' Insert all values into the officelink table for the serial in question
Set dsAddOfficeLink = CreateObject("ADODB.RecordSet")
SQL = "insert into office_link values (" & dsSerialNum.Fields("newserial") & ", " & SerialNum & ", 0, 0, '" & DocPathStr & "/" & DocNameStr & "', '" & DocNameStr & "', getdate(), 'AUTO', 'Word')"
dsAddOfficeLink.Open SQL, DBLawbase, 3, 3

' Kill serial number off of document
Selection.Delete

' Resave after cleaning
ActiveDocument.Save

' Cleanup
dsSerialNum.Close
Set dsSerialNum = Nothing
'dsAddOfficeLink.Close
Set dsAddOfficeLink = Nothing
DBLawbase.Close
Set DBLawbase = Nothing

End

End Sub


Explanation
Our frim uses Lawbase as a frontend w/ a SQL Server backend. Since there is very little custom coding for Lawbase, I wrote this to simplify the ever-enthralling task of choosing the correct directory to save the document in and linking the document to the matter in our database. We use a directory with subdirectories of the alphabet. For example, documents involving John Doe would go in:

//SERVER/SomePath/D/...

I think it is eaisier to use DOS names instead of long file names.

First thing anyone wanting to use this code needs to do is insert two bookmarks into their document. The first is called DocumentName, and the second DocuSerial. Here is the format I use (you will want to use your own naming conventions):

DocumentName - [Bookmark]«lb_link_header_client_lname», «lb_link_header_client_fname», «lb_template», «TODAY:3 June 1990»[Bookmark]
DocuSerial - [Bookmark] «lb_header_serial» [Bookmark]

NOTE: «lb_header_serial» in DocuSerial must be padded with spaces on either side if it is not long enough to be a Bookmark.

I just make the text white so that it will not be visible in the document and will not confuse anyone.

©2023 by My Site. Proudly created with Wix.com

  • Facebook
  • Twitter
  • LinkedIn
bottom of page