Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-26-2018, 01:35 AM
avogt avogt is offline how to add building blocks in Word by VBA also in footer Windows 7 64bit how to add building blocks in Word by VBA also in footer Office 2016
Novice
how to add building blocks in Word by VBA also in footer
 
Join Date: Apr 2018
Posts: 4
avogt is on a distinguished road
Default how to add building blocks in Word by VBA also in footer

Hello,



I want to add building blocks in my Word Document by VBA.
How can I place them correctly?

How can I place them in the footer?

Code:
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
        ActiveDocument.Range(ActiveDocument.Bookmarks("start").Range.Start + 1, ActiveDocument.Bookmarks("ende").Range.Start).Select
        Set objTemplate = Templates(1)
        Set objBB = objTemplate.BuildingBlockEntries.Item("Fusszeile_geschäftlich_DE")
        objBB.Insert Selection.Range
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
does not work.. :-(

Thanks,
Andreas
Reply With Quote
  #2  
Old 04-26-2018, 03:35 AM
gmayor's Avatar
gmayor gmayor is offline how to add building blocks in Word by VBA also in footer Windows 10 how to add building blocks in Word by VBA also in footer Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

The following function will insert a building block from a named template at a bookmarked location. That bookmark can be in a footer. See also thew example at https://www.gmayor.com/word_vba_examples_3.htm

Code:
Sub AutoTextToBM(strbmName As String, oTemplate As Template, strAutotext As String)
'strBMName is the name of the bookmark to fill
'oTemplate is the template with the autotext - probably ActiveDocument.AttachedTemplate
'strAutotext is the name of the autotext entry
Dim oRng As Range
    On Error GoTo lbl_Exit
    With ActiveDocument
        Set oRng = .Bookmarks(strbmName).Range
        Set oRng = oTemplate.AutoTextEntries(strAutotext).Insert _
                   (Where:=oRng, RichText:=True)
        .Bookmarks.Add Name:=strbmName, Range:=oRng
    End With
lbl_Exit:
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 04-26-2018, 05:44 AM
avogt avogt is offline how to add building blocks in Word by VBA also in footer Windows 7 64bit how to add building blocks in Word by VBA also in footer Office 2016
Novice
how to add building blocks in Word by VBA also in footer
 
Join Date: Apr 2018
Posts: 4
avogt is on a distinguished road
Default is the bookmark always needed?

Hi,
is there an other possibility to add the building block instead of a bookmarkt?

If not, the only way would be to delete all bookmarks after the macro has run..?

I am using the VBA to create templates depending on a menue, where the user chooses the template and some parameters.. (language, invoice/offer/.., and date..).

So I have a blank dotm and the VBA inserts all blocks, which will be needed..

is there a function to add an building block "after" something? e.g. after I inserted blockA, I will get a vbcrlf and then add blockB..

thanks,
Andreas
Reply With Quote
  #4  
Old 04-26-2018, 06:35 AM
gmayor's Avatar
gmayor gmayor is offline how to add building blocks in Word by VBA also in footer Windows 10 how to add building blocks in Word by VBA also in footer Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Frankly you can insert building blocks anywhere practical. If you want to insert a sequernce of building blocks at a range then try the following function

Code:
Sub AutoTextToRange(oRng As Range, oTemplate As Template, strAutotext As String)
'oRng is the name of the range to Place
'oTemplate is the template with the autotext - probably ActiveDocument.AttachedTemplate
'strAutotext is the name of the autotext entry
    Set oRng = oTemplate.AutoTextEntries(strAutotext).Insert _
               (Where:=oRng, RichText:=True)
    oRng.Collapse 0
    oRng.Select
lbl_Exit:
    Exit Sub
End Sub
You can call that as many times as you have building blocks to insert. Each time it puts the selection at the end of the building block. The following puts two building blocks consecutively at the cursor.

You could instead insert the first one at a range and the rest will follow.

Code:
AutoTextToRange Selection.Range, ActiveDocument.AttachedTemplate, "BBName1"
AutoTextToRange Selection.Range, ActiveDocument.AttachedTemplate, "BBName2"
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
Reply

Tags
building block, footer and header, vba

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA Word - Building Block (AutoText) - Array - Replace Text with Specific Building Blocks jc491 Word VBA 7 01-03-2016 10:34 PM
Building blocks show correctly in dropdown, but wrong building block populates in doc wordgirl123 Word 0 10-03-2013 08:30 AM
INSERT building blocks from Quickpart in word jasserin Word VBA 0 06-05-2013 12:55 PM
Word - Attach Building Blocks namedujour Word 0 04-04-2011 09:59 AM
Word 2007 Building Blocks - difficult to update... Ian Hunneybell Word 0 10-19-2009 06:27 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:11 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft