.NET Forum / .NET Framework / Interop / June 2004
Word Development Mixed Office Environment
|
|
Thread rating:  |
Joel Rumerman - 21 Jun 2004 20:05 GMT Hi!
I'm trying to develop some MS Word automation from within a VB.NET WinForm application. My user base has either Office XP or Office 2003 installed. I currently have Office 2003 installed. Two questions:
1. Is it possible for me to develop against the COM V10 without installing Office XP? (Can 2003 and XP be installed side-by-side?)
2. If I develop against the Office XP COM V10, will my Office 2003 users be okay? Are the COM Components backwards compatible?
Thx, Joel
"Peter Huang" - 22 Jun 2004 06:33 GMT Hi Joel,
As we know that Office XP and Office 2003 all have a set of PIAs, which can not be replaced with each other. i.e. we can use one version PIA with another version of office product. So if we wants to develop an office application to run both on office xp and office 2003 we need to develop the application on an machine office xp and office xp PIA. Because the office COM library is backwards compatible, so the lower version application can run on the machine with high version, but we can not guarantee the behavior if we do that in the reverse order.
In all, if you wants your application to run on both office xp and office 2003, I think we would better develop on the machine with office xp and office xp PIA. Or we can just use latebinding, which we do not need to use the PIA. But the approach is less efficient. e.g. private void button1_Click(object sender, System.EventArgs e) { Type t = System.Type.GetTypeFromProgID("Excel.Application"); Object o = Activator.CreateInstance(t); t.InvokeMember("Visible",BindingFlags.SetProperty,null,o,new object[]{true}); }
Best regards,
Peter Huang Microsoft Online Partner Support
 Signature Get Secure! - www.microsoft.com/security This posting is provided "AS IS" with no warranties, and confers no rights.
Joel Rumerman - 22 Jun 2004 17:21 GMT Peter,
Thanks for the clarification. We realized we probably could develop on Office XP and have the backwards compatibility of COM Libraries handle Office 2003 requests, but we had not thought of late-binding. It's just been so long since I've done it!! We're actually going to take the late-binding approach because I've already used intellisense to develop the code using the 2003 PIA and all I have to do now is switch over the object creation code. Also, the one or two machines that are running Office 2000 will most likely work too!
Thanks for the help.
-Joel
> Hi Joel, > [quoted text clipped - 27 lines] > Get Secure! - www.microsoft.com/security > This posting is provided "AS IS" with no warranties, and confers no rights. "Peter Huang" - 23 Jun 2004 02:56 GMT Hi Joel,
I think you may take a look at the links as below for reference about late-binding. INFO: Writing Automation Clients for Multiple Office Versions http://support.microsoft.com/default.aspx?scid=KB;EN-US;244167
INFO: Using Early Binding and Late Binding in Automation http://support.microsoft.com/default.aspx?scid=KB;EN-US;245115
HOW TO: Binding for Office Automation Servers with Visual C# .NET http://support.microsoft.com/default.aspx?scid=KB;EN-US;302902
INFO: Develop Microsoft Office Solutions with Visual Studio .NET http://support.microsoft.com/default.aspx?scid=KB;EN-US;311452
Best regards,
Peter Huang Microsoft Online Partner Support
 Signature Get Secure! - www.microsoft.com/security This posting is provided "AS IS" with no warranties, and confers no rights.
