Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-14-2019, 05:55 AM
bheemskerk bheemskerk is offline Table named after first cell Windows 10 Table named after first cell Office 2016
Novice
Table named after first cell
 
Join Date: May 2019
Posts: 5
bheemskerk is on a distinguished road
Default Table named after first cell

Hey,

The document (CV) is generated by software and opened in Word. The document contains tables for each item (workexperience, education, etc.). If there is no data for a specific item, the table for this is not created. So the order and number of tables is different. Different things must be adjusted for each table (search and replace, layout, etc.)

Each table must have a bookmark with the name of the first cell. How do you do this?
Reply With Quote
  #2  
Old 05-14-2019, 07:38 AM
gmaxey gmaxey is offline Table named after first cell Windows 10 Table named after first cell Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

If you want to bookmark each table using the text in cell 1 then:

Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oTbl As Table
Dim oRng As Range
  For Each oTbl In ActiveDocument.Tables
    Set oRng = oTbl.Range
    ActiveDocument.Bookmarks.Add Left(oTbl.Cell(1, 1).Range.Text, Len(oTbl.Cell(1, 1).Range.Text) - 2), oRng
  Next oTbl
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 05-14-2019, 10:53 PM
bheemskerk bheemskerk is offline Table named after first cell Windows 10 Table named after first cell Office 2016
Novice
Table named after first cell
 
Join Date: May 2019
Posts: 5
bheemskerk is on a distinguished road
Default

Thanks for the answer.

Unfortunately the macro does not work. I get the following error message: The bookmark name is invalid.

What should I change?
Reply With Quote
  #4  
Old 05-15-2019, 12:39 AM
macropod's Avatar
macropod macropod is offline Table named after first cell Windows 7 64bit Table named after first cell Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

Simpler:
Code:
Sub Demo()
Dim Tbl As Table
For Each Tbl In ActiveDocument.Tables
  With Tbl
    .Range.Bookmarks.Add Split(.Cell(1, 1).Range.Text, vbCr)(0), .Range
  End With
Next Tbl
End Sub
As with Greg's code, though, if the content of the first paragraph in the first cell isn't valid for a bookmark, you'll get an error.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 05-15-2019, 04:50 AM
bheemskerk bheemskerk is offline Table named after first cell Windows 10 Table named after first cell Office 2016
Novice
Table named after first cell
 
Join Date: May 2019
Posts: 5
bheemskerk is on a distinguished road
Default

Thanks it works, but as always, there is now another problem

In some first cells there are two words with a space between them. The macro also gives an error message here. Can the macro be adjusted to keep the first word as the bookmark name?
Reply With Quote
  #6  
Old 05-15-2019, 05:44 AM
macropod's Avatar
macropod macropod is offline Table named after first cell Windows 7 64bit Table named after first cell Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

Try:
Code:
Sub Demo()
Dim Tbl As Table
For Each Tbl In ActiveDocument.Tables
  With Tbl
    .Range.Bookmarks.Add Split(Split(.Cell(1, 1).Range.Text, vbCr)(0), " ")(0), .Range
  End With
Next Tbl
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 05-15-2019, 05:47 AM
gmaxey gmaxey is offline Table named after first cell Windows 10 Table named after first cell Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

bookmarks can't have spaces in them. You can try:

Code:
Sub Demo()
Dim Tbl As Table
For Each Tbl In ActiveDocument.Tables
  With Tbl
     .Range.Bookmarks.Add Replace(Split(.Cell(1, 1).Range.Text, vbCr)(0), " ", "_"), .Range
  End With
Next Tbl
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #8  
Old 05-15-2019, 06:11 AM
bheemskerk bheemskerk is offline Table named after first cell Windows 10 Table named after first cell Office 2016
Novice
Table named after first cell
 
Join Date: May 2019
Posts: 5
bheemskerk is on a distinguished road
Default

YES! It has succeeded with the solution of macropod

1 small question:
Is it possible that the first table is skipped in the macro?
Reply With Quote
  #9  
