Log in

View Full Version : Macro to create legal exhibits and schedules



bstephens
09-05-2010, 11:55 PM
I am trying to create a macro that will create a legal "exhibit" or "schedule".

Basically the macro needs to do the following:

1) Insert a non-linked section break at the current selection
2) Apply a Document Building Block called "Exhibit Header" to the header of the newly created section
3) Apply a Document Building Block called "Exhibit Footer" to the footer of the newly created section
4) Adjust the page numbering of the newly created section so that it is page number 1.

I have the following as a start:

Sub CreateDocumentExhibit()
Dim i As Long
Dim j As Long
Dim oDoc As Word.Document
Dim myRng As Word.Range
Set oDoc = ActiveDocument
Dim oRng As Range

Selection.InsertBreak Type:=wdSectionBreakNextPage
'Get the index number of the added section
i = oDoc.Range(0, Selection.Sections(1).Range.End).Sections.Count
With oDoc.Sections(i)
For j = 1 To 3
.Headers(j).LinkToPrevious = False
.Footers(j).LinkToPrevious = False
Next j
End With
'Note: j provides the constant value to unlink the primary page
'(and the first/even page [if exists]) header/footer in the new section
Application.Templates( _
"C:\Users\bts2000\AppData\Roaming\Microsoft\Document Building Blocks\1033\14\Building Blocks.dotx" _
).BuildingBlockEntries("Exhibit").Insert Where:=Selection.Range, RichText _
:=True

Application.Templates( _
"C:\Users\bts2000\AppData\Roaming\Microsoft\Document Building Blocks\1033\14\Building Blocks.dotx" _
).BuildingBlockEntries("Footer (Exhibit)").Insert Where:=Selection.Range, _
RichText:=True

ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

End Sub
but it obviously doesnt apply the building blocks to the header or footer, can't figure out how to apply the building blocks in the right place.

Appreciate guidance on this.

BTW: planning on releasing a free word add-in for lawyers and legal professionals using all the useful information I have learned and gathered here at www.lawyerword.com (http://www.lawyerword.com). I almost have it ready.

gmaxey
09-06-2010, 06:35 AM
Try:

Sub CreateDocumentExhibit()
Dim oDoc As Word.Document
Dim oTmp As Template
Dim oSec As Word.Section
Dim i As Long
Templates.LoadBuildingBlocks
Set oTmp = Templates("C:\Documents and Settings\Greg Maxey\Application Data\Microsoft\Document Building Blocks\1033\Building Blocks.dotx")
With oSec
For i = 1 To 3
.Headers(i).LinkToPrevious = False
.Footers(i).LinkToPrevious = False
oTmp.BuildingBlockTypes(wdTypeQuickParts).Categories("General").BuildingBlocks("Test").Insert .Headers(i).Range, True
oTmp.BuildingBlockTypes(wdTypeQuickParts).Categories("General").BuildingBlocks("Test").Insert .Footers(i).Range, True
Next i
End With
Set oDoc = Nothing
Set oSect = Nothing
Set oTmm = Nothing
End Sub

bstephens
09-06-2010, 12:42 PM
Thanks Greg, tried this slightly revised version where the only revision is the reference to the building blocks themselves:

Sub CreateDocumentExhibitTEST()
Dim oDoc As Word.Document
Dim oTmp As Template
Dim oSec As Word.Section
Dim i As Long
Templates.LoadBuildingBlocks
Set oTmp = Templates("C:\Users\bts2000\AppData\Roaming\Microsoft\Document Building Blocks\1033\14\Building Blocks.dotx")
With oSec
For i = 1 To 3
.Headers(i).LinkToPrevious = False
.Footers(i).LinkToPrevious = False
oTmp.BuildingBlockTypes(wdTypeQuickParts).Categories("General").BuildingBlocks("Exhibit").Insert .Headers(i).Range, True
oTmp.BuildingBlockTypes(wdTypeQuickParts).Categories("General").BuildingBlocks("Footer (Exhibit)").Insert .Footers(i).Range, True
Next i
End With
Set oDoc = Nothing
Set oSect = Nothing
Set oTmm = Nothing
End Sub

but when I run it I get the following error:


Run-time error '91:
Object variable or With block variable not set

When I hit debug the code highlights in yellow to:

.Headers(i).LinkToPrevious = False

gmaxey
09-06-2010, 01:56 PM
I was testing you. No sorry, I forgot to create the new section:
Sub CreateDocumentExhibitTEST()
Dim oDoc As Word.Document
Dim oTmp As Template
Dim oSec As Word.Section
Dim i As Long
Templates.LoadBuildingBlocks
oTmp = Templates("C:\Users\bts2000\AppData\Roaming\Microsoft\Document Building Blocks\1033\14\Building Blocks.dotx")
Set oSec = Selection.Sections.Add(Selection.Range)
With oSec
For i = 1 To 3
.Headers(i).LinkToPrevious = False
.Footers(i).LinkToPrevious = False
oTmp.BuildingBlockTypes(wdTypeQuickParts).Categories("General").BuildingBlocks("Exhibit").Insert .Headers(i).Range, True
oTmp.BuildingBlockTypes(wdTypeQuickParts).Categories("General").BuildingBlocks("Footer (Exhibit)").Insert .Footers(i).Range, True
Next i
End With
Set oDoc = Nothing
Set oSec = Nothing
Set oTmp = Nothing
End Sub

bstephens
09-13-2010, 03:44 PM
Hi Greg, I can't get the code as posted to work nor can I figure out where it is going wrong.

Code:

Sub CreateDocumentExhibitTEST()
Dim oDoc As Word.Document
Dim oTmp As Template
Dim oSec As Word.Section
Dim i As Long
Templates.LoadBuildingBlocks
Set oTmp = Templates("C:\Users\Brian\AppData\Roaming\Microsoft\Document Building Blocks\1033\14\Building Blocks.dotx")
With oSec
For i = 1 To 3
.Headers(i).LinkToPrevious = False
.Footers(i).LinkToPrevious = False
oTmp.BuildingBlockTypes(wdTypeQuickParts).Categories("General").BuildingBlocks("SK Exhibit").Insert .Headers(i).Range, True
oTmp.BuildingBlockTypes(wdTypeQuickParts).Categories("General").BuildingBlocks("SK Exhibit").Insert .Footers(i).Range, True
Next i
End With
Set oDoc = Nothing
Set oSect = Nothing
Set oTmm = Nothing
End Sub

I confirmed that "C:\Users\Brian\AppData\Roaming\Microsoft\Document Building Blocks\1033\14\Building Blocks.dotx" is a valid reference to my building blocks file (obviously I would generalize with an environment variable for the final version), I also confirmed that "SK Exhibit" is saved in "Building Blocks.dotx" in gallery "Headers" in category "General" and that "SK Exhibit" is also saved in "Building Blocks.dotx" in gallery "Footers" in category "General".

However, I still get the following error:


Run-time error '91':

Object variable or With block variable not set

When I hit debug I get the following code highlighted in yellow:

.Headers(i).LinkToPrevious = False

Any suggestions?

bstephens
09-13-2010, 03:50 PM
Also tried the following:

Sub CreateDocumentExhibitTEST()
Dim oDoc As Word.Document
Dim oTmp As Template
Dim oSec As Word.Section
Dim i As Long
Templates.LoadBuildingBlocks
Set oTmp = Templates("C:\Users\Brian\AppData\Roaming\Microsoft\Document Building Blocks\1033\14\Building Blocks.dotx")
With oSec
For i = 1 To 3
.Headers(i).LinkToPrevious = False
.Footers(i).LinkToPrevious = False
oTmp.BuildingBlockTypes(wdTypeCustomHeaders).Categories("General").BuildingBlocks("SK Exhibit").Insert .Headers(i).Range, True
oTmp.BuildingBlockTypes(wdTypeCustomFooters).Categories("General").BuildingBlocks("SK Exhibit").Insert .Footers(i).Range, True
Next i
End With
Set oDoc = Nothing
Set oSect = Nothing
Set oTmm = Nothing
End Sub


where I changed the type to "wdTypeCustomHeaders" and "wdTypeCustomFooters" but still no go...

gmaxey
09-13-2010, 04:09 PM
You still haven't added the line of code that creates oSec:

Set oSec = Selection.Sections.Add(Selection.Range)

fumei
09-14-2010, 12:51 PM
Are you using Option Explicit?

Make sure you are, because (unless there is a typo) you set oSect = Nothing...but the object is actually oSec (no "t").

As Greg has pointed out, you have not Set oSec. So, because there is no object until it is set, an instruction using:

With oSec

will fail, because: Object variable or With block variable not set