Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-16-2019, 03:26 PM
jeffreybrown jeffreybrown is offline Insert StyleRef and Seq field into text Windows 10 Insert StyleRef and Seq field into text Office 2016
Expert
Insert StyleRef and Seq field into text
 
Join Date: Apr 2016
Posts: 673
jeffreybrown has a spectacular aura aboutjeffreybrown has a spectacular aura about
Default 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
I would like the above two macros to fill in in the place of the question marks in the first code block.

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
Reply With Quote
  #2  
Old 12-16-2019, 04:10 PM
Guessed's Avatar
Guessed Guessed is offline Insert StyleRef and Seq field into text Windows 10 Insert StyleRef and Seq field into text 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

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
Reply With Quote
  #3  
Old 12-16-2019, 04:32 PM
jeffreybrown jeffreybrown is offline Insert StyleRef and Seq field into text Windows 10 Insert StyleRef and Seq field into text Office 2016
Expert
Insert StyleRef and Seq field into text
 
Join Date: Apr 2016
Posts: 673
jeffreybrown has a spectacular aura aboutjeffreybrown has a spectacular aura about
Default

Thanks Andrew. That seq field setup works great. I created the string...

Quote:
Table {StyleRef 1 \s}.{Seq Table \s 1}
and then saved it as a building block call Table.

Code:
Sub InsertAutoTextEntry()
ActiveDocument.AttachedTemplate.BuildingBlockTypes(1).Categories("General").BuildingBlocks("Table").Insert Where:=Selection.Range, RichText:=True
End Sub
With this code I get a runtime error 5941. If I record the action, I get...

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
Not sure what step I am missing?
Attached Images
File Type: jpg Capture.JPG (24.6 KB, 17 views)
Reply With Quote
  #4  
Old 12-16-2019, 05:20 PM
jeffreybrown jeffreybrown is offline Insert StyleRef and Seq field into text Windows 10 Insert StyleRef and Seq field into text Office 2016
Expert
Insert StyleRef and Seq field into text
 
Join Date: Apr 2016
Posts: 673
jeffreybrown has a spectacular aura aboutjeffreybrown has a spectacular aura about
Default

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.
Attached Files
File Type: dotm Table 1.dotm (43.5 KB, 9 views)

Last edited by jeffreybrown; 12-16-2019 at 08:50 PM.
Reply With Quote
  #5  
Old 12-16-2019, 09:12 PM
Guessed's Avatar
Guessed Guessed is offline Insert StyleRef and Seq field into text Windows 10 Insert StyleRef and Seq field into text 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

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
If that was all you were putting in the macro, you could just type the word Table and then press F3 to insert the same
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert StyleRef and Seq field into text 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 StyleRef and Seq field into text 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
Insert StyleRef and Seq field into text StyleRef field Ulodesk Word 4 03-21-2012 11:15 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:20 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