Old 05-15-2019, 06:19 AM
gmaxey gmaxey is offline Table named after first cell Windows 10 Table named after first cell Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Well, the solution provided by Macropod does not bookmark the table with the content of the first cell. You are wasting a lot of other people's time by not being clear on what you want.
Code:
Sub Demo()
Dim lngIndex As Long
Dim oTbl As Table
  For lngIndex = 2 To ActiveDocument.Tables.Count
    Set oTbl = ActiveDocument.Tables(lngIndex)
    With oTbl
       .Range.Bookmarks.Add Replace(Split(.Cell(1, 1).Range.Text, vbCr)(0), " ", "_"), .Range
    End With
  Next lngIndex
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #10  
Old 05-15-2019, 06:39 AM
bheemskerk bheemskerk is offline Table named after first cell Windows 10 Table named after first cell Office 2016
Novice
Table named after first cell
 
Join Date: May 2019
Posts: 5
bheemskerk is on a distinguished road
Default

Sorry but the solution of Macroped works with the first cell in the table in my document.
I think I have clearly indicated what I want and this also happens with this macro.

Your solution for skipping the first table is also working.

I am very happy with this solution and it was not the intention to waste someone's time.
Reply With Quote
  #11  
Old 05-15-2019, 06:49 AM
gmaxey gmaxey is offline Table named after first cell Windows 10 Table named after first cell Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

No harm, no foul. My point is that you didn't indicate from the start that your cell content could contain text that was invalid for a bookmark name, that it could be two or more words separated by spaces or that your really only wanted the first word.


If you really only want the first word then both mine and Paul's code could have been different:

Code:
Sub Demo()
Dim lngIndex As Long
Dim oTbl As Table
  For lngIndex = 2 To ActiveDocument.Tables.Count
    Set oTbl = ActiveDocument.Tables(lngIndex)
    With oTbl
      .Range.Bookmarks.Add Trim(.Cell(1, 1).Range.Words(1)), .Range
    End With
  Next lngIndex
End Sub
Now still, if the first "word" happens to be a number or if there is no first word (empty cell), you will still get and error.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/

Last edited by gmaxey; 05-15-2019 at 12:09 PM.
Reply With Quote
  #12  
Old 05-15-2019, 04:07 PM
macropod's Avatar
macropod macropod is offline Table named after first cell Windows 7 64bit Table named after first cell Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

Quote:
Originally Posted by gmaxey View Post
Well, the solution provided by Macropod does not bookmark the table with the content of the first cell.
Given that bookmarks cannot contain spaces (which the OP subsequently clarified) or paragraph breaks (which your code doesn't handle at all), my code goes as close as is possible. I do agree, though, that the specifications were lacking.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #13  
Old 05-15-2019, 07:38 PM
gmaxey gmaxey is offline Table named after first cell Windows 10 Table named after first cell Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Come on Paul, I wasn't trying to be critical of your code, I was just trying to point out that had either of us known what the actual requirement was, one or both of us could have provided a workable solution the first time.


I can't imaging why the first word of the cell would be a paragraph break or need to be handled:

.Range.Bookmarks.Add Trim(.Cell(1, 1).Range.Words(1)), .Range
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #14  
Old 05-15-2019, 07:40 PM
macropod's Avatar
macropod macropod is offline Table named after first cell Windows 7 64bit Table named after first cell Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

Quote:
Originally Posted by gmaxey View Post
Come on Paul, I wasn't trying to be critical of your code
Sorry, but that wasn't apparent to me.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #15  
Old 05-15-2019, 07:43 PM
gmaxey gmaxey is offline Table named after first cell Windows 10 Table named after first cell Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

The last thing I want to do friend is fence with you over code ;-)
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply

Tags
bookmark, table

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Use of named ranges based on cell content Intruder Excel 12 02-25-2019 09:42 AM
save selected worksheets, named by cell value rossmortimore Excel 1 12-27-2018 01:56 PM
Table named after first cell Code to find a named (bookmarked?) table, replicate a row or table, and delete a specified table. kevinbradley57 Word VBA 9 09-21-2017 04:58 PM
Table named after first cell how do i use a named cell for each worksheet cwkotch Excel 3 11-03-2015 09:04 AM
Can't use a zero in a named cell TechEd Excel 2 10-16-2012 10:06 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:50 AM.


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