Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-31-2021, 09:00 PM
Bikram Bikram is offline Unable to access buildingblocks through vba Windows 10 Unable to access buildingblocks through vba Office 2007
Advanced Beginner
Unable to access buildingblocks through vba
 
Join Date: Jul 2021
Location: Nepal
Posts: 90
Bikram is on a distinguished road
Default Unable to access buildingblocks through vba

I created some building blocks and recorded macro to insert them. But when I ran the same macro recorded by macro recorder, I encountered error 5941. I even tried Application.Template.Loadbuildingblocks to load building blocks but reencountered the same error. I am also not being able to access building blocks.dotx ("C:\Program Files (x86)\Microsoft Office\Office12\Document Parts\1033\Building Blocks.dotx").



While I wrote "Set BBtemplate = Application.Templates("C:\Program Files (x86)\Microsoft Office\Office12\Document Parts\1033\Building Blocks.dotx")" vba displays the same error 5941 saying the collection does not exist. How can I solve this problem??
Reply With Quote
  #2  
Old 08-31-2021, 10:00 PM
Guessed's Avatar
Guessed Guessed is offline Unable to access buildingblocks through vba Windows 10 Unable to access buildingblocks through vba Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,969
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

It sounds like the template is not currently loaded as an addin. Can you confirm that it is ticked in your addin list?
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 08-31-2021, 10:42 PM
Bikram Bikram is offline Unable to access buildingblocks through vba Windows 10 Unable to access buildingblocks through vba Office 2007
Advanced Beginner
Unable to access buildingblocks through vba
 
Join Date: Jul 2021
Location: Nepal
Posts: 90
Bikram is on a distinguished road
Default

I tried it too but doesn't work. It still shows error 5941.
Reply With Quote
  #4  
Old 08-31-2021, 11:33 PM
gmayor's Avatar
gmayor gmayor is offline Unable to access buildingblocks through vba Windows 10 Unable to access buildingblocks through vba Office 2019
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

From my web site, the following code can be used to insert a named building block.
Code:
Option Explicit
'Graham Mayor - https://www.gmayor.com - Last updated - 01 Sep 2021 
Sub InsertMyBuildingBlock()
Dim oTemplate As Template
Dim oAddin As AddIn
Dim bFound As Boolean
Dim i As Long
    'Define the required building block entry
Const strBuildingBlockName As String = "Building Block Name"

    'Set the found flag default to False
    bFound = False
    'Ignore the attached template for now if the
    'document is based on the normal template
    If ActiveDocument.AttachedTemplate <> NormalTemplate Then
        Set oTemplate = ActiveDocument.AttachedTemplate
        'Check each building block entry in the attached template
        For i = 1 To oTemplate.BuildingBlockEntries.Count
            'Look for the building block name
            'and if found, insert it.
            If oTemplate.BuildingBlockEntries(i).Name = strBuildingBlockName Then
                oTemplate.BuildingBlockEntries(strBuildingBlockName).Insert _
                        Where:=Selection.Range
                'Set the found flag to true
                bFound = True
                'Clean up and stop looking
                Set oTemplate = Nothing
                GoTo lbl_Exit
            End If
        Next i
    End If
    'The entry has not been found
    If bFound = False Then
        For Each oAddin In AddIns
            'Check currently loaded add-ins
            If oAddin.Installed = False Then Exit For
            Set oTemplate = Templates(oAddin.path & _
                                      Application.PathSeparator & oAddin.Name)
            'Check each building block entry in the each add in
            For i = 1 To oTemplate.BuildingBlockEntries.Count
                If oTemplate.BuildingBlockEntries(i).Name = strBuildingBlockName Then
                    'Look for the building block name
                    'and if found, insert it.
                    oTemplate.BuildingBlockEntries(strBuildingBlockName).Insert _
                            Where:=Selection.Range
                    'Set the found flag to true
                    bFound = True
                    'Clean up and stop looking
                    Set oTemplate = Nothing
                    GoTo lbl_Exit
                End If
            Next i
        Next oAddin
    End If
    'The entry has not been found. Check the normal template
    If bFound = False Then
        For i = 1 To NormalTemplate.BuildingBlockEntries.Count
            If NormalTemplate.BuildingBlockEntries(i).Name = strBuildingBlockName Then
                NormalTemplate.BuildingBlockEntries(strBuildingBlockName).Insert _
                        Where:=Selection.Range
                'set the found flag to true
                bFound = True
                GoTo lbl_Exit
            End If
        Next i
    End If
    'If the entry has still not been found
    'finally check the Building Blocks.dotx template
    If bFound = False Then
        Templates.LoadBuildingBlocks
        For Each oTemplate In Templates
            If oTemplate.Name = "Building Blocks.dotx" Then Exit For
        Next
        For i = 1 To Templates(oTemplate.FullName).BuildingBlockEntries.Count
            If Templates(oTemplate.FullName).BuildingBlockEntries(i).Name = strBuildingBlockName Then
                Templates(oTemplate.FullName).BuildingBlockEntries(strBuildingBlockName).Insert _
                        Where:=Selection.Range
                'set the found flag to true
                bFound = True
                'Clean up and stop looking
                Set oTemplate = Nothing
                GoTo lbl_Exit
            End If
        Next i
    End If
    'All sources have been checked and the entry is still not found
    If bFound = False Then
        'so tell the user.
        MsgBox "Entry not found", _
               vbInformation, _
               "Building Block " _
               & Chr(145) & strBuildingBlockName & Chr(146)
    End If
