Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #16  
Old 03-21-2022, 01:01 AM
gorkac gorkac is offline Content blocks stop working when changing file path Windows 10 Content blocks stop working when changing file path Office 2019
Banned
Content blocks stop working when changing file path
 
Join Date: Jul 2021
Location: Usa
Posts: 62
gorkac is on a distinguished road
Default

I have managed to replace:
Code:
Application.Templates( _"L:\06. MODELOS DE DOCUMENTOS\01 ATESTADOS\ACCIDENTES DE CIRCULACIÓN\BAEI\Informe Tecnico\BAEI - INFORME TÉCNICO.dotm"). _
BuildingBlockEntries("INICIO Y EXPOSICION DE HECHOS").Insert Where:=Selection.Range, RichText _
:=True
by:
variable Ruta in:
Code:
Application.Templates("Ruta"). _
            BuildingBlockEntries("INICIO Y EXPOSICION DE HECHOS").Insert Where:=Selection.Range, RichText _
            :=True
Using this simple code. Now I will upload more information on how I have organized the content blocks, etc.

Code:
Sub Ruta()
Dim InicioRuta As String
Dim FinalRuta As String
Dim Ruta As String
Dim NumeroAT As String
InicioRuta = "L:\02.- ATESTADOS 2022\ATESTADOS PAMPLONA\2. ACCIDENTES CIRCULACION"
FinalRuta = "BAEI – INFORME TECNICO.docm"
NumeroAT = "\AT-1234568\"     'Here I will create a dialog for the user to indicate what AT-xxxxxxx number is going to be given.
Ruta = InicioRuta + NumeroAT + FinalRuta
MsgBox Ruta 'It returns the path, so this works for now.
End Sub
Here, MsgBox Ruta, effectively returns the path but when putting this Ruta variable where I indicated above, it does not have the effect, but it does not give an error either.

I have tried to put the same path with and without variable. With no variable, BuildingBlockEntries("INICIO Y EXPOSICION DE HECHOS") is inserted. But with the variable Ruta, no.

Let me explain, this code:


Code:
Application.Templates( _"L:\06. MODELOS DE DOCUMENTOS\01 ATESTADOS\ACCIDENTES DE CIRCULACIÓN\BAEI\Informe Tecnico\BAEI - INFORME TÉCNICO.dotm"). _
BuildingBlockEntries("INICIO Y EXPOSICION DE HECHOS").Insert Where:=Selection.Range, RichText _
:=True
is the same as:
Code:
Application.Templates("Ruta"). _
            BuildingBlockEntries("INICIO Y EXPOSICION DE HECHOS").Insert Where:=Selection.Range, RichText _
            :=True
if in the Ruta macro I put these values:
Code:
Sub Ruta()
Dim InicioRuta As String
Dim FinalRuta As String
Dim Ruta As String
Dim NumeroAT As String
InicioRuta = "L:\06. MODELOS DE DOCUMENTOS\01 ATESTADOS\ACCIDENTES DE CIRCULACIÓN\BAEI"
FinalRuta = "BAEI – INFORME TECNICO.docm"
NumeroAT = "\Informe Tecnico\"     'Here I will create a dialog for the user to indicate what AT-xxxxxxx number is going to be given.
Ruta = InicioRuta + NumeroAT + FinalRuta
MsgBox Ruta 'It returns the path, so this works for now. The message returns L:\06. MODELOS DE DOCUMENTOS\01 ATESTADOS\ACCIDENTES DE CIRCULACIÓN\BAEI\Informe Tecnico\BAEI - INFORME TÉCNICO.dotm
End Sub
This doesn't give an error, but it doesn't do anything. HELP

Code:
Application.Templates("Ruta"). _
            BuildingBlockEntries("INICIO Y EXPOSICION DE HECHOS").Insert Where:=Selection.Range, RichText _
            :=True
We are close to getting it

Last edited by gorkac; 03-21-2022 at 04:33 AM. Reason: More info
Reply With Quote
  #17  
Old 03-21-2022, 05:30 AM
gorkac gorkac is offline Content blocks stop working when changing file path Windows 10 Content blocks stop working when changing file path Office 2019
Banned
Content blocks stop working when changing file path
 
Join Date: Jul 2021
Location: Usa
Posts: 62
gorkac is on a distinguished road
Default How do i start with this?

I created a word template called BAEI - INFORME TÉCNICO.dotm
Write everything you see in the image:

1.PNG

In the insert tab, quick parts, save it in building block organizer ("organizador de bloques de construccion" on image):
5.PNG

Attachment 17668

I save it as you see in the following 3 images:

