Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-07-2017, 05:42 AM
Deadly48 Deadly48 is offline Permanently renaming a word table Windows 10 Permanently renaming a word table Office 2016
Novice
Permanently renaming a word table
 
Join Date: Sep 2017
Posts: 3
Deadly48 is on a distinguished road
Default 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.
Reply With Quote
  #2  
Old 09-07-2017, 06:41 AM
gmayor's Avatar
gmayor gmayor is offline Permanently renaming a word table Windows 10 Permanently renaming a word table Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,105
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #3  
Old 09-07-2017, 03:02 PM
macropod's Avatar
macropod macropod is offline Permanently renaming a word table Windows 7 64bit Permanently renaming a word table Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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

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]
Reply With Quote
  #4  
Old 09-09-2017, 12:53 PM
Deadly48 Deadly48 is offline Permanently renaming a word table Windows 10 Permanently renaming a word table Office 2016
Novice
Permanently renaming a word table
 
Join Date: Sep 2017
Posts: 3
Deadly48 is on a distinguished road
Default

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?
Reply With Quote
  #5  
Old 09-09-2017, 07:00 PM
Guessed's Avatar
Guessed Guessed is offline Permanently renaming a word table Windows 10 Permanently renaming a word table Office 2013
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
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

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:
  1. Setting the Alt Text property in Table Properties
  2. Adding a bookmark to a table
  3. Using a different table style applied to a table
  4. Reading the text in a particular cell
Depending on which method you choose to tag the table, the method of identifying the relevant table then varies. The bookmark has to be unique so it is direct whereas the other tagging methods would require you to loop through all the tables in the document to see if any have that attribute.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #6  
Old 09-10-2017, 03:01 PM
Deadly48 Deadly48 is offline Permanently renaming a word table Windows 10 Permanently renaming a word table Office 2016
Novice
Permanently renaming a word table
 
Join Date: Sep 2017
Posts: 3
Deadly48 is on a distinguished road
Default

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
Reply With Quote
  #7  
Old 09-12-2017, 02:32 AM
macropod's Avatar
macropod macropod is offline Permanently renaming a word table Windows 7 64bit Permanently renaming a word table Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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

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]
Reply With Quote
Reply



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

Other Forums: Access Forums

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