grNadpa
grNadpa

Reputation: 79

Positioning cursor before inserting building block and while avoiding page break afterwards

I have a full-page Building Block I saved through the Building Blocks Organizer as name: "aWaiver" in my custom category: "Waivers" in the "Quick Parts" Gallery with Building Block Property: "insert content on its own page".

Two issues: (1) I cannot seem to discover the proper "Selected.Range" VBA code to use to locate to the bottom of the current (last or only) page where I want to insert my aWaiver building block. (2) When I manually locate to the bottom of my document and insert the aWaiver, it does begin its own page, but then inserts another following blank page.

Here is vba code I modified from the macro I recorded that seems to work albeit with the above issues.

        "C:\Users\Brian\AppData\Roaming\Microsoft\Templates\Normal.dotm"). _
        BuildingBlockEntries("aWaiver").Insert Where:=Selection.Range, _
        RichText:=True```

Upvotes: 0

Views: 163

Answers (2)

grNadpa
grNadpa

Reputation: 79

I’m reluctant to post given the recommendation that I gain Word literacy. But this post does appear in results for various key words in a Google Search. So, I will share my solution knowing that it lacks best practices.

At the end of my last paragraph in my prototype document I added a bkPageBreakHere bookmark. I then saved the selection in the Quick Parts gallery as a new Category Waivers as aPartialWaiver with Option insert content only (turns out that insert content as its own page does the page break after, not before)

Turning to the VBA Code, I found that Building Blocks did not automatically load with my module. So into the document I entered:

Private Sub Document_New()
'load Building Blocks
    Application.Templates.LoadBuildingBlocks
'Present Waiver Form
    formCombined.Show

End Sub

Private Sub Document_Open()
'load Building Blocks
    Application.Templates.LoadBuildingBlocks
'Present Waiver Form
    formCombined.Show
End Sub

Finally, in my form:

Private Sub CommandNext_Click()

                 ...
   Selection.GoTo what:=wdGoToBookmark, Name:="bkPageBreakHere"  'Position cursor
   Selection.InsertBreak Type:=wdPageBreak                       'Start new page                                           
'load next document
    Application.Templates( _
        "C:\Users\" & Environ("username") & "\AppData\Roaming\Microsoft\Document Building Blocks\1033\16\Building Blocks.dotx" _
        ).BuildingBlockEntries("aPartialWaiver").Insert Where:=Selection.range, _
        RichText:=True
                 ...

Issues remain. (1) Despite everything, I still get an empty page after my last legitimate one. (No, I do NOT execute the above code section on my last page.) (2) I get the “Security warning Macros have been disabled” when invoking the document from a template.

But all in all, the client is satisfied. I’ll continue my flailing to find a solution. Any thoughts welcome but not expected.

Upvotes: 1

Timothy Rylatt
Timothy Rylatt

Reputation: 7850

  1. When writing VBA the Selection object is only used in exceptionally rare circumstances when it is unavoidable. To add your Building Block at the end of the document you can use Where:=Document.Characters.Last
  2. The Building Block should be stored in your document template, not Normal.dotm, otherwise you will not be able to distribute it to other users.
  3. The issues with the blank page are a result of the content that you have included in the Building Block.

Upvotes: 1

Related Questions