PDA

View Full Version : Solved: Number of TABLES within a word document.



espencer
09-02-2005, 09:29 AM
Hello,
I would like to know if anyone knows , with an macro, how to find out how many TABLES are within a document?
I've search and was not able to find this information. Please help and thanks in advance.

Killian
09-02-2005, 09:50 AM
How aboutMsgBox "There are " & ActiveDocument.Tables.Count & " tables in this document"

MOS MASTER
09-02-2005, 10:18 AM
Hi, :hi:

Killian has answered your question exactly.

It looks like you want to loop through all the tables in the document. (Assumption)

So here's to methods of doing that in one macro:
Sub LoopTables()
Dim oTable As Word.Table
Dim iTable As Integer
For Each oTable In ActiveDocument.Tables()
With oTable
'Do stuff to the table
End With
Next

'or
For iTable = 1 To ActiveDocument.Tables.Count
Set oTable = ActiveDocument.Tables(iTable)

With oTable
'Do stuff to the table
End With
Next

End Sub


HTH, :whistle:

MWE
09-02-2005, 11:03 AM
How aboutMsgBox "There are " & ActiveDocument.Tables.Count & " tables in this document"
Just a FYI on this ...

VBA creators did a pretty good job defining structures and similar methods/properties for those structures. Finding "how many" is normally something like:

ActiveThing.ObjectThing.Count

MOS MASTER
09-02-2005, 11:07 AM
True the coders of the VBA Object Models did in most cases a very nice and logic job!

Good call Mark! :yes

espencer
09-02-2005, 11:49 AM
Thanks for the help and those commands/ macros work. I need to find the number of tables of the document that's being inserted to a new document. I'm sorry that I was not clear on that. I want to make a change to the style of the first table of the inserted file to the new document. Sorry again. I will try some things and if you can help, THANKS.

MOS MASTER
09-02-2005, 11:50 AM
You're Welcome. :yes

Ok in that case you need Killian's answer. :thumb

espencer
09-06-2005, 03:30 AM
When I try this it give me the total of all the table for the active document. I need just the table count of the inserted document to use to GoToPrevious, Count command to set the style of the first table of the inserted document.

Sample code below;
'Setup for footnote 2
With Selection
.EndKey unit:=wdStory
.InsertBreak Type:=wdSectionBreakNextPage
.PageSetup.Orientation = wdOrientPortrait
.PageSetup.TopMargin = InchesToPoints(0.56)
.PageSetup.BottomMargin = InchesToPoints(1)
.PageSetup.LeftMargin = InchesToPoints(0.76)
.PageSetup.RightMargin = InchesToPoints(0.76)
' .EndKey unit:=wdStory
' .InsertBreak Type:=wdSectionBreakNextPage
.InsertFile FileName:=ActiveDocument.Path & "\" & "note02.doc"
' go back to the Notes table
.GoTo What:=wdGoToTable, _
Which:=wdGoToPrevious, Count:=2, Name:=""
' select first cell and reset style
' and MANUAL formatting
.Tables(1).Cell(1, 1).Select
.Style = ActiveDocument.Styles("Body Text 2")
With .Font
.Bold = True
.Size = 14
.NameAscii = "Arial"
.Underline = 0
End With
' .EndKey unit:=wdStory
End With
'------------------------------------------------------------------------------
'Setup for footnote 3
With Selection
.EndKey unit:=wdStory
.InsertBreak Type:=wdSectionBreakNextPage
.PageSetup.Orientation = wdOrientPortrait
.PageSetup.TopMargin = InchesToPoints(0.44)
.PageSetup.BottomMargin = InchesToPoints(1)
.PageSetup.LeftMargin = InchesToPoints(1.25)
.PageSetup.RightMargin = InchesToPoints(1.25)
' .EndKey unit:=wdStory
' .InsertBreak Type:=wdSectionBreakNextPage
.InsertFile FileName:=ActiveDocument.Path & "\" & "note03.doc", range:="" _
, ConfirmConversions:=False, Link:=True, Attachment:=False
' go back to the Notes table
.GoTo What:=wdGoToTable, _
Which:=wdGoToPrevious, Count:=5, Name:=""
' select first cell and reset style
' and MANUAL formatting
.Tables(1).Cell(1, 1).Select
.Style = ActiveDocument.Styles("Body Text 2")
With .Font
.Bold = True
.Size = 14
.NameAscii = "Arial"
.Underline = 0
End With
' .EndKey unit:=wdStory
End With
'------------------------------------------------------------------------------
'Setup for footnote 4
With Selection
.EndKey unit:=wdStory
.InsertBreak Type:=wdSectionBreakNextPage
.PageSetup.Orientation = wdOrientLandscape
.PageSetup.TopMargin = InchesToPoints(0.38)
.PageSetup.BottomMargin = InchesToPoints(1.25)
.PageSetup.LeftMargin = InchesToPoints(1)
.PageSetup.RightMargin = InchesToPoints(1)
' .EndKey unit:=wdStory
' .InsertBreak Type:=wdSectionBreakNextPage
.InsertFile FileName:=ActiveDocument.Path & "\" & "note04.doc", range:="" _
, ConfirmConversions:=False, Link:=True, Attachment:=False
' go back to the Notes table
.GoTo What:=wdGoToTable, _
Which:=wdGoToPrevious, Count:=3, Name:=""
' select first cell and reset style
' and MANUAL formatting
.Tables(1).Cell(1, 1).Select
.Style = ActiveDocument.Styles("Body Text 2")
With .Font
.Bold = True
.Size = 14
.NameAscii = "Arial"
.Underline = 0
End With
' .EndKey unit:=wdStory
End With


