#1
|
|||
|
|||
Insert StyleRef and Seq field into text
I'm inserting some StyleRef/Seq fields into the first row of a table, but not sure how to tie the code together.
Code:
.Cell(Row:=1, Column:=1).Range.Text = "Table ?.?. Name of Table" Code:
Sub Macro1() Selection.Fields.Add Range:=Selection.Range, _ Type:=wdFieldEmpty, Text:="STYLEREF ""Heading 1"" \s", PreserveFormatting:=True End Sub Code:
Sub Macro2() Selection.Fields.Add Range:=Selection.Range, _ Type:=wdFieldEmpty, Text:="SEQ Table", PreserveFormatting:=True End Sub Also, from everything I've read, you need to use the switch \r 1 to restart the numbering in the next sequence block. So, let's say I have four tables under Heading 1, so those tables would be 1.1., 1.2., 1.3., and 1.4. Now under Heading 2, the table should start with 2.1. What would be the best way to handle this? I suppose a new macro would be required, but how to recognize the first table in a new Heading section? Code:
Sub Macro3() Selection.Fields.Add Range:=Selection.Range, _ Type:=wdFieldEmpty, Text:="SEQ Table \r 1", PreserveFormatting:=True End Sub |
#2
|
||||
|
||||
Create a paragraph with content and fields set up this way
Table {StyleRef 1 \s}.{Seq Table \s 1} This can be used in every instance and doesn't need to be reset after each Heading 1. In terms of making a macro to insert this in the required place, I would say it is easiest to save that content as a building block and then just insert that BB in the required range. Something along the lines of the following... ActiveDocument.AttachedTemplate.BuildingBlockTypes (1).Categories("General").BuildingBlocks("bbName") .Insert Where:=Selection.Range, RichText:=True
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
Thanks Andrew. That seq field setup works great. I created the string...
Quote:
Code:
Sub InsertAutoTextEntry() ActiveDocument.AttachedTemplate.BuildingBlockTypes(1).Categories("General").BuildingBlocks("Table").Insert Where:=Selection.Range, RichText:=True End Sub Code:
Sub Macro1() Application.Templates( _ "C:\Users\jeffb\AppData\Roaming\Microsoft\Document Building Blocks\1033\16\Building Blocks.dotx" _ ).BuildingBlockEntries("Table").Insert Where:=Selection.Range, RichText:=True End Sub |
#4
|
|||
|
|||
Okay. My complete misunderstanding of how to use this. I saved the document as a template to use the building block because it will not be on just my machine, but still the same error.
Okay, with some further testing, I created the table, saved it as a building block, and then saved the document as a Table1.dotm. Now when I open Table1.dotm, I get a new document because you can't change the template. I run the macro InsertMyBB and the building block Table inserts, but row 1 is 10 pt when I saved the BB as 12 pt. Last edited by jeffreybrown; 12-16-2019 at 08:50 PM. |
#5
|
||||
|
||||
The macro and the building block should reside in the same template file. This reduces complexity when you need to distribute both to other users.
The code that is pointing at ActiveDocument.AttachedTemplate assumes you actually placed the building block in that template. The acceptable values for BuildingBlockTypes are shown on this page WdBuildingBlockTypes enumeration (Word) | Microsoft Docs So if you saved the building block in the attached template as an AutoText, with a category of General and a name of Table then the following line should work Code:
ActiveDocument.AttachedTemplate.BuildingBlockTypes(wdTypeAutoText).Categories("General").BuildingBlocks("Table").Insert Where:=Selection.Range, RichText:=True
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Styleref field returns error | Levi92 | Word | 6 | 10-21-2018 01:30 PM |
StyleRef field shows <> | Stoic | Word | 2 | 04-20-2017 04:42 AM |
Insert text form field help | sj80 | Word | 1 | 01-26-2016 10:50 PM |
Problem using a StyleRef field in footer | sleake | Word | 10 | 06-27-2013 07:45 AM |
StyleRef field | Ulodesk | Word | 4 | 03-21-2012 11:15 AM |