PDA

View Full Version : [SOLVED:] How to insert 2 building blocks in 2 different places in a Word document at same time



PicklePBFH
10-25-2015, 11:22 AM
I am developing a legal document in which I need to provide the user with the option of inserting additional clauses if required - each option consists of two parts, namely a definition clause and an operational clause. The definition clause must be included in the Definitions section of the document while the operational clause is included later in the document. I have created the building blocks as well as the code to insert each building block separately in the document by indicating where user must position cursor.

I have two requirements:
Firstly, is there a way the two building blocks for an option can be inserted simultaneously?

Secondly, in the first option, the first clause (building block) cross references the second clause (building block) - Before I record the building block, I insert the cross reference - however, the cross reference is lost after inserting both building blocks for that option.

I would really appreciate any help in solving these issues.

Lindsey

PicklePBFH
10-28-2015, 11:32 AM
Hi everyone :yes

I have managed to resolve my query as detailed above and have included the code I used. I created 3 bookmarks and then inserted the clauses at each bookmark simultaneously - it inserts the clauses and now retains the cross references as well.

If anyone who reads the code sees anything that I have done wrong or could do better, I would appreciate your feedback as I am still learning VBA as I go along.

I have another problem regarding the formatting of the multi level list now but I will post on a new thread.

Thank you


Sub Lindsey()

'Insert definition of Excluded Persons
Private Sub BttnDefExcludedPersons_Click()
Static Lastclick As Boolean
If (Lastclick = False) Then
ActiveDocument.Bookmarks("ExcludedPersons1").Select
ActiveDocument.AttachedTemplate.BuildingBlockEntries("BVI-DefinitionExcludedPerson").Insert where:=Selection.Range, RichText:=True

ActiveDocument.Bookmarks("ExcludedPersons2").Select
ActiveDocument.AttachedTemplate.BuildingBlockEntries("BVI-ExcludedPerson1").Insert where:=Selection.Range, RichText:=True

ActiveDocument.Bookmarks("ExcludedPersons3").Select
ActiveDocument.AttachedTemplate.BuildingBlockEntries("BVI-ExcludedPerson2").Insert where:=Selection.Range, RichText:=True
Lastclick = True
Else

ActiveDocument.Undo
ActiveDocument.Undo
ActiveDocument.Undo
Lastclick = False
End If
ActiveDocument.UpdateStyles

End sub

gmaxey
10-28-2015, 04:46 PM
Not really sure what the purpose of your Lastclick process is, but if the user does anything else in the document between running the macro and running it again, then it isn't going to work like I think you think it will. You don't have to select the bookmarks to put BB at them.


Private Sub BttnDefExcludedPersons_Click()
Static Lastclick As Boolean
Dim oRng As Word.Range
With ActiveDocument
If (Lastclick = False) Then
Set oRng = .AttachedTemplate.BuildingBlockEntries("A").Insert(.Bookmarks("A").Range, RichText:=True)
.Bookmarks.Add "A", oRng
Set oRng = .AttachedTemplate.BuildingBlockEntries("B").Insert(.Bookmarks("B").Range, RichText:=True)
.Bookmarks.Add "B", oRng
Set oRng = .AttachedTemplate.BuildingBlockEntries("C").Insert(.Bookmarks("C").Range, RichText:=True)
.Bookmarks.Add "C", oRng
Lastclick = True
Else
Set oRng = .Bookmarks("A").Range
oRng.Text = vbNullString
.Bookmarks.Add "A", oRng
Set oRng = .Bookmarks("B").Range
oRng.Text = vbNullString
.Bookmarks.Add "B", oRng
Set oRng = .Bookmarks("C").Range
oRng.Text = vbNullString
.Bookmarks.Add "C", oRng
Lastclick = False
End If
.UpdateStyles
End With
End Sub

PicklePBFH
11-01-2015, 03:39 AM
Hi Greg
Thanks for your reply - I must admit, I'm learning a lot from your replies to everyone's queries - I will definitely be visiting your site soon. I am just so hectic at the moment. I am busy teaching myself VBA as I go along - my background is in Fiduciary/legal/training but I hated the way legal documents looked and also wanted to create efficient documents. I clearly have tons to learn.

I understand what you mean about the "Lastclick = false" - this was in the code before I added bookmarks - I used it when I included a control button on the user form to allow users to insert building blocks - if they clicked again, it would take the building block out again.
If I have another query, is there a way to tag you in my query?

Regards

Lindsey

gmaxey
11-01-2015, 07:12 AM
Lindsey,

You're welcome and glad I could help. I don't know if you can tag queries or not. I typically frequent this forum though and if not Graham Mayor, Macropod or one of the other regulars can probably help. You can also request specific consulting services (fee included) using my website.

Good luck. I have lots to learn too.