Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
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: 22,467
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
  #2  
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
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 09:48 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft