How do I position a graphic in a Word document?

A

aualias

I have an application that does a sort of mail merge. It prints out
letters. All the data is stored in SQL Server (template for the letter,
name, address, etc. A graphic for a company logo will also be stored.

The application will either be written in C# or VB.NET. Right now I am
testing with C#. I can pull the data from the database, replace the
placeholders in the template with the correct values, and print out a
letter.

I am using <application>.Selection.TypeText() for the creation of the
document, so that part is very straightforward.

I need to be able to position the graphic in the document. Most likely the
graphic will be stored as a bitmap in the database. If anyone can point me
to some information on how to do this (examples, articles, methods, etc.) I
would really appreciate it.

Thanks.

David
 
A

aualias

I got a bit further...

I am using this code to show the image. The positioning is arbitrary. I
need a way to measure the width of the page and position the image exactly.
I'm also very open to any other suggestions as to how to do this. It would
be great if I could insert a placeholder for the image.

Also, if the image is stored in the database (this is how the client wants
it) I do not know how to use it (other than saving it to a file and deleting
it after use). There has to be a better way.

object noParam = System.Reflection.Missing.Value;
object bLinkToFile = true;
object bSaveWithDoc = true;
object left = 0;
object top = 0;
object width = 100;
object height = 100;

Word.Shape shp = doc.Shapes.AddCanvas(300, 50, 100, 100, ref noParam);
shp.CanvasItems.AddPicture(
@"<path to image>\logo.jpg",
ref bLinkToFile,
ref bSaveWithDoc,
ref left,
ref top,
ref width,
ref height );

Thanks for any help.

David
 
D

Doug Robbins

The best thing would be to insert a one cell table into the template and
insert the graphic into the cell using the .Range object.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
A

aualias

Hi Doug,

Thanks for the reply. I have this working by creating the Word document and
inserting text and positioning the image manually. For example:

doc = app.Documents.Add(ref missing,ref missing, ref missing, ref missing);
app.Selection.Font.Name = "Arial";
app.Selection.Font.Size = 12;

I am getting the page size with doc.PageSetup properties, so I can position
the image where I like.

Also, I am creating my own place holders ( %<placeholder>% ) and using
regular expressions to do the replacement.

However, the user has to create the template as a text file, inserting some
sort of markup for the font, measuring the image location, etc. It is not
very user friendly. Ideally, I would like for the user to just create a
template with Word. Then they could choose the fonts and use your idea of
the one cell table for the graphic.

However, I do not know how I can do the text replacement in a Word template.
Right now I am simply replacing the place holders with Regex and then
inserting the text into the document.
Is there a way I can replace text within a template Word document?

Thanks again.

David
 
A

aualias

Doug,

Got it. Thanks.

David



Doug Robbins said:
The best thing would be to insert a one cell table into the template and
insert the graphic into the cell using the .Range object.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
B

BootN

Were you able to insert both the text and the image into a Word template or
is it still in the text file? I am in a similar position where I need to be
able to allow the users to modify the template (ie rearrange the field codes)
but still have the data insert where the codes lie (there I cant
prgrammatically select an absolute position)

Your help would be greatly appreciated
 

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