Joel Rumerman - 23 Jun 2004 19:15 GMT Thanks for the links.
I've got the code working using Objects and the CreateObject method (Dim wordApp as Object = CreateObject("Word.Application")), but I'm attempting to use the InvokeMember method and I'm struggling with the syntax. Can you help?
Here's what I have so far.
Dim appType As System.Type = System.Type.GetTypeFromProgID("Word.Application") Dim docType As System.Type = System.Type.GetTypeFromProgID("Word.Document")
Dim wordApp As Object = Activator.CreateInstance(appType)
Dim docs As Object = appType.InvokeMember("Documents", BindingFlags.GetProperty, Nothing, wordApp, Nothing)
Dim aDoc As Object = docs.GetType.InvokeMember("Add", BindingFlags.InvokeMethod, Nothing, docs, New Object() {"C:\Program Files\Common Files\R&D Cold Storage\RDLabelTemplate.dot", False, 0, True})
appType.InvokeMember("Visible", BindingFlags.SetProperty, Nothing, wordApp, New Object() {True})
docType.InvokeMember("Activate", BindingFlags.InvokeMethod, Nothing, aDoc, Nothing)
'IT WORKS FINE UP TO HERE. It creates the document using the DOT and activates it.
Dim tables As Object = aDoc.GetType.InvokeMember("Tables", BindingFlags.GetProperty, Nothing, aDoc, Nothing)
Dim table As Object = tables.GetType.InvokeMember("Item", BindingFlags.GetProperty Or BindingFlags.Instance, Nothing, tables, New Object() {1})
Dim rows As Object = table.GetType.InvokeMember("Rows", BindingFlags.GetProperty, Nothing, table, Nothing)
. . It seems that the aDoc.GetType.InvokeMember("Tables...) doesn't work properly. There isn't a Word.Tables object and I'm stuck as to why it won't retrive the property correctly. What am I doing wrong? If I code ...
Dim tables as Object = aDoc.Tables(1) or
Dim tables as Object = aDoc.tables.Item(1)
that works okay, but is there a way to use InvokeMember??
Thanks so much!
-Joel
> Hi Joel, > [quoted text clipped - 19 lines] > Get Secure! - www.microsoft.com/security > This posting is provided "AS IS" with no warranties, and confers no rights. "Peter Huang" - 24 Jun 2004 03:30 GMT Hi Joel,
I think you may try the code below. Dim tbls As Object = docType.InvokeMember("Tables", BindingFlags.GetProperty, Nothing, aDoc, Nothing) Dim table As Object = tbls.GetType().InvokeMember("Item", BindingFlags.InvokeMethod, Nothing, tbls, New Object() {1}) Dim rows As Object = table.GetType.InvokeMember("Rows", BindingFlags.GetProperty, Nothing, table, Nothing) Dim ct As Integer = CType(rows.GetType().InvokeMember("Count", BindingFlags.GetProperty, Nothing, rows, Nothing), Integer) Console.WriteLine(ct)
The Item should be a method of the Tables object.
Best regards,
Peter Huang Microsoft Online Partner Support
 Signature Get Secure! - www.microsoft.com/security This posting is provided "AS IS" with no warranties, and confers no rights.
Joel Rumerman - 24 Jun 2004 22:30 GMT Hi Peter,
Thanks for the code. I had used "GetProperty" instead of "InvokeMethod" as the binding flag on the Item. How can you tell which one to use? I brought my Word COM library into VS.NET and took a look at the Object Library and it said that Item was the default property of Tables. I'm assuming that is the case only for the TLB .NET uses to access the COM object. So how do I find out which methods are methods, properties, etc?
I've gotten the code I wanted to use reflection for working correctly. If you know, what are the performance gains / hits of using reflection (i.e. InvokeMember) vs. using the Dim wordApp as Object = CreateObject("Word.Application") and working with the object as you would if you had intellisense and all that information?
Thanks for all your help so far. This is my first time using reflection on a COM object so it's taking me some time to get used to it.
-Joel
> Hi Joel, > [quoted text clipped - 18 lines] > Get Secure! - www.microsoft.com/security > This posting is provided "AS IS" with no warranties, and confers no rights. "Peter Huang" - 25 Jun 2004 07:12 GMT Hi Joel,
I think what you observe in the Object Viewer should be the "Microsoft.Office.Interop.Word", which is an .net wrap assembly for the Word COM library. So it has wrap the item as the property, if you use the early binding, i.e. not use the createobject or invokemember, while use the intellisense directly, you will go this way. But if we wants to use the late binding ,we need to go the com library way directly.
you may take a look at the word vba helper, we will find that the item should be method of the tables. You may search tables in the help file below. The help file is usually located in the path below. c:\Program Files\Microsoft Office\OFFICE11\1033\VBAWD10.CHM
As for the Latebinding, we had memtion two methods, use the createinstance or createobject? In .NET framework, the two approaches are go the same way, at last, they both will call the reflection code as we do. Why we can use the CreateObject in VB.NET is because we need to make the vb.net compatible with the syntax with VB6, so that the code migration will be easier. i.e. the Createobject will call the CreateInstance in the .net framework reflection implementation. So I think there will no large performance hit, and it seems that use the Createobject approach will be more convenient am I right?
If we use the intellisense, i.e. do not use the latebinding and reflection, and add reference to the com library, the performance will be more better. You may refer to the article I provide in the last reply about latebinding and earlybinding.
Best regards,
Peter Huang Microsoft Online Partner Support
 Signature Get Secure! - www.microsoft.com/security This posting is provided "AS IS" with no warranties, and confers no rights.
Joel Rumerman - 25 Jun 2004 17:16 GMT Hi Peter,
I figured that what I was viewing in the Object Viewer was .NET's wrapper for the COM Object. It would be nice if the Object Viewer gave a direct link out to the COM Viewer tool that came with VB6, however.
I will say that CreateObject was an easier implementation than the InvokeMember, but because CreateObject is only there to help migration from VB6 to VB.NET, I was hesitant to use it. I'm not migrating this application from VB6 - VB.NET so why use a hack that was put in place to help it? I also wanted to learn more about Reflection.
Again, the reason I need to use late-binding is because I'm not sure which version of Office, XP or 2003 I'll be deploying the application against. I have 2003 installed and to use the .NET wrapper assemblies for Office XP I would have to reinstall Office XP which is something I'm trying to avoid. The use of late-binding allowed me to avoid this.
Thanks for all your help. You've been a great resource.
Regards, Joel
> Hi Joel, > [quoted text clipped - 36 lines] > Get Secure! - www.microsoft.com/security > This posting is provided "AS IS" with no warranties, and confers no rights. "Peter Huang" - 28 Jun 2004 02:34 GMT Hi Joel,
I think we can customized the Object Browser in the vs.net by following the steps below. 1. In the Object Browser, click Customized and then select Add 2. In the component selector dialog, click the COM tab and select the Word COM library and click OK 3. And now we can browse the Word COM library in the VS.NET IDE Object Browser.
Also the why the VB.NET using the CreateObject like syntax, one purpose is to help migrate vb6 application to vb.net application, the other one is to keep the syntax consistent with VB6, so that the VB users do not need to make many changes about their programming approach and habits. The .NET framework's goad is to provide the programmer using different language with the unified framework, that is to say, even if you are using VB.NET, C#, J# or what ever, you can keep using your original programming language, so that we do not need to learn the new syntax.
Use the latebinding will avoid the version issue, but we still need to pay attention to the problem that the feature in the Office 2003 may not work in the office xp because of the COM library's back-compatibility.
Hope this helps.
Best regards,
Peter Huang Microsoft Online Partner Support
 Signature Get Secure! - www.microsoft.com/security This posting is provided "AS IS" with no warranties, and confers no rights.
Joel Rumerman - 28 Jun 2004 17:16 GMT Peter,
Thanks. Adding the COM library to the Object Browser proves to be extremely helpful. Although I'm interacting with the Word 11 object in a seemingly simple way (I'm just building a table on the fly), you are correct in that because I'm programming against Word 11 I will need to install the application to a machine running Office XP and run a thorough test of the code.
-Joel
> Hi Joel, > [quoted text clipped - 28 lines] > Get Secure! - www.microsoft.com/security > This posting is provided "AS IS" with no warranties, and confers no rights. Rajiv R - 23 Jun 2004 05:44 GMT Hi,
Is it possible to develop with OfficeXP PIAs without installing OfficeXP on the development machine?
 Signature Cheers! Rajiv. R Rajspace.Org
> Peter, > [quoted text clipped - 46 lines] > > This posting is provided "AS IS" with no warranties, and confers no > rights. Joel Rumerman - 23 Jun 2004 18:35 GMT I don't believe so because the Office XP PIAs are just the type libraries for the COM Components. You may be able to program with just the PIAs, but during compiliation or JIT compiliation it would throw errors saying it can't find the necessary COM components.
-Joel
> Hi, > [quoted text clipped - 56 lines] > > > This posting is provided "AS IS" with no warranties, and confers no > > rights.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|