Microsoft.Office.Interop.Word stops working after inserting a pict

  • Thread starter Andrés Meza-Escallón
  • Start date
A

Andrés Meza-Escallón

Greetings.
I developed a WinForms .NET 2.0 application that utilizes the Microsoft Word
11 COM Object Library to replace data fields in a .dot template. It works
fine replacing the selected data fields by text when I target either Office
2003 or Office 2007. However, when it tries to replace a selected data field
by an image using Office 2007, the
Microsoft.Office.Interop.Word.ApplicationClass.Selection object just stops
working (with Office 2003 this replacement by an image still works as
expected), no matter what Word method the application call after that.

In an attempt to isolate the problem, I created a simple application with
just the interaction with Word and... it worked with Office 2007! This
situation is exactly the same using the Microsoft Word 12.0 COM Object
Library that comes with Office 2007.

So, I would like to know what in my application could be making a conflict
with the Microsoft.Office.Interop.Word.ApplicationClass object that causes
this kind of behavior. Otherwise, I would like to learn about another
approach I could use to do the same task.

My sample code is:

ApplicationClass wordApp;
Document destination;
string templatesPath =
System.Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) +
"\\My company\\My application\\Templates\\";
string pCompanyLogo = "E:\\niño_libro_tute.JPG";
string pCompanyName = "my big company with a huge billboard";
string pCompanyWeb = "http://www.huge billboard.biz";

wordApp = new ApplicationClass();
//this.wordApp.Visible = false;
wordApp.Visible = true;

// Create new destination file
object missing = System.Reflection.Missing.Value;
object isVisible = true;
object templateFilename = templatesPath + "Empty_base.dot";
destination = wordApp.Documents.Add(ref templateFilename, ref
missing, ref missing, ref isVisible);

templateFilename = templatesPath + "ContractForm.dot";

destination.Activate();

// Open new document based on selected template
object doSaveChanges = false;
object filename = "c:\\Temp\\" + "temp.doc";
Document doc = wordApp.Documents.Add(ref templateFilename, ref
missing, ref missing, ref isVisible);

int fieldsCount = doc.Fields.Count;
foreach (Field field in doc.Fields)
{
this.textBox1.Text += "Current field: " + field.Code.Text +
"\r";
if (field.Code.Text.Contains("COMPANYLOGO"))
{
field.Select();
object link = false;
object saveWithDoc = true;
object selectionRange = wordApp.Selection.Range;


wordApp.Selection.InlineShapes.AddPicture(pCompanyLogo,
ref link, ref saveWithDoc, ref missing);
this.textBox1.Text += "I've just inserted the picture\r";
}
else if (field.Code.Text.Contains("COMPANYNAME"))
{
field.Select();
wordApp.Selection.TypeText(pCompanyName);
}
else if (field.Code.Text.Contains("COMPANYWEB"))
{
field.Select();
wordApp.Selection.TypeText(pCompanyWeb);
}
}

wordApp.Selection.WholeStory();
wordApp.Selection.Copy();
doc.Close(ref doSaveChanges, ref missing, ref missing);
destination.Activate();
wordApp.Selection.PasteAndFormat(WdRecoveryType.wdPasteDefault);

// Preview
--------------------------------------------------------------
destination.Activate();
wordApp.ActiveWindow.ActivePane.View.Type =
WdViewType.wdPrintView;
// Zoom to two pages
wordApp.ActiveWindow.ActivePane.View.Zoom.PageColumns = 2;
wordApp.ActiveWindow.ActivePane.View.Zoom.PageRows = 1;
// Go to first page
object homeUnit = WdUnits.wdStory;
wordApp.Selection.HomeKey(ref homeUnit, ref missing);

wordApp.Visible = true;
 
C

Cindy M.

Hi =?Utf-8?B?QW5kcsOpcyBNZXphLUVzY2FsbMOzbg==?=,

<< However, when it tries to replace a selected data field
by an image using Office 2007, the
Microsoft.Office.Interop.Word.ApplicationClass.Selection object just stops
working (with Office 2003 this replacement by an image still works as
expected), no matter what Word method the application call after that.>>

Hard to be sure, since it's not clear what kind of fields you're using as
targets, here. And whether you expect the fields to still exist after you've
processed the document. My best guess, given that you're using fields +
Selection object, is that the problem may be an option that controls what
happens when a field is selected. Generally, it's a good idea to avoid using
Selection and work with Range, instead. I might try something like this (off
the top of my head).

Dim rng as Word.Range

Set rng = field.Result
field.Delete
rng.InlineShapes.AddPicture...
If (field.Code.Text.Contains("COMPANYLOGO"))
{
field.Select();
object link = false;
object saveWithDoc = true;
object selectionRange = wordApp.Selection.Range;


wordApp.Selection.InlineShapes.AddPicture(pCompanyLogo,
ref link, ref saveWithDoc, ref missing);

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

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