Each inserted document will have a different table count and I would like to be able to find the number of tables in each inserted document and maybe set it as a varible and place in the count:= command. is this possible? Is the a way to find the table count before inserting the document?

Let me try to clear this. I am try to get the number of tables in an inserted document, that can vary, and modify the style of the first table of the inserted document to the active document. In the example above Note 2 has 3 tables and the wdGoToPrevious with count of 2 makes the change to the first table...etc. Some user may modify the Note 2 in their directory and add another table to the document and now this person has 4 table in their document and the style change will be made to table 2 in their document will the change has to be in Table 1 of all documents. I hope this is clearer.

fumei
09-06-2005, 07:42 AM
1. Once you insert the other document, then well...of course when ask for the Table.Count it gives you the whole document. You could get the count of the tables of the inserted document by making a Range from the inserted point to the new end of the document, then getting a table count of that.
Dim oRange As Range
' put this at the insertion point
ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="Here"
Selection.InsertFile FileName:=ActiveDocument.Path & _
"\" & "note04.doc", range:="" _
, ConfirmConversions:=False, Link:=True, Attachment:=False
Set oRange = ActiveDocument.Range(Start:=ActiveDocument.Bookmarks("Here").Start, _
End:=ActiveDocument.Bookmarks("\endofdoc").End)

oRange.Tables.Count will give the count of the tables in that Range - the inserted document.

Do not forget to make oRange = Nothing at the end of each loop, as you need to make a new one for each iteration.

Do not forget to delete the "Here" bookmark at the end of each loop, as you need to make a new one for each iteration.

2. You are NOT doing this correctly!!!
' select first cell and reset style
' and MANUAL formatting
.Tables(1).Cell(1, 1).Select
.Style = ActiveDocument.Styles("Body Text 2")
With .Font
.Bold = True
.Size = 14
.NameAscii = "Arial"
.Underline = 0
End With
is BAD code, BAD use of Word.

What on earth is the point of making text a style, then manully formatting it?? It is a total waste of time.

3. You are asking for table count, and you mention that you want to modify the style in the first table in the inserted document. But I see nothing that use a Table style. You are not using a table style, you are not modifying the style of the table. You are modifying the TEXT of cells to a style......then changing it! Why bother assigning the style, if you are then manually formatting?

To repeat, NO you can not get the table count of an inserted document - once it is inserted it no longer is a document, it is part of the document it is in. YES, you can get the table count of the range of inserted document, using the code above.

espencer
09-06-2005, 08:51 AM
I've modified the beginning of the code and I'm getting a compile error. The code is below. What am I doing wrong on this?

----------------------
Dim oRange As Range
' put this at the insertion point
ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="Here"
With Selection
.EndKey unit:=wdStory
.InsertBreak Type:=wdSectionBreakNextPage
.PageSetup.Orientation = wdOrientPortrait
.PageSetup.TopMargin = InchesToPoints(0.56)
.PageSetup.BottomMargin = InchesToPoints(1)
.PageSetup.LeftMargin = InchesToPoints(0.76)
.PageSetup.RightMargin = InchesToPoints(0.76)
' .EndKey unit:=wdStory
' .InsertBreak Type:=wdSectionBreakNextPage
.InsertFile FileName:=ActiveDocument.Path & "\" & "note02.doc"
' go back to the Notes table
End With
Set oRange = ActiveDocument.Range(Start:=ActiveDocument.Bookmarks("Here").Start, _
End:=ActiveDocument.Bookmarks("\endofdoc").End)
MsgBox "There are " & oRange.Tables.Count & " tables in this document"

