![]() |
#16
|
|||
|
|||
![]()
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 variable Ruta in: Code:
Application.Templates("Ruta"). _ BuildingBlockEntries("INICIO Y EXPOSICION DE HECHOS").Insert Where:=Selection.Range, RichText _ :=True 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 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 Code:
Application.Templates("Ruta"). _ BuildingBlockEntries("INICIO Y EXPOSICION DE HECHOS").Insert Where:=Selection.Range, RichText _ :=True 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 Code:
Application.Templates("Ruta"). _ BuildingBlockEntries("INICIO Y EXPOSICION DE HECHOS").Insert Where:=Selection.Range, RichText _ :=True Last edited by gorkac; 03-21-2022 at 04:33 AM. Reason: More info |
#17
|
|||
|
|||
![]()
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... |
#18
|
|||
|
|||
![]()
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. Last edited by macropod; 03-21-2022 at 05:47 PM. |
#19
|
|||
|
|||
![]()
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 |
#20
|
|||
|
|||
![]()
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 Code:
C:\Users\%username%\AppData\Roaming\Microsoft\Templates C:\Users\%username%\AppData\Roaming\Microsoft\Word\STARTUP Finally, modify your code to point to the template in the default location. |
#21
|
|||
|
|||
![]()
I need you to guide me. Version Microsoft Office Standard 2016
Last edited by macropod; 03-21-2022 at 05:32 PM. Reason: Deleted unnecessary quote of entire post replied to |
#22
|
||||
|
||||
![]()
@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] |
#23
|
|||
|
|||
![]()
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
|
#24
|
|||
|
|||
![]()
Sorry. I do not dominate the forum very well and how to post.
|
#25
|
|||
|
|||
![]()
@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. |
#26
|
|||
|
|||
![]()
You're welcome.
![]() |
#27
|
|||
|
|||
![]()
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 |
#28
|
|||
|
|||
![]()
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 |
#29
|
|||
|
|||
![]()
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 |
#30
|
|||
|
|||
![]() Quote:
Quote:
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. |
![]() |
Thread Tools | |
Display Modes | |
|
![]() |
||||
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 |
![]() |
Genericname1111 | Word | 0 | 05-20-2020 04:17 PM |
![]() |
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 |