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;
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;