Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / March 2008

Tip: Looking for answers? Try searching our database.

Controlling Pages in Word through automation

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
GS80 - 27 Feb 2008 22:04 GMT
Hi All,

I'm fairly new to using the Word object model (2007) and have a couple of
questions regarding controling the pages programatically. What i need to be
able to do is to copy various charts and images from excel and lay them out
on a word page. I'm going to be resizing these images so that the fill a 1/4
page size each.

WHat i'd like to know firstly is how are new pages added? My intention is to
resize the images and ensure that there is no way that 4 images will flow
over onto the next page. How, if i do this can i add a new page and then
reference this to start adding further elements?

Any help is most appreciated, thanks in advance,

G
Jay Freedman - 28 Feb 2008 01:15 GMT
>Hi All,
>
[quoted text clipped - 12 lines]
>
>G

Word automatically adds a new page whenever the document's contents won't fit
within the current set of pages, or when you insert a manual page break. From a
programmatic viewpoint, though, the question is essentially meaningless.

The best way to solve this problem goes like this:

- Create a template specifically as a base for this type of document.
- Make a 2x2 table. Use the Table > AutoFit > Fixed Column Width and Table >
Table Properties > Row > Specify row height commands to fix the sizes of the
cells to 1/4 page each. (Note that Word requires a paragraph mark below a table,
so you can't make the table go all the way to the bottom margin. You can format
the paragraph mark to 1 pt, which will get you close.) Turn off the table's
borders if you want to.
- Save the table as a Building Block in this template.
- In a document based on the template, your code will start by inserting a table
from the Building Block.
- Insert the images, with inline wrapping, one per cell. If they're larger than
the cell dimensions, they'll automatically resize to fit the cell.
- After each set of four images, insert a second paragraph mark at the end of
the document (which will automatically create another page, since it won't fit
on the first page) and another copy of the table Building Block.

--
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.
GS80 - 28 Feb 2008 09:02 GMT
Sounds good, a very detailed explanation. I wasn't aware of the building
blocks concept. I presume if i need to i can merge cells in the table if i
want to use half page sizes. I'll give this a shot and see how i get on.

One question i do have is how to identify a specific page. Is this to do
with the sections object?

Thanks for your help.

> >Hi All,
> >
[quoted text clipped - 40 lines]
> Microsoft Word MVP        FAQ: http://word.mvps.org
> Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
GS80 - 02 Mar 2008 19:01 GMT
I've managed to get the template set up and insert the table from a building
block into the page followed by a paragraph mark which does as intended and
adds a new page. the following code does this:

object bb = "QtrPage";
               object richText = false;
               wordDoc = wordApp.Documents.Add(ref templateFile, ref
paramMissing, ref paramMissing, ref visable);

               //MessageBox.Show(wordApp.Documents.Count.ToString());
               wordDoc.Select();

               
((Microsoft.Office.Interop.Word.Template)wordDoc.get_AttachedTemplate()).BuildingBlockEntries.Item(ref bb).Insert(wordApp.Selection.Range,ref richText);

               //MessageBox.Show(wordDoc.Tables.Count.ToString());
               wordDoc.Tables[1].AllowAutoFit = false;
               wordDoc.Tables[1].AllowPageBreaks = false;
               wordDoc.Tables[1].Cell(1, 1).Select();
               wordApp.Selection.Paste();

prior to this code there is a piece which copies a chart from Excel and this
is pasted into one of the table elements.

This is all fine but when i try to copy the insert code and use it again it
overwrites the first table instead of adding it to the following page.

What i want to know is how to insert the second table on the second page.
and a better idea of how to insert further items into the document.

Thanks in advance,

G

> >Hi All,
> >
[quoted text clipped - 40 lines]
> Microsoft Word MVP        FAQ: http://word.mvps.org
> Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
Jay Freedman - 02 Mar 2008 23:39 GMT
Your .Insert statement uses wordApp.Selection.Range as the location to insert
the building block. If you blindly repeated all the code after the
wordApp.Documents.Add, you got a copy of the wordDoc.Select() statement that
selects the whole document, and then the second iteration of .Insert replaces
the existing document contents with the blank table from the building block.

The best fix also happens to be the quickest: Between the wordDoc.Select() and
the .Insert, add the statement

  wordApp.Selection.Collapse wdCollapseEnd

(I'm not sure what qualifiers you need on wdCollapseEnd to use it in your code;
it's a member of the wdCollapseDirection enumeration, with a value of 0.)

The result of that is that the Selection becomes a single point immediately to
the left of the final paragraph mark in the document, which is guaranteed not to
be inside any existing table.

>I've managed to get the template set up and insert the table from a building
>block into the page followed by a paragraph mark which does as intended and
[quoted text clipped - 68 lines]
>> the document (which will automatically create another page, since it won't fit
>> on the first page) and another copy of the table Building Block.

--
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.
GS80 - 03 Mar 2008 09:08 GMT
Thanks again. This with the WordApp.Selection.MoveEnd() method allowed me to
insert table after table but it does raise another question.

It only ever appears to think that there is one table in the document
regardless of how many I add. I can only reference the table with Index 1.
I'm making the assumption that this is because the table when creating the
building block had this index and as such it looks to add it to a document
like this. Is there anyway to specify what table i add the images too? This
should be easy yet i'm still having trouble with it.

Thanks again

G

> Your .Insert statement uses wordApp.Selection.Range as the location to insert
> the building block. If you blindly repeated all the code after the
[quoted text clipped - 92 lines]
> Microsoft Word MVP        FAQ: http://word.mvps.org
> Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
Jay Freedman - 03 Mar 2008 13:07 GMT
When you insert a table into the paragraph immediately after an existing table,
Word combines them into one table. This isn't true just for macros; it works the
same way when you use the Insert > Table command from the ribbon. To have two
consecutive tables that are separate, you need at least one paragraph mark
between them.

The easy way to fix this is to include an extra empty paragraph mark as the last
character in the building block. The alternative is to have the macro insert a
paragraph at the end of the document as part of each loop.

If you single-step through the macro code (use F8 in the VBA editor), with
nonprinting characters turned on in the document, after the first table is
inserted there should be two paragraph marks at the end of the document -- one
from the building block and the other as the required final paragraph mark of
the document.

>Thanks again. This with the WordApp.Selection.MoveEnd() method allowed me to
>insert table after table but it does raise another question.
[quoted text clipped - 100 lines]
>> >> the document (which will automatically create another page, since it won't fit
>> >> on the first page) and another copy of the table Building Block.

--
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.

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.