With Selection
.GoTo What:=wdGoToTable, _
Which:=wdGoToPrevious, Count:=oRange.Tables.Count - 1, Name:=""
' Which:=wdGoToFirst
' Which:=wdGoToFirst, Count:=1, Name:=""
' Which:=wdGoToPrevious, Count:=2, Name:=""
' select first cell and reset style
' and MANUAL formatting
.Tables(1).Cell(1, 1).Select
.Style = ActiveDocument.Styles("Body Text 2")
With .Font
.Bold = True
.Size = 14
.NameAscii = "Arial"
.Underline = 0
End With
' .EndKey unit:=wdStory
End With

oRange = Nothing
--------------------------------------------------


The error is;
Compile error.
Invalid use of property

In the editor Bookmarks is highlited on the ActiveDocument. Did I misplace the code?

Thanks for all of your help.

fumei
09-06-2005, 08:58 AM
oRange = Nothing

should be:

Set oRange = Nothing

espencer
09-06-2005, 09:25 AM
Made that change and getting the same error.

MOS MASTER
09-06-2005, 10:25 AM
Made that change and getting the same error.

Change:
ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="Here"
To:

ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:="Here"


It should be on one line! :whistle:

fumei
09-06-2005, 01:16 PM
CRAP!

MOS MASTER
09-06-2005, 01:18 PM
CRAP!

Doh...please full sentences buddy! :devil: :rofl:

espencer
09-12-2005, 06:44 AM
I've changed the code and this is not giving me the number of table of the inserted document but the running total of tables in the active document. I need the total of tables in each separate document

fumei
09-12-2005, 07:08 AM
You are not reading the posts very well. Go back and read carefully. The complete answer is posted, but I will see if I can make this clear for you.

1. You have a document. It has 4 tables.

2. You insert a document that has 3 tables.

WHAT DO YOU HAVE??????

ONE document with 7 tables. As you have noted. YES, it does - [-b]as I posted[/b] give the running number of tables in the active document. What else could it give you????? You are asking:

ActiveDocument.Tables.Count......duh, the active document has 7 tables.

You ask for the number of tables in each separate document. I suggest you actually think about this a wee bit. If you insert a document into another.....is that inserted document "separate"?....well, gee gosh....it is not. You inserted it. You made it part of the active document. It now IS part of the document.

How is Word to know that the inserted chunk is to be considered "separate". You did not make it separate.

I posted exactly how you can do this. The only way you can do this. Well, not the only way. What I posted was a way to get a running count. If you want to get and keep a permanent count - in other words, actually have Word be able to get a "separate" table count for each inserted document, then make each inserted document a ranged bookmark. Then using the code that I posted - uh, corrected for my dumb typos - you can get the table count for each BOOKMARK. Each bookmark would be the range of the inserted document.

How can it be more clear? Once you insert a file, it is not "separate".

Using my code, while still having the oRange object, use ActiveDocument.Bookmarks.Add to make a bookmark of the inserting document. Use whatever naming convention you want - "InsertedFile01", or even use the filename. Whatever. Then you can....anytime, get the table count of that bookmark. But you can not get a table count of the inserted document....because it is INSERTED....it is not a separate document anymore.

Killian
09-12-2005, 07:32 AM
So I suppose the thing to do would be to set a another range object that defines the inserted content. When you InsretFile, the selection will be at the end of that - which is handy since we already know where it started. Does this help?Dim oRange As Range
Dim newrange As Range
' put this at the insertion point
ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:="Here"
'insert the file
Selection.InsertFile FileName:="C:\Documents and Settings\Killian\Desktop\test.doc"
'set the range for the inserted content
Set newrange = ActiveDocument.Range(Start:=ActiveDocument.Bookmarks("Here").Start, _
End:=Selection.End)
'this is the previous range - from inserted to the end
Set oRange = ActiveDocument.Range(Start:=ActiveDocument.Bookmarks("Here").Start, _
End:=ActiveDocument.Bookmarks("\endofdoc").End)

'here's some messages
MsgBox "There are " & ActiveDocument.Range.Tables.Count & " tables in this document"
MsgBox "There are " & oRange.Tables.Count & " tables from the insertion point to the end of the document"
MsgBox "There are " & newrange.Tables.Count & " tables in the inserted part of the document"

