Controlling Pages in Word through automation

G

GS80

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
 
J

Jay Freedman

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

GS80

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

GS80

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
 
J

Jay Freedman

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
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 said:
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.
 
G

GS80

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 said:
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
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 said:
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
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
 
J

Jay Freedman

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.

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 said:
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
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,

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top