lbl_Exit:
    Set oTemplate = Nothing
    Set oAddin = Nothing
    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
  #5  
Old 09-04-2021, 02:16 AM
Bikram Bikram is offline Unable to access buildingblocks through vba Windows 10 Unable to access buildingblocks through vba Office 2007
Advanced Beginner
Unable to access buildingblocks through vba
 
Join Date: Jul 2021
Location: Nepal
Posts: 90
Bikram is on a distinguished road
Default

Thanks a lot for the reply, Sir. The code you provided worked awesomely and inserted the buildingblock I wanted to insert but
ActiveDocument.AttachedTemplate.BuildingBlockEntri es("heading").Insert Where:= _
Selection.Range, RichText:=True
still shows the error 5941.

For i = 1 To Templates(oTemplate.FullName).BuildingBlockEntries .Count
If Templates(oTemplate.FullName).BuildingBlockEntries (i).Name = strBuildingBlockName Then
Templates(oTemplate.FullName).BuildingBlockEntries (strBuildingBlockName).Insert _
Where:=Selection.Range
'set the found flag to true
bFound = True
'Clean up and stop looking
Set oTemplate = Nothing
GoTo lbl_Exit
End If
Next i
This part inserted the buildings blocks I needed. But when I tried
Dim oTemplate As Template
Set oTemplate = ActiveDocument.AttachedTemplate
Templates(oTemplate.FullName).BuildingBlockEntries ("heading").Insert _
Where:=Selection.Range
It again shows error 5941. Sir, can you please suggest what may have caused this problem and how can i solve this?
Reply With Quote
  #6  
Old 09-04-2021, 06:03 AM
gmayor's Avatar
gmayor gmayor is offline Unable to access buildingblocks through vba Windows 10 Unable to access buildingblocks through vba Office 2019
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 error almost certainly suggests that the 'heading' entry is not present in the document's attached template. The code I posted looks through all the available locations for the entry. The answer is to use the code I posted.
__________________
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
  #7  
Old 09-04-2021, 09:47 PM
Bikram Bikram is offline Unable to access buildingblocks through vba Windows 10 Unable to access buildingblocks through vba Office 2007
Advanced Beginner
Unable to access buildingblocks through vba
 
Join Date: Jul 2021
Location: Nepal
Posts: 90
Bikram is on a distinguished road
Default

Thank you, for the reply Sir.
Reply With Quote
  #8  
Old 06-07-2022, 08:00 AM
tonse tonse is offline Unable to access buildingblocks through vba Windows 7 64bit Unable to access buildingblocks through vba Office 2010
Novice
 
Join Date: Aug 2020
Posts: 2
tonse is on a distinguished road
Default

Hi, i want to use building blocks from another dotx file, because
a.normal.dot cannot be used because to many different users.
b. buildingblocks.dotx is not accessible for me.
C. in the document itself is also not an option as the desired buildingblocks contents is more or less classified. The original document has to be send to customer, but nog all the other buildingblocks. Which Buildingblocks are sent, depends on the customer.

Is this possible with your code??
Reply With Quote
  #9  
Old 06-07-2022, 09:12 AM
Italophile Italophile is online now Unable to access buildingblocks through vba Windows 11 Unable to access buildingblocks through vba Office 2021
Expert
 
Join Date: Mar 2022
Posts: 334
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

Building blocks cannot be included in documents, so option C doesn't apply anyway.

Building blocks can only be stored in a template (dotx/dotm), so you either store them in the document template (which won't get sent with the document) or in another global template (one that is located in the Startup folder).
Reply With Quote
Reply

Tags
building bl, vba



Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to access buildingblocks through vba Unable to access picture layout settings. wadori Word 3 12-02-2020 01:20 PM
Alias email - unable to access folders prgwd Outlook 0 01-17-2020 09:43 AM
Using FieldCodes, AutoText, BuildingBlocks ptmuldoon Word 5 01-02-2015 01:33 PM
Unable to access attachments on older emails charon Outlook 0 10-31-2013 05:52 AM
Unable to access Online Templates and Clipart via Office programs LostAngles Office 1 02-10-2012 07:20 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:34 AM.


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