#1
|
|||
|
|||
Permanently renaming a word table
I have a requirement to automatically add rows and text to various tables in a word document, using a VBA form and option boxes. The code I have can rename a table and insert, however, I need to know the table order number. This order may change as the document is edited. The problem I have is finding a way of permanently rename a table so that I can find the correct table to insert the row and text. In the document I'm adding multiple tables throughout the document I need to ensure I select the correct one.
|
#2
|
||||
|
||||
You could bookmark the tables then assign a variable to the table with the bookmark name e.g.
Code:
Dim oTable As Table Set oTable = ActiveDocument.Bookmarks("bmTableB").Range.Tables(1) oTable.Select
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
||||
|
||||
Even without bookmarking the table, your code should be able to work out which table it's being run from. For example, suppose you're using content controls with a ContentControlOnExit macro:
Code:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean) Application.ScreenUpdating = False Dim i As Long, j As Long With CCtrl 'The following code conditionally adds a new row, with content controls, to the current table. If .ShowingPlaceholderText = True Then Exit Sub 'Get the number of ContentControls in the table i = .Range.Tables(1).Range.ContentControls.Count 'Get our ContentControl's index # in the table j = ActiveDocument.Range(.Range.Tables(1).Range.Start, .Range.End).ContentControls.Count 'Check that we're using the last content control If i <> j Then Exit Sub 'Solicit user input If MsgBox("Add new row?", vbQuestion + vbYesNo) <> vbYes Then Exit Sub With .Range i = .Cells(1).RowIndex With .Tables(1).Range j = .Cells(.Cells.Count).RowIndex End With End With With .Range.Rows(1).Range If i = j Then 'Insert an empty paragraph after our table .Characters.Last.Next.InsertBefore vbCr Else 'temporarily split our table, which inserts an empty paragraph after it .Characters.Last.Next.InsertBreak wdColumnBreak End If 'replace the empty paragraph after our table with a replica of the last row .Characters.Last.Next.FormattedText = .FormattedText End With i = i + 1 'Initialize the content controls in our new row Call InitializeCCtrls(.Range.Tables(1), i) End With Application.ScreenUpdating = True End Sub Sub InitializeCCtrls(Tbl As Table, r As Long) Dim CCtrl As ContentControl 'Reset all content controls in the designated row to the initial state With Tbl.Rows(r).Range For Each CCtrl In .ContentControls With CCtrl Select Case .Type Case wdContentControlCheckBox: .Checked = False Case wdContentControlRichText, wdContentControlText, wdContentControlDate: .Range.Text = "" Case wdContentControlDropdownList .Type = wdContentControlText .Range.Text = "" .Type = wdContentControlDropdownList Case wdContentControlComboBox .Type = wdContentControlText .Range.Text = "" .Type = wdContentControlComboBox End Select End With Next End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#4
|
|||
|
|||
Hi Macropod,
Thanks for the reply however, unfortunately I'm sorry but I don't understand your code and as I'm not a great coder can't seem to make it work. I'm not sure I made myself clear with what I needed. As the document is edited I open a userform and select the text to insert and the table in which to insert into. This table is somewhere in the document. This table may be several pages away from where the userform is opened ie. the cursor is not in a cell in the desired table, also several other tables exist within the doc. I have all the code to insert rows and column and text, also temporally rename the table. This table order number changes as other tables are inserted. I can rename the table if I know the table order number however as this changes as I edit the doc, is there were a way to permanently give the table a name instead of a number based on its order. Also if this is possible would these names remain after the doc has been saved and reopened? |
#5
|
||||
|
||||
There are a few subtle ways to identify a table despite it being moved around a document. No method is flawless but you can choose to 'tag' a table by:
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#6
|
|||
|
|||
Thank you all for your postings.
I like the sound of option 4 reading text within a specific cell. I'll have a play with this as I think I can make this work. I can add a cell in my table as an identifier. Thanks again and kind regards Dave Dudson |
#7
|
||||
|
||||
Cross-posted at: https://social.msdn.microsoft.com/Fo...?forum=worddev
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
permanently highlight searched words in word 2013 | arjay | Word | 4 | 08-16-2013 09:29 AM |
Image Renaming Macro? | jammer | PowerPoint | 5 | 07-15-2013 09:21 AM |
Renaming Word Formfields: string too long error | silverspr | Word VBA | 7 | 01-22-2013 06:20 PM |
Renaming Book Marks in Microsoft Word (2007) | cheondee | Word | 0 | 03-04-2011 10:36 PM |