Adding multiple building blocks

  • Thread starter Thread starter OrderedChaos
  • Start date Start date
While Greg's add-in should work if installed correctly (though some of the
buttons do not actually have content and so will do nothing until you
program them to do so) his web page actually points to a solution more in
line with what you had in mind - ie batch insertion of autotext entries,
though his posted code doesn't work to that end

The following macro requires the entries to be saved in a two column table
in a Word document defined at sFname =
The first column contains the 'prompts', the second the text to be entered.
The entries are stored in the normal template - its location defined at Set
oTmp = in the autotext gallery in the category 'Scientific Names'.

The macro doesn't save the normal template so you would need to do that if
you are happy with the result.

Do note that there is no autocomplete for autotext in Word 2007 so you would
need to use memorable and unique autotext names (or you could use
autocorrect instead - there is a method to batch insert autocorrect entries
on the MVPS.ORG web site).


Sub EnterBBProgramatically()
Dim oRng As Word.Range
Dim oTmp As Template
Dim objBB As BuildingBlock
Dim ChangeDoc As Document
Dim cTable As Table
Dim aPrompt As Range, aTextPart As Range
Dim i As Long
Dim sFname As String
Set oTmp = Templates("D:\Word 2007 Templates\Normal.dotm")
sFname = "D:\My Documents\Test\AutoTextTable.doc"
Set ChangeDoc = Documents.Open(sFname)
Set cTable = ChangeDoc.Tables(1)
For i = 1 To cTable.Rows.Count
Set aPrompt = cTable.Cell(i, 1).Range
aPrompt.End = aPrompt.End - 1
Set aTextPart = cTable.Cell(i, 2).Range
aTextPart.End = aTextPart.End - 1
Set objBB = oTmp.BuildingBlockEntries _
.Add(name:=aPrompt, _
Type:=wdTypeAutoText, _
Category:="Scientific Names", _
Range:=aTextPart)
Next i
ChangeDoc.Close wdDoNotSaveChanges
End Sub

http://www.gmayor.com/installing_macro.htm


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
Thanks Graham - that is working well (for someone who dosn't know macros :)).
i appreciate the help.
 
I forgot to mention that you would need to insert the normal template
location where shown (as the macro shows my non standard location) but you
seem to have discovered that :)

Had I thought about it for a tad longer I would have allowed the macro to
discover the template location as below. I have passed the changes for Greg
to insert on his web page.

Sub EnterBBProgramatically()

Dim oRng As Word.Range

Dim oTmp As Template

Dim objBB As BuildingBlock

Dim ChangeDoc As Document

Dim cTable As Table

Dim aPrompt As Range, aTextPart As Range

Dim i As Long

Dim sFname As String

Dim sPath As String

Dim sCategory As String

sCategory = "Scientific Names"

sPath = Options.DefaultFilePath(wdUserTemplatesPath) _

& "\Normal.dotm"

Set oTmp = Templates(sPath)

sFname = "C:\AutoTextTable.doc"

Set ChangeDoc = Documents.Open(sFname)

Set cTable = ChangeDoc.Tables(1)

For i = 1 To cTable.Rows.Count

Set aPrompt = cTable.Cell(i, 1).Range

aPrompt.End = aPrompt.End - 1

Set aTextPart = cTable.Cell(i, 2).Range

aTextPart.End = aTextPart.End - 1

Set objBB = oTmp.BuildingBlockEntries.Add(name:=aPrompt, _

Type:=wdTypeAutoText, _

Category:=sCategory, _

Range:=aTextPart)

Next i

ChangeDoc.Close wdDoNotSaveChanges

End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
Back
Top