Reply
 
Thread Tools Display Modes
  #1   Report Post  
Posted to microsoft.public.word.tables
Tasin
 
Posts: n/a
Default How to add multiple tables via vbscript?

Hi,

I'm trying to create a multi-table doc via a vbscript, with some text
between each table (a title effectively). The script I've come up with adds
each of the items, but just overwrites the previous item. ie adds the first
title, then overwrites with a table, then overwrites that with the next title
and so on.

I think I'm missing something pretty simple, just to move the selction on,
but as usual, it's the simple thing that's proving to be the hardest. My
script is listed below for anyone who can help me

Thanks.


Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Add()
Set objSelection = objWord.Selection

objSelection.EndKey wdStory, wdMove

'define table section, including title
objSelection.Font.Size = "12"
objSelection.TypeParagraph()
objSelection.TypeText "title 1"
objSelection.TypeParagraph()
objSelection.EndKey wdStory, wdMove

Set objRange = objDoc.Range()
objDoc.Tables.Add objRange, 1,3
Set objTable = objDoc.Tables(1)
objTable.AutoFormat(16)

Set objSelection = objWord.Selection
objSelection.EndKey wdStory, wdMove

'define table section, including title
objSelection.Font.Size = "12"
objSelection.TypeParagraph()
objSelection.TypeText "title2"
objSelection.TypeParagraph()
objSelection.EndKey wdStory, wdMove

Set objRange = objDoc.Range()
objDoc.Tables.Add objRange, 1, 4
Set objTable = objDoc.Tables(2)
objTable.AutoFormat(6)

  #2   Report Post  
Posted to microsoft.public.word.tables
Jay Freedman
 
Posts: n/a
Default How to add multiple tables via vbscript?

The reason you're overwriting previous tables with new ones is that you set
objRange = objDoc.Range(), which is literally "the range of the entire
document", and then you add the new table using that range. Therefore the
table replaces anything that's already in the document.

One way to fix it is to add this line immediately after each Set objRange
statement:

objRange.Collapse 0

(In VBA the constant wdCollapseEnd has the value 0. The named constant isn't
available to you in vbscript, so you have to use the numeric value.) That
causes the range to consist of the single point between the last character
and the final paragraph mark.

Instead of the Collapse statement, since you've already moved the selection
to the end of the document with the EndKey statement, you can just replace
each Set objRange = objDoc.Range() statement with this:

Set objRange = objSelection.Range

By the way, this post would have been more on-topic in one of the
programming newsgroups, such as microsoft.public.word.vba.general.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

Tasin wrote:
Hi,

I'm trying to create a multi-table doc via a vbscript, with some text
between each table (a title effectively). The script I've come up
with adds each of the items, but just overwrites the previous item.
ie adds the first title, then overwrites with a table, then
overwrites that with the next title and so on.

I think I'm missing something pretty simple, just to move the
selction on, but as usual, it's the simple thing that's proving to be
the hardest. My script is listed below for anyone who can help me

Thanks.


Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Add()
Set objSelection = objWord.Selection

objSelection.EndKey wdStory, wdMove

'define table section, including title
objSelection.Font.Size = "12"
objSelection.TypeParagraph()
objSelection.TypeText "title 1"
objSelection.TypeParagraph()
objSelection.EndKey wdStory, wdMove

Set objRange = objDoc.Range()
objDoc.Tables.Add objRange, 1,3
Set objTable = objDoc.Tables(1)
objTable.AutoFormat(16)

Set objSelection = objWord.Selection
objSelection.EndKey wdStory, wdMove

'define table section, including title
objSelection.Font.Size = "12"
objSelection.TypeParagraph()
objSelection.TypeText "title2"
objSelection.TypeParagraph()
objSelection.EndKey wdStory, wdMove

Set objRange = objDoc.Range()
objDoc.Tables.Add objRange, 1, 4
Set objTable = objDoc.Tables(2)
objTable.AutoFormat(6)



  #3   Report Post  
Posted to microsoft.public.word.tables
Tasin
 
Posts: n/a
Default How to add multiple tables via vbscript?

Thanks Jay - very helpful!! (not only the solution, but also the explanation).

Apologies for posting here rather than the programming groups - wasn't aware
there was a better place for it.

"Jay Freedman" wrote:

The reason you're overwriting previous tables with new ones is that you set
objRange = objDoc.Range(), which is literally "the range of the entire
document", and then you add the new table using that range. Therefore the
table replaces anything that's already in the document.

One way to fix it is to add this line immediately after each Set objRange
statement:

objRange.Collapse 0

(In VBA the constant wdCollapseEnd has the value 0. The named constant isn't
available to you in vbscript, so you have to use the numeric value.) That
causes the range to consist of the single point between the last character
and the final paragraph mark.

Instead of the Collapse statement, since you've already moved the selection
to the end of the document with the EndKey statement, you can just replace
each Set objRange = objDoc.Range() statement with this:

Set objRange = objSelection.Range

By the way, this post would have been more on-topic in one of the
programming newsgroups, such as microsoft.public.word.vba.general.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

Tasin wrote:
Hi,

I'm trying to create a multi-table doc via a vbscript, with some text
between each table (a title effectively). The script I've come up
with adds each of the items, but just overwrites the previous item.
ie adds the first title, then overwrites with a table, then
overwrites that with the next title and so on.

I think I'm missing something pretty simple, just to move the
selction on, but as usual, it's the simple thing that's proving to be
the hardest. My script is listed below for anyone who can help me

Thanks.


Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Add()
Set objSelection = objWord.Selection

objSelection.EndKey wdStory, wdMove

'define table section, including title
objSelection.Font.Size = "12"
objSelection.TypeParagraph()
objSelection.TypeText "title 1"
objSelection.TypeParagraph()
objSelection.EndKey wdStory, wdMove

Set objRange = objDoc.Range()
objDoc.Tables.Add objRange, 1,3
Set objTable = objDoc.Tables(1)
objTable.AutoFormat(16)

Set objSelection = objWord.Selection
objSelection.EndKey wdStory, wdMove

'define table section, including title
objSelection.Font.Size = "12"
objSelection.TypeParagraph()
objSelection.TypeText "title2"
objSelection.TypeParagraph()
objSelection.EndKey wdStory, wdMove

Set objRange = objDoc.Range()
objDoc.Tables.Add objRange, 1, 4
Set objTable = objDoc.Tables(2)
objTable.AutoFormat(6)




Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to delete multiple tables? Á÷À˵ÄË«Óã Tables 7 March 22nd 06 01:43 PM
Tables setting multiple section breaks with multiple headers [email protected] Tables 1 January 19th 06 11:05 AM
Create multiple stacked tables in word using vb.net Jenny Beavers Tables 2 January 17th 06 04:12 PM
Changing format of multiple pre-created tables Wendy Microsoft Word Help 2 September 14th 05 08:55 AM
mailmerging multiple tables from access in one word-document: how? svdh Mailmerge 2 December 15th 04 03:32 PM


All times are GMT +1. The time now is 10:19 AM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"