Adding a shape using Microsoft Publisher 11.0 Object Library

S

stolin

After a 12 hours workday I can’t think at all. I need help with programming
Publisher. First of all I can’t use C# because

Publisher.Table test =
document.Pages[1].Shapes.AddTable(some args) as Table;
test.Rows[1].Cells[1].TextRange.Text = "test";

is not working, “object reference not set to an instance of an objectâ€, and
there is no “with†in C#. But no problem I can use VB.

Is there any easy way to find a shape in the document? I will rather draw a
table in the template and then fill it with data than I will add the same
table programmatically on every page. I haven’t tried to find a shape by its
index. And how do I know the index of a shape?

But if I have to add a table anyway, do I have to format every single cell
or can I just to make the changes for the whole table?

I tried using “FindReplaceâ€. My idea was to have tags in the template such
as “$text$â€. I would just fin the tag and replace it with the real data. But
the next page I add copies the data there the tags have already been
overwritten. The second issue here is performance. If my document grows to be
over 5000 pages it can take long time to search through the document.

Is it possible to find more help except Microsoft’s help files?

Ed Bennett to the rescue? ;-)
 
E

Ed Bennett

stolin said:
Publisher.Table test =
document.Pages[1].Shapes.AddTable(some args) as Table;
test.Rows[1].Cells[1].TextRange.Text = "test";

is not working, "object reference not set to an instance of an
object", and there is no "with" in C#. But no problem I can use VB.

I don't speak C#, but I know a very small amount of C. Is "as Table" valid
at the end of a C# statement? Isn't that what the Publisher.Table at the
start of the statement is for?
Is there any easy way to find a shape in the document? I will rather
draw a table in the template and then fill it with data than I will
add the same table programmatically on every page. I haven't tried to
find a shape by its index. And how do I know the index of a shape?

Trial and error is the only way to know the index of a shape.

You can use an object data type to hold a reference to the table, though.
Once you've done that, you don't need the index. The tricky part is getting
the reference in the first place. The easiest way is to create the table
through code. Otherwise, you have to iterate through shapes, checking
whether or not they're tables, and then checking whether or not they match
the criteria you need.

To be able to find the shape(s) the next time you open the document (when
your code is reset and so the references no longer exist), you can add a tag
to the shape. There's info on this in the object model reference.

For Each is a useful construct in VB, I'm not sure what the C# equivalent
is.
But if I have to add a table anyway, do I have to format every single
cell or can I just to make the changes for the whole table?

I'd think some changes have to be applied to single cells, some across the
table, some across cell ranges.

I'm not sure, though, as I haven't really worked with tables in Publisher's
OM.
I tried using "FindReplace". My idea was to have tags in the template
such as "$text$". I would just fin the tag and replace it with the
real data. But the next page I add copies the data there the tags
have already been overwritten.

Copy a source table?

Use the ClipBoard to store the table with the tags in, then paste it on each
concurrent page?

(There is a polite way to utilise the ClipBoard like this - make sure you
store its old contents, and replace it when you're done. Use GetIDataObject
and SetIDataObject for this purpose; you'll need to reference the Scripting
runtime. If I get the chance, I'll write a short tutorial on how to do that
(it's little more than a code fragment).
The second issue here is performance.
If my document grows to be over 5000 pages it can take long time to
search through the document.

5000 pages is a BIG document. Have you thought of splitting the project
into smaller documents by batch? Or does it have to be a 5000-page
Publisher file? (5000-page Publisher files are unwieldly and hit your
system pretty hard at the best of times, even when not running
search-replace across them!)
Is it possible to find more help except Microsoft's help files?

There are some articles on MSDN, although I'm not sure how helpful they'll
be in your case.
http://msdn.microsoft.com/office/understanding/publisher/articles/default.aspx

Not many people use the Publisher VBA from what I can tell, and so it's not
really worth most tech sites including articles on.
Ed Bennett to the rescue? ;-)

Hopefully :eek:)
 
S

stolin

I don't speak C#, but I know a very small amount of C. Is "as Table" valid
at the end of a C# statement? Isn't that what the Publisher.Table at the
start of the statement is for?

Because AddTable returns a Shape I was trying to cast is as a Table. But now
I found that I have to instantiate a Shape and then use property Table to get
to the table I just created. So that is solved.
Trial and error is the only way to know the index of a shape.

I don't have the time. ;-)
You can use an object data type to hold a reference to the table, though.
Once you've done that, you don't need the index. The tricky part is getting
the reference in the first place. The easiest way is to create the table
through code. Otherwise, you have to iterate through shapes, checking
whether or not they're tables, and then checking whether or not they match
the criteria you need.

I took Your advice and now creating shapes, (tables and text boxes)
programmatically.
I'd think some changes have to be applied to single cells, some across the
table, some across cell ranges.

The problem is that there is no way to format a whole table. You have to
drill down to a Row or Cell level. On Rows level you can only change the
height. But as You said foreach is very helpful here.
5000 pages is a BIG document. Have you thought of splitting the project
into smaller documents by batch? Or does it have to be a 5000-page
Publisher file? (5000-page Publisher files are unwieldly and hit your
system pretty hard at the best of times, even when not running
search-replace across them!)

I use it to create a document which will be scanned by a machine. So some
text has to be placed within an error margin of +- 0.5 mm. So I don’t really
work with the file. It just goes directly to print. But I have to say You’ve
got me nervous there. Maybe Publisher wasn’t the best choice ever?
There are some articles on MSDN, although I'm not sure how helpful they'll
be in your case.
http://msdn.microsoft.com/office/understanding/publisher/articles/default.aspx

Been there, done that.
Not many people use the Publisher VBA from what I can tell, and so it's not
really worth most tech sites including articles on.

I might write a small article to describe the experience (if I ever will
find the time).
Hopefully :eek:)

Thanks a lot!
 
E

Ed Bennett

stolin said:
I use it to create a document which will be scanned by a machine. So
some text has to be placed within an error margin of +- 0.5 mm. So I
don't really work with the file. It just goes directly to print. But
I have to say You've got me nervous there. Maybe Publisher wasn't the
best choice ever?

You can create multiple ten-page documents by automating Publisher from a
separate C# application. Then send 500 print jobs. Or create 500 PDF
files, and send those, or concatenate the PDF files in Acrobat and send a
single large PDF.
 

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