fumei
09-14-2005, 09:00 AM
There is no need for the second Range, at all. You in fact pointed out why - when the file is inserted the selection IS at the end of the document. So......

oRange is using Selection.End
newRange is using Bookmarks("\endofdoc").End

except.....they are the same thing! So why bother?? The second range is not needed.

espencer
09-14-2005, 10:07 AM
Below is a few lines from my code. I've put some displays in and the numbers seem to be different on the actual table counts for each inserted documents. My concern is that I'm trying to change the style of the first table of each inserted document. I think my coding in the area of;

With Selection
.GoTo What:=wdGoToTable, _
Which:=wdGoToPrevious, Count:=newrange.Tables.Count - 1, Name:=" "

is wrong or I'm not getting the right variable for the count. Can you advise?



Sub Concatenated_Footnotes()
'
' Concatenated_Footnotes Macro
' Macro created 8/31/2005
'---------------------------------------------------------------------------
'Setup for footnote 2
Dim oRange As Range
Dim newrange As Range
' put this at the insertion point
'ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:="Here"
MsgBox "There are " & ActiveDocument.Range.Tables.Count & " tables in this document - Note1"
With Selection
.EndKey unit:=wdStory
.InsertBreak Type:=wdSectionBreakNextPage
.PageSetup.Orientation = wdOrientPortrait
.PageSetup.TopMargin = InchesToPoints(0.56)
.PageSetup.BottomMargin = InchesToPoints(1)
.PageSetup.LeftMargin = InchesToPoints(0.76)
.PageSetup.RightMargin = InchesToPoints(0.76)
.InsertFile FileName:=ActiveDocument.Path & "\" & "note02.doc", Range:="" _
, ConfirmConversions:=False, Link:=True, Attachment:=False
End With

' go back to the Notes table
'set the range for the inserted content
Set newrange = Nothing
Set newrange = ActiveDocument.Range(Start:=ActiveDocument.Bookmarks("Here").Start, _
End:=Selection.End)

MsgBox "There are " & newrange.Tables.Count & " NEWRANGE - note02"

'this is the previous range - from inserted to the end
Set oRange = Nothing
Set oRange = ActiveDocument.Range(Start:=ActiveDocument.Bookmarks("Here").Start, _
End:=ActiveDocument.Bookmarks("\endofdoc").End)
MsgBox "There are " & oRange.Tables.Count & " oRANGE - note02"

With Selection
.GoTo What:=wdGoToTable, _
Which:=wdGoToPrevious, Count:=newrange.Tables.Count - 1, Name:=" "
' select first cell and reset style and MANUAL formatting
.Tables(1).Cell(1, 1).Select
.Style = ActiveDocument.Styles("Body Text 2")

With .Font
.Bold = True
.Size = 14
.NameAscii = "Arial"
.Underline = 0
End With
' .EndKey unit:=wdStory
End With
'------------------------------------------------------------------------------
'Setup for footnote 3
ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:="Here"
MsgBox "There are " & ActiveDocument.Range.Tables.Count & " tables in this document - Note 2"
With Selection
.EndKey unit:=wdStory
.InsertBreak Type:=wdSectionBreakNextPage
.PageSetup.Orientation = wdOrientPortrait
.PageSetup.TopMargin = InchesToPoints(0.44)
.PageSetup.BottomMargin = InchesToPoints(1)
.PageSetup.LeftMargin = InchesToPoints(1.25)
.PageSetup.RightMargin = InchesToPoints(1.25)
.InsertFile FileName:=ActiveDocument.Path & "\" & "note03.doc", Range:="" _
, ConfirmConversions:=False, Link:=True, Attachment:=False
End With

' go back to the Notes table
'set the range for the inserted content
Set newrange = Nothing
Set newrange = ActiveDocument.Range(Start:=ActiveDocument.Bookmarks("Here").Start, _
End:=Selection.End)

MsgBox "There are " & newrange.Tables.Count & " NEWRANGE - note03"

'this is the previous range - from inserted to the end
Set oRange = Nothing
Set oRange = ActiveDocument.Range(Start:=ActiveDocument.Bookmarks("Here").Start, _
End:=ActiveDocument.Bookmarks("\endofdoc").End)
MsgBox "There are " & oRange.Tables.Count & " oRANGE - note03"

With Selection
.GoTo What:=wdGoToTable, _
Which:=wdGoToPrevious, Count:=newrange.Tables.Count - 1, Name:=" "