3.PNG

4.PNG



Continue...
Attached Images
File Type: jpg 2A.jpg (203.2 KB, 16 views)
Reply With Quote
  #18  
Old 03-21-2022, 05:36 AM
gorkac gorkac is offline Content blocks stop working when changing file path Windows 10 Content blocks stop working when changing file path Office 2019
Banned
Content blocks stop working when changing file path
 
Join Date: Jul 2021
Location: Usa
Posts: 62
gorkac is on a distinguished road
Default ...continue

Create a custom ribbon, the leftmost button is the one that inserts the building block "INICIO Y EXPOSICION DE HECHOS" in the word document:

Attachment 17671

Being the code the following

Attachment 17670

Well, this works if I have the template in the indicated directory: C:\Users\X041754\Desktop\GATu - INFORME TÉCNICO.dotm

But it won't work in the directory. I need to tell the user to tell me where he is going to work, and thus replace that path with another.
Attached Images
File Type: png 7A.png (174.5 KB, 16 views)

Last edited by macropod; 03-21-2022 at 05:47 PM.
Reply With Quote
  #19  
Old 03-21-2022, 06:11 AM
gorkac gorkac is offline Content blocks stop working when changing file path Windows 10 Content blocks stop working when changing file path Office 2019
Banned
Content blocks stop working when changing file path
 
Join Date: Jul 2021
Location: Usa
Posts: 62
gorkac is on a distinguished road
Default

I have detected the following error, if I remove on error resume next:
Run-time error '5941' occurred: Set element does not exist.
Code:
Sub MacroDinicio()
'On Error Resume Next

Application.Templates("Ruta").BuildingBlockEntries("INICIO Y EXPOSICION DE HECHOS").Insert Where:=Selection.Range, RichText _
            :=True
Sub end
Reply With Quote
  #20  
Old 03-21-2022, 09:00 AM
Peterson Peterson is offline Content blocks stop working when changing file path Windows 10 Content blocks stop working when changing file path Office 2019
Competent Performer
 
Join Date: Jan 2017
Posts: 143
Peterson is on a distinguished road
Default

You need to tell Word to load the building blocks in the template. You can do this by adding the following code at the beginning of your macro:
Code:
' Add this line to the beginning of your code, to access your template's building blocks:
Application.Templates.LoadBuildingBlocks
HOWEVER, I don't know if this will work because of where you are storing your templates -- you will need to test. LoadBuildingBlocks may only load building blocks from templates saved in your users' default templates location and the STARTUP folder (see below), but you are saving templates in nonstandard locations.
Code:
C:\Users\%username%\AppData\Roaming\Microsoft\Templates

C:\Users\%username%\AppData\Roaming\Microsoft\Word\STARTUP
If after you add the line of code with LoadBuildingBlocks it still doesn't work, then I strongly recommend having your users move their templates to their default location -- C:\Users\%username%\AppData\Roaming\Microsoft\Temp lates -- which is where Microsoft designed them to be stored.

Finally, modify your code to point to the template in the default location.
Reply With Quote
  #21  
Old 03-21-2022, 01:36 PM
gorkac gorkac is offline Content blocks stop working when changing file path Windows 10 Content blocks stop working when changing file path Office 2019
Banned
Content blocks stop working when changing file path
 
Join Date: Jul 2021
Location: Usa
Posts: 62
gorkac is on a distinguished road
Default Answer and questions

Quote:
Originally Posted by Peterson View Post
You need to tell Word to load the building blocks in the template.
I need you to guide me. Version Microsoft Office Standard 2016
Attached Images
File Type: png Captura6.PNG (44.2 KB, 21 views)
File Type: png Captura7.PNG (14.4 KB, 22 views)
File Type: png Captura8.PNG (11.2 KB, 20 views)
File Type: png Captura9.PNG (11.1 KB, 20 views)

Last edited by macropod; 03-21-2022 at 05:32 PM. Reason: Deleted unnecessary quote of entire post replied to
Reply With Quote
  #22  
Old 03-21-2022, 05:34 PM
macropod's Avatar
macropod macropod is offline Content blocks stop working when changing file path Windows 10 Content blocks stop working when changing file path Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,387
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

@gorkac: Please stop replying with quote to every post you reply to. All that does is add clutter. If you need to quote part of a post, quote only that part.

Also, instead of adding multiple screenshots from documents to your posts, simply attach the relevant portions of those documents - if you're posting code post the actual code, not a screenshot of it. And, if you do need to attach screenshots, scale them down to a suitable size (i.e. no more than 1200 pixels wide).
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #23  
Old 03-21-2022, 09:53 PM
Peterson Peterson is offline Content blocks stop working when changing file path Windows 10 Content blocks stop working when changing file path Office 2019
Competent Performer
 
