#1
|
|||
|
|||
Word VBA coding for tables
Help needed please with coding below, currently this works but only if i dont have any tables in the document. I want to be able to continually use macro to add different tables within same document, think its something to do with activedocument.tables (1), how do i name the table if i don't want it to be a set number?
Sub TestCGBT() Dim oTbl As Table ' TestCGBT Macro Application.Templates("P:\Templates\CG Research Template.dotm"). _ BuildingBlockEntries("CG body text table").Insert Where:=Selection.Range, _ RichText:=True ActiveDocument.Tables(1).Cell(1, 1).Range.Select Selection.SplitTable Application.Templates("P:\Templates\CG Research Template.dotm"). _ BuildingBlockEntries("Figure 1: Title").Insert Where:=Selection.Range, _ RichText:=True Selection.Delete Unit:=wdCharacter, Count:=1 Dialogs(wdDialogTableInsertTable).Show End Sub |
#2
|
||||
|
||||
Without access to your building blocks - activedocument.tables (1) is the first table in the document, whereas you need to name the table so you can process it by name e.g.
Code:
Set oTbl = Selection.Tables(1) oTbl.Cell(1, 1).Range.Select
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
Thanks for your reply, i changed the code to below, but get a run error 5941 now any ideas?
Sub TestCGBT() Dim oTbl As Table ' TestCGBT Macro Application.Templates("P:\Templates\CG Research Template.dotm"). _ BuildingBlockEntries("CG body text table").Insert Where:=Selection.Range, _ RichText:=True Set oTbl = Selection.Tables(1) oTbl.Cell(1, 1).Range.Select Selection.SplitTable Application.Templates("P:\Templates\CG Research Template.dotm"). _ BuildingBlockEntries("Figure 1: Title").Insert Where:=Selection.Range, _ RichText:=True Selection.Delete Unit:=wdCharacter, Count:=1 Dialogs(wdDialogTableInsertTable).Show End Sub |
#4
|
||||
|
||||
Something doesn't exist. You haven't told us which line is failing so it could be
The template The first building block may not be in the template The selection may not contain a table The second building block may not be in the template
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#5
|
|||
|
|||
it works until I paste the lst building block entry, but then i think the problem is the cursor is then select on the paragraph after the the building block(table) entry rather than in the table, so it cannot select. Can you ask to select 1st row of table above?
thanks for help |
#6
|
||||
|
||||
Assuming the building block-placed table is IMMEDIATELY above the selection, you could move the selection back into the table by using the .Previous method along the lines of
Code:
Dim aRng As Range, aTbl As Table, aCell As Cell Set aRng = Selection.Range.Paragraphs(1).Previous.Range Set aTbl = aRng.Tables(1) Set aCell = aTbl.Cell(1, 1) aCell.Range.Select
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
2 general question about coding | puff | Excel Programming | 1 | 12-07-2017 08:53 AM |
Problem Coding End of Page | DRD992 | Word VBA | 1 | 10-12-2017 05:15 PM |
If else statement-Need help with coding | rsrasc | Excel Programming | 4 | 10-30-2015 01:10 AM |
UserForm VBA coding | Niclasfa | Word VBA | 1 | 04-08-2015 10:23 PM |
What kind of coding? | ep2002 | Excel | 3 | 07-22-2014 07:52 AM |