' select first cell and reset style and MANUAL formatting
.Tables(1).Cell(1, 1).Select
.Style = ActiveDocument.Styles("Body Text 2")

With .Font
.Bold = True
.Size = 14
.NameAscii = "Arial"
.Underline = 0
End With
' .EndKey unit:=wdStory
End With
'------------------------------------------------------------------------------
'Setup for footnote 4
ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:="Here"
MsgBox "There are " & ActiveDocument.Range.Tables.Count & " tables in this document - Note 3"
With Selection
.EndKey unit:=wdStory
.InsertBreak Type:=wdSectionBreakNextPage
.PageSetup.Orientation = wdOrientLandscape
.PageSetup.TopMargin = InchesToPoints(0.38)
.PageSetup.BottomMargin = InchesToPoints(1.25)
.PageSetup.LeftMargin = InchesToPoints(1)
.PageSetup.RightMargin = InchesToPoints(1)
.InsertFile FileName:=ActiveDocument.Path & "\" & "note04.doc", Range:="" _
, ConfirmConversions:=False, Link:=True, Attachment:=False
End With
' go back to the Notes table
'set the range for the inserted content
Set newrange = Nothing
Set newrange = ActiveDocument.Range(Start:=ActiveDocument.Bookmarks("Here").Start, _
End:=Selection.End)

MsgBox "There are " & newrange.Tables.Count & " NEWRANGE- note04"

'this is the previous range - from inserted to the end
Set oRange = Nothing
Set oRange = ActiveDocument.Range(Start:=ActiveDocument.Bookmarks("Here").Start, _
End:=ActiveDocument.Bookmarks("\endofdoc").End)
MsgBox "There are " & oRange.Tables.Count & " oRANGE- note04"

With Selection
.GoTo What:=wdGoToTable, _
Which:=wdGoToPrevious, Count:=newrange.Tables.Count - 1, Name:=" "

' select first cell and reset style and MANUAL formatting
.Tables(1).Cell(1, 1).Select
.Style = ActiveDocument.Styles("Body Text 2")
With .Font
.Bold = True
.Size = 14
.NameAscii = "Arial"
.Underline = 0
End With
.EndKey unit:=wdStory
End With

Set oRange = Nothing
' Set newrange = Nothing

MOS MASTER
09-14-2005, 03:41 PM
Hi, :hi:

I've edited your post to include VBA tags so that the code is more readable. :whistle:

Killian
09-15-2005, 01:51 AM
There is no need for the second Range, at all. You in fact pointed out why - when the file is inserted the selection IS at the end of the document. So......
oRange is using Selection.End
newRange is using Bookmarks("\endofdoc").End
except.....they are the same thing! So why bother?? The second range is not needed.But isn't that only the case if you make your insertion at the end of a document? If you're inserting in the middle of a document, then the selection point will be at the end of the inserted content, not nessecarily the end of the whole doc.

If I understand this correctly, the requirement is to get the first table from the inserted content, so once the new range is defined, you can check it has at least one table and do something with the first one like thisDim oRange As Range
Dim newrange As Range
' put this at the insertion point
ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:="Here"
'insert the file
Selection.InsertFile FileName:="C:\Documents and Settings\Killian\Desktop\test.doc"
'set the range for the inserted content
Set newrange = ActiveDocument.Range(Start:=ActiveDocument.Bookmarks("Here").Start, _
End:=Selection.End)

'check the inserted content has a table
If newrange.Tables.Count > 0 Then
'apply some manual formatting to the topleft cell of the first table of the inserted range
With newrange.Tables(1).Cell(1, 1).Range
.Style = ActiveDocument.Styles("Body Text 2")
With .Font
.Bold = True
.Size = 14
.NameAscii = "Arial"
.Underline = 0
End With
End With
End If

espencer
09-15-2005, 04:07 AM
This works PERFECT. Thanks for all of the help and completing the code for me. Much appreciated. Also thanks to all those that have helped.

MOS MASTER
09-15-2005, 02:28 PM
This has become a interesting topic!

Nice work guys! :thumb

fumei
09-16-2005, 12:13 AM
True, the assumption was that it was the end of the document. I based that on the fact that multiple documents were being inserted.

However, you STILL do not need a second range. Instead of EndOfDoc, use EndOfSel, as the Selection point will always be at the end of the inserted file.

Set oRange = ActiveDocument.Range(Start:=ActiveDocument.Bookmarks("Here").Start, _
End:=ActiveDocument.Bookmarks("\endofsel").End)