Join Date: Jan 2017
Posts: 143
Peterson is on a distinguished road
Default

Aquí hay un código:
Code:
Sub MacroDinicio()

' NOTA: Cada usario debe poner la plantilla aquí:
'       C:\Users\%username%\AppData\Roaming\Microsoft\Templates\

    'On Error Resume Next

    
    ' No es necesario para preguntar al usuario la ruta plantilla.
    ' Estas dos líneas de código obtiene la plantilla del documento:
    Dim myTemplate As Template
    Set myTemplate = Application.Templates(ActiveDocument.AttachedTemplate.FullName)
    
    ' En el macro, tienes que incluir la siguiente línea de código antes de cada
    ' línea como las líneas de abajo ("Application.Templates...). Es necesario
    ' porque la plantilla no es en el archivo STARTUP o plantilla Normal. El código
    ' hace que todos los bloques de creacion disponibles mientras el documento está abierto.
    
    ' Cuando el usuario inserta una bloque de creación para el primer tiempo,
    ' esta línea los cargará todos ellos.
    
    ' Si no sabes cual bloque de creación se insertará primero, pon el código
    ' antes de todas las líneas que el ejemplo abajo, para "INICIO & EXPOSICION...".
    ' Pero, si estás absolutamente seguro que el usuario siempre insertará un bloque
    ' específico primero, entonces puedes ponerlo antes de solamente el código para ese
    ' bloque.
    Application.Templates.LoadBuildingBlocks
    
    ' Cambia cada línea en el macro que incluye "Application.Templates(..." a
    ' "Application.Templates(myTemplate.FullName)... Por ejemplo:
    Application.Templates(myTemplate.FullName).BuildingBlockEntries("INICIO Y EXPOSICION DE HECHOS"). _
    Insert Where:=Selection.Range, RichText:=True

End Sub
Mas notas:
  • No pongas los bloques de creación en BuildingBlocks.dotx o Normal.dotx, solamente en la plantilla que creaste.
  • No entiendo por qué sus usuarios están creando documentos habilitados para macros. Si los macros estan en la plantilla, no es necesario tenerlos en los documentos.
Reply With Quote
  #24  
Old 03-21-2022, 11:44 PM
gorkac gorkac is offline Content blocks stop working when changing file path Windows 10 Content blocks stop working when changing file path Office 2019
Banned
Content blocks stop working when changing file path
 
Join Date: Jul 2021
Location: Usa
Posts: 62
gorkac is on a distinguished road
Default

Sorry. I do not dominate the forum very well and how to post.
Reply With Quote
  #25  
Old 03-22-2022, 05:48 AM
gorkac gorkac is offline Content blocks stop working when changing file path Windows 10 Content blocks stop working when changing file path Office 2019
Banned
Content blocks stop working when changing file path
 
Join Date: Jul 2021
Location: Usa
Posts: 62
gorkac is on a distinguished road
Default

@Peterson: This code works much better for me, thank you very much.
To the question of why users use the template, it is because the template has a personalized ribbon with buttons that activate the different macros.
Reply With Quote
  #26  
Old 03-22-2022, 01:47 PM
Peterson Peterson is offline Content blocks stop working when changing file path Windows 10 Content blocks stop working when changing file path Office 2019
Competent Performer
 
Join Date: Jan 2017
Posts: 143
Peterson is on a distinguished road
Default

You're welcome.
Reply With Quote
  #27  
Old 03-23-2022, 01:19 AM
gorkac gorkac is offline Content blocks stop working when changing file path Windows 10 Content blocks stop working when changing file path Office 2019
Banned
Content blocks stop working when changing file path
 
Join Date: Jul 2021
Location: Usa
Posts: 62
gorkac is on a distinguished road
Default

One last question that arises unexpectedly and I close the issue. There are some users who open the template with the right mouse click, Open option, which causes them to open the template and not a new document based on the template. How can I program so that when these users press the Open option, it opens as a document based on the template?

mouse options
Double left click: new document "Document1.docx"
Right click, New option: same as above (I would like to program this event to have the same effect as left click.)
Right click, Open option: open template, MyTemplate.docm

I have tried some things, but it opens both template and new document

Last edited by gorkac; 03-23-2022 at 03:01 AM. Reason: add info
Reply With Quote
  #28  
Old 03-23-2022, 09:07 AM
Peterson Peterson is offline Content blocks stop working when changing file path Windows 10 Content blocks stop working when changing file path Office 2019
Competent Performer
 
