Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Open Word document using VB.NET
Message
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:
00992271
Vues:
41
Hi Allan,

Try changing the Destination line to the following:
objMailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument
>Hi Cathi,
>
>I'm very close to getting this to work using late binding. I'm able to open document templates, populate a mail merge source - but the one stumbling block is setting the Destination proprety of the MailMerge object.
>
>I actually got this to work in VB6 using:
>
>
>
>objMailMerge.Destination = wdSendToNewDocument
>
>'or
>
>objMailMerge.Destination = 0
>
>
>
>However, in VB.Net, settin the destination isn't as simple. I've tried a number of different things the last few hours to set the destination of the mail merge - any ideas on how this property is set using VB.NET?
>
>I think I've been looking at this stuff too long... The answer is probably staring me in the face but I'm stuck right now.
>
>
>
>
>
>
>
>>Rick,
>>
>>VB.NET is different than C# in that setting Option Strict Off, you let the compiler handle all the crud behind the scenes to use reflection. To use CreateObject() and just call into the properties and methods, you must set Option Strict Off. If you don't then the compiler will error. You are correct in saying that CreateObject uses late-binding. The compiler would error out when you tried to access any methods or properties directly. For example, here is sample Excel code which would error out if you didn't set Option Strict Off:
>>
>>
>>Dim objApp As Object
>>Dim objBook As Object
>>Dim objBooks As Object
>>Dim objSheets As Object
>>Dim objSheet As Object
>>Dim range As Object
>>
>>' Instantiate Excel and start a new workbook.
>>objApp = CreateObject("Excel.Application")
>>objBooks = objApp.Workbooks
>>objBook = objBooks.Add
>>objSheets = objBook.Worksheets
>>objSheet = objSheets.Item(1)
>>
>>range = objSheet.Range("A1")
>>
>>'Set the range value.
>>range.Value = "Hello, World!"
>>
>>'Return control of Excel to the user.
>>objApp.Visible = True
>>objApp.UserControl = True
>>
>>
>>If you wanted to not set Option Strict Off, then you need to use Reflection to have the code compile. This is how it is done in C#. Here is the same functionality as above but using Reflection and keeping Option Strict On:
>>
>>
>>Dim objApp_Late As Object
>>Dim objBook_Late As Object
>>Dim objBooks_Late As Object
>>Dim objSheets_Late As Object
>>Dim objSheet_Late As Object
>>Dim objRange_Late As Object
>>Dim Parameters() As Object
>>
>>Try
>>    ' Get the class type and instantiate Excel.
>>    Dim objClassType As Type
>>    objClassType = Type.GetTypeFromProgID("Excel.Application")
>>    objApp_Late = Activator.CreateInstance(objClassType)
>>
>>    'Get the workbooks collection.
>>    objBooks_Late = objApp_Late.GetType().InvokeMember("Workbooks", BindingFlags.GetProperty, Nothing, objApp_Late, Nothing)
>>
>>    'Add a new workbook.
>>    objBook_Late = objBooks_Late.GetType().InvokeMember("Add", BindingFlags.InvokeMethod, Nothing, objBooks_Late, Nothing)
>>
>>    'Get the worksheets collection.
>>    objSheets_Late = objBook_Late.GetType().InvokeMember("Worksheets", BindingFlags.GetProperty, Nothing, objBook_Late, Nothing)
>>
>>    'Get the first worksheet.
>>    Parameters = New Object(0) {}
>>    Parameters(0) = 1
>>    objSheet_Late = objSheets_Late.GetType().InvokeMember("Item", BindingFlags.GetProperty, Nothing, objSheets_Late, Parameters)
>>
>>    'Get a range object that contains cell A1.
>>    Parameters = New Object(1) {}
>>    Parameters(0) = "A1"
>>    Parameters(1) = Missing.Value
>>    objRange_Late = objSheet_Late.GetType().InvokeMember("Range", BindingFlags.GetProperty, Nothing, objSheet_Late, Parameters)
>>
>>    'Write "Hello, World!" in cell A1.
>>    Parameters = New Object(0) {}
>>    Parameters(0) = "Hello, World!"
>>    objRange_Late.GetType().InvokeMember("Value", BindingFlags.SetProperty, Nothing, objRange_Late, Parameters)
>>
>>    'Return control of Excel to the user.
>>    Parameters = New [Object](0) {}
>>    Parameters(0) = True
>>    objApp_Late.GetType().InvokeMember("Visible", BindingFlags.SetProperty, Nothing, objApp_Late, Parameters)
>>    objApp_Late.GetType().InvokeMember("UserControl", BindingFlags.SetProperty, Nothing, objApp_Late, Parameters)
>>Catch theException As Exception
>>    Dim errorMessage As [String]
>>    errorMessage = "Error: "
>>    errorMessage = [String].Concat(errorMessage, theException.Message)
>>    errorMessage = [String].Concat(errorMessage, " Line: ")
>>    errorMessage = [String].Concat(errorMessage, theException.Source)
>>
>>    MessageBox.Show(errorMessage, "Error")
>>End Try
>>
>>
>>
>>>>Hi Rick,
>>>>
>>>>You have to set the Option Strict Off in VB.NET to get late binding. You can't tell from the code.
>>>
>>>Sorry - I'm ignorant on VB.NET <g> so I may be off, but I *think* this is how this works:
>>>
>>>I think with Option Strict off CreateObject() still does late binding, but the returned type is of type object, so you'd have to use Reflection to access any members. With Option Strict On you can just call the methods and the compiler will automatically do the Reflection calls.
>>>
>>>As far as I know CREATEOBJECT() always uses Late Binding - what would hte compiler bind against to do early binding? Unless you have done a TLBIMP (or using the Office Integration Kit). In that case you wouldn't use CreateObject() but the wrapper type...
>>>
>>>Let me know if this is wrong - it'd actually help to understand this, because there are a few things where this kind of interaction with COM objects would be much preferrable over TLBIMP or Reflection access.
>>>
>>>
>>>+++ Rick ---
>>>
>>>
>>>
>>>But isn't the problem that
>>>>
>>>>>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?
-----------------------------------------

Cathi Gero, CPA
Prenia Software & Consulting Services
Microsoft C# / .NET MVP
Mere Mortals for .NET MVP
cgero@prenia.com
www.prenia.com
Weblog: blogs.prenia.com/cathi
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform