Group: microsoft.public.word.vba.general
From: =?Utf-8?B?R1M4MA==?=
Date: Monday, March 03, 2008 3:08 AM
Subject: Re: Controlling Pages in Word through automation

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

"Jay Freedman" wrote:

> 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.
>
> On Sun, 2 Mar 2008 11:01:01 -0800, GS80 wrote:
>
> >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
> >
> >
> >"Jay Freedman" wrote:
> >
> >> On Wed, 27 Feb 2008 14:04:00 -0800, GS80 wrote:
> >>
> >> >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
> >>
> >> 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.
>

Safety Articles | Usenet Groups | Usenet News | Bluegrass