Join Date: Jan 2017
Posts: 143
Peterson is on a distinguished road
Default

Right-clicking and selecting Open is how Microsoft designed Word to open template files so that you can work in them and not create a blank document based on them. There is no way to right-click on a template, select Open, and have it create a new blank document based on the template.

Your users are not using Word to create blank documents in the way Microsoft designed the program to work.

The primary method of creating a new file based on a template is via File > New. In the screen that appears, there is a Personal button. When users click Personal, Word displays all of the templates in the user's default template storage folder. The user should then click on the template with which they want to create a new document.

The other way to create a new file from a template is, as you noted, to double-click on a template that's already in a folder somewhere.

But, again, it is not possible to right-click on a template, select Open, and create a document based on that template.

To create a new file based on a template using VBA, use the following:
Code:
Sub CreateNewDocFromUserTemplate() ' 03/23/2022

    ' Create a variable for the filepath to your template:
    Dim strFilepath As String
    
    ' Use the Environ("UserProfile") function to get the first portion of the
    ' user's file path -- that is, C:\User\username -- and concatenate with the
    ' rest of the path to the template in your users' template folder:
    strFilepath = Environ("UserProfile") & _
    "\AppData\Roaming\Microsoft\Templates\Replace-This-With-Your-Template.dotm"
    
    ' Here's what the full string looks like:
    Debug.Print strFilepath
    
    ' Create new document based on template:
    Documents.Add (strFilepath)
    
End Sub
Reply With Quote
  #29  
Old 03-23-2022, 01:14 PM
gorkac gorkac is offline Content blocks stop working when changing file path Windows 10 Content blocks stop working when changing file path Office 2019
Banned
Content blocks stop working when changing file path
 
Join Date: Jul 2021
Location: Usa
Posts: 62
gorkac is on a distinguished road
Default

But when I right click on the template, the context menu shows Open, New, Print, Save as...etc. The New option opens a document based on the template, however the Open option opens the entire template.
Captura1.PNG

On the other hand, I have programmed a button on the ribbon to save in the appropriate format, based on the template, but none of the options work for me. And that the Document1 document that is opened is in docx format
Code:
Sub Docm(control As IRibbonControl)
              With Dialogs(wdDialogFileSaveAs)
            .Format = wdFormatXMLDocumentMacroEnabled
            .Show
      End With
End Sub
Reply With Quote
  #30  
Old 03-23-2022, 02:35 PM
Peterson Peterson is offline Content blocks stop working when changing file path Windows 10 Content blocks stop working when changing file path Office 2019
Competent Performer
 
Join Date: Jan 2017
Posts: 143
Peterson is on a distinguished road
Default

Quote:
Originally Posted by gorkac View Post
But when I right click on the template, the context menu shows Open, New, Print, Save as...etc. The New option opens a document based on the template, however the Open option opens the entire template.
Yes, that is how the program is designed to work, as I noted earlier. If you click Open, then Word opens the template file. If you click New, then Word creates a new document based on the template. This is simply how it works, and it cannot be changed, any more than I could, say, change the brake pedal on my car to control acceleration instead of stopping.
Quote:
Originally Posted by gorkac View Post
On the other hand, I have programmed a button on the ribbon to save in the appropriate format, based on the template, but none of the options work for me. And that the Document1 document that is opened is in docx format
Code:
Sub Docm(control As IRibbonControl)
              With Dialogs(wdDialogFileSaveAs)
            .Format = wdFormatXMLDocumentMacroEnabled
            .Show
      End With
 End Sub
I don't know exactly what you or your users are trying to do. I can't see what you're doing. Your users may be complaining that they want Microsoft's interface to work differently than the way it does -- see my brake pedal analogy.
It sounds like you need to open the actual template file, then save a copy of it as a docm file. You may need to write or find code that will open the template, then you can use your sub above to save it as a docm file, then close the template. Perhaps another member of this forum can help you with that, if needed.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mapped Content Controls in Building Blocks? Charles Kenyon Word 6 05-21-2021 05:40 PM
Stop hyperlink to a file from changing address - causes problems with pdf c.davidson Word 1 09-15-2020 11:11 PM
Content blocks stop working when changing file path New building blocks have stopped working (Word 2016) Genericname1111 Word 0 05-20-2020 04:17 PM
Content blocks stop working when changing file path Get image path from content control field - NO VBA possible thoerzer Word 5 10-10-2019 12:10 AM
Changing Absolute path to Relative in a Macro MrKim Excel Programming 13 01-26-2019 02:18 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:28 AM.


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