P
Peri
I'm using MailMerge from C#. It works fine on all Word versions except
2003. I don't add reference to Word dll file
instead I use late binding. High level code looks like this.
word = new TWord();
word.Open(templateFile);
word.MailMerge(datasourceFile);
word.CloseDocument(); // close template document
word.Show();
The problem occurs after word.MailMerge(datasourceFile) call.
public void MailMerge(string datasourceFile)
{
object datasource = null;
object mailMerge = null;
object activeDocument = null;
try
{
activeDocument = getActiveDocument();
mailMerge = getPropertyObject(activeDocument, "MailMerge");
datasource = getPropertyObject(mailMerge, "Datasource");
// values here I got from macro registered whilie doing this in Word
// error occurs on any of this 3 calls
setPropertyValue(mailMerge, "Destination", new object[] { 0 });
setPropertyValue(datasource, "FirstRecord", new object[] { 1 });
setPropertyValue(datasource, "LastRecord", new object[] { -16 });
InvokeMethod(mailMerge, "OpenDatasource", new object[] { datasourceFile,
0 });
InvokeMethod(mailMerge, "Execute", null);
}
finally
{
ComHelper.ReleaseComObject(activeDocument);
ComHelper.ReleaseComObject(datasource);
ComHelper.ReleaseComObject(mailMerge);
}
}
I think the code speaks for itself. getPropertyObject, setPropertyValue
and InvokeMethod are just shorcuts for InvokeMember.
protected object getPropertyObject(object parentObject, string
propertyName)
{
return g_type.InvokeMember(propertyName,
BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance,
null,
parentObject,
null);
}
protected void setPropertyValue(object objectContaining, string
propertyName, object[] value)
{
g_type.InvokeMember(propertyName,
BindingFlags.SetProperty | BindingFlags.Public | BindingFlags.Instance,
null,
objectContaining,
value);
}
protected object InvokeMethod(object objectWithMethod, string methodName,
object[] parameters)
{
return g_type.InvokeMember(methodName,
BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance,
null,
objectWithMethod,
parameters);
}
I recorded Macro in Office 2003 and it looks that it does exactly the same
I'm doing here. I have no idea what
to do. Anyone has any ideas?
2003. I don't add reference to Word dll file
instead I use late binding. High level code looks like this.
word = new TWord();
word.Open(templateFile);
word.MailMerge(datasourceFile);
word.CloseDocument(); // close template document
word.Show();
The problem occurs after word.MailMerge(datasourceFile) call.
public void MailMerge(string datasourceFile)
{
object datasource = null;
object mailMerge = null;
object activeDocument = null;
try
{
activeDocument = getActiveDocument();
mailMerge = getPropertyObject(activeDocument, "MailMerge");
datasource = getPropertyObject(mailMerge, "Datasource");
// values here I got from macro registered whilie doing this in Word
// error occurs on any of this 3 calls
setPropertyValue(mailMerge, "Destination", new object[] { 0 });
setPropertyValue(datasource, "FirstRecord", new object[] { 1 });
setPropertyValue(datasource, "LastRecord", new object[] { -16 });
InvokeMethod(mailMerge, "OpenDatasource", new object[] { datasourceFile,
0 });
InvokeMethod(mailMerge, "Execute", null);
}
finally
{
ComHelper.ReleaseComObject(activeDocument);
ComHelper.ReleaseComObject(datasource);
ComHelper.ReleaseComObject(mailMerge);
}
}
I think the code speaks for itself. getPropertyObject, setPropertyValue
and InvokeMethod are just shorcuts for InvokeMember.
protected object getPropertyObject(object parentObject, string
propertyName)
{
return g_type.InvokeMember(propertyName,
BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance,
null,
parentObject,
null);
}
protected void setPropertyValue(object objectContaining, string
propertyName, object[] value)
{
g_type.InvokeMember(propertyName,
BindingFlags.SetProperty | BindingFlags.Public | BindingFlags.Instance,
null,
objectContaining,
value);
}
protected object InvokeMethod(object objectWithMethod, string methodName,
object[] parameters)
{
return g_type.InvokeMember(methodName,
BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance,
null,
objectWithMethod,
parameters);
}
I recorded Macro in Office 2003 and it looks that it does exactly the same
I'm doing here. I have no idea what
to do. Anyone has any ideas?