#1
|
|||
|
|||
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? |
#2
|
|||
|
|||
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 |
#3
|
|||
|
|||
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? |
#4
|
||||
|
||||
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
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
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? |
#6
|
||||
|
||||
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] |
#7
|
|||
|
|||
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 |
#8
|
|||
|
|||
YES! It has succeeded with the solution of macropod
1 small question: Is it possible that the first table is skipped in the macro? |
#9
|
|||
|
|||
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 |
#10
|
|||
|
|||
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. |
#11
|
|||
|
|||
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 Last edited by gmaxey; 05-15-2019 at 12:09 PM. |
#12
|
||||
|
||||
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] |
#13
|
|||
|
|||
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 |
#14
|
||||
|
||||
Sorry, but that wasn't apparent to me.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#15
|
|||
|
|||
The last thing I want to do friend is fence with you over code ;-)
|
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 |
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 |
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 |