Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Open Word document using VB.NET
Message
 
À
01/03/2005 03:44:34
Walter Meester
HoogkarspelPays-Bas
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
VB.NET 1.1
OS:
Windows XP SP2
Database:
MS SQL Server
Divers
Thread ID:
00990971
Message ID:
00993065
Vues:
30
>Hi Rick,
>
>>I'm not sure how VB.NET does this actually, but I think this is late binding with Option Strict off.
>
>Hence cathi's remark. Without the option strict off it would be early binding (correct ??)

No. If you use CreateObject() you're late binding. You get early binding only if you use the TLBIMP tool and use a .NET reference. And even then I'm not entirely sure whether .NET does early binding since it's using COM Interop to access the component.

+++ Rick ---


>>Early COM binding in VB uses the new operator or a definition of the actual imported type.
>
>>But I'm not 100% I don't know exactly wthat the VB compiler does since it can be so ambiguous with some commands.
>
>>Actually I see what the problem is now. He's using Object as the type and this won't work because the object doesn't have the methods he is accessing.
>
>Yes, the
>
>
wrdApp = CType(CreateObject("Word.Application"), Word.Application)
>
>Is the culprit. The createobject creates a latebinding object which then casted with a early binding typelib. Since the latebinding object is not of the same version of the early binding type library (Because it is originated from the dev machine), this creates problem when the versions don't match.
>
>
>Walter,
>
>
>>
>>So Cathi is actually correct in saying that turning Option Strict off will make that code work as VB.NET will then automatically try to parse the method and use Reflection to automatically call the appropirate COM method. D
>>
>>Definitely late binding!
>>
>>+++ Rick ---
>>
>>
>>>Hi rick,
>>>
>>>I'm not a 100% sure (as I'm a convicted fox knob), but to me it seems that this is early binding.
>>>Whether you use a generic ProgID like "Word.application" or specific "Word.application.8" it still in this case is early binding (just as you can do this in VFP with the CREATEOBJECTEX() command). The compile recognizes the Dim wrdDoc As Word.Document and retrieves the typelibrary and compiles with that. Any subsequent CREATEOBJECT() using this progid is going to use the typelibrary present on your machine during compile time.
>>>
>>>In VB you could do
>>>
DIM oWord AS Object
>>>oObject = CREATEOBJECT("Word.Application")
>>>
>>>to create late binding. Not sure if this still is the case in VB.NET ..
>>>
>>>Walter,
>>>
>>>
>>>
>>>
>>>>Uh, isn't that what he's doing already? He's using generic Word.Application (rather than the version specific ProgId)...
>>>>
>>>>I think the problem is something different since the error doesn't appear to occur on instantiation but when the doc is opened...
>>>>
>>>>+++ Rick ---
>>>>
>>>>>Hi Allan,
>>>>>
>>>>>When you are working with different versions of Word or Excel, you will need to write your Word interfacing with VB.NET, using Option Strict Off or use Reflection in C# (or VB.NET). This allows for late binding so the compiler does not link to a specific version. Here is a MSDN article that explains this and provides examples:
>>>>>
>>>>>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_vsto2003_ta/html/OffCSharp.asp
>>>>>
>>>>>
>>>>>>Hi Bonnie,
>>>>>>
>>>>>>You're right. I have OfficeXP, the client has Office 2000.
>>>>>>
>>>>>>>Your problem is probably different versions of Word on your client's box than what's on your dev box. Unfortunately, there are slight differences in how Word is opened in Word 2000 vs Word 2003. Which do you have and which does your client have?
>>>>>>>
>>>>>>>~~Bonnie
>>>>>>>
>>>>>>>
>>>>>>>>Hi,
>>>>>>>>
>>>>>>>>Just trying to open a Word Document using VB.NET.
>>>>>>>>
>>>>>>>>This code works on my dev box.
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>Dim wrdDoc As Word.Document
>>>>>>>>Dim wrdApp As Word.Application
>>>>>>>>Dim strLargEnvTemplate as String = "C:\EnvTemplate.doc"
>>>>>>>>
>>>>>>>>wrdApp = CType(CreateObject("Word.Application"), Word.Application)
>>>>>>>>wrdApp.Visible = True
>>>>>>>>
>>>>>>>>wrdDoc = wrdApp.Documents.Open(strLargEnvTemplate)
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>However, at the client site, an exception is created on
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>wrdDoc = wrdApp.Documents.Open(strLargEnvTemplate)
>>>>>>>>
>>>>>>>>'The error message is "Object Reference not set to instance of an object"
>>>>>>>>
>>>>>>>>'Word is instantiated OK - but the document will not load...
>>>>>>>>
>>>>>>>>
>>>>>>>>Any idea why the code works on my dev box but generates an exception on the production PC's?
>>>>>>>>
>>>>>>>>Is there a better way to open a Word Document from VB.NET?
+++ Rick ---

West Wind Technologies
Maui, Hawaii

west-wind.com/
West Wind Message Board
Rick's Web Log
Markdown Monster
---
Making waves on the Web

Where do you want to surf today?
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform