COMException was unhandled

Johnson

Well-known member
Joined
Mar 6, 2009
Messages
158
Programming Experience
Beginner
can any one spot the misstake? The bookmark exists in my template

VB.NET:
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim oWord As Word.Application
        Dim oDoc As New Word.Document

        'Start Word and open the document template.
        oWord = CreateObject("Word.Application")
        oWord.Documents.Add("C:\Documents and Settings\Tim\My Documents\Work\Templates\TimeSheet.docx")
        oDoc.Bookmarks.Item("MondayDate").Range.Text = "17/01/2010"
        oWord.Visible = True
    End Sub
 
VB.NET:
Dim app As New Word.Application
Dim doc As Word.Document = oWord.Documents.Add(...)
 
forgive my stupidnes john, i dont understand what you're doing in the changes of varibles? can you please explain more.

thanks
 
Using the New constructor for Word.Application; I like strongly typed code better. CreateObject is common probably because that's what you had to do when scripting ActiveX objects. Use of CreateObject is also related to lack of exception handling in earlier environments. Finally CreateObject has remote server capabilities that is not needed here.

Add is a function that returns the result of adding the new Document (here based on template) to the Documents collection. The problem with your code was that you created a new Document instance that was not attached and carried no template or any other content, ignoring the Document you added from template.
 
Using the New constructor for Word.Application; I like strongly typed code better. CreateObject is common probably because that's what you had to do when scripting ActiveX objects. Use of CreateObject is also related to lack of exception handling in earlier environments. Finally CreateObject has remote server capabilities that is not needed here.

Add is a function that returns the result of adding the new Document (here based on template) to the Documents collection. The problem with your code was that you created a new Document instance that was not attached and carried no template or any other content, ignoring the Document you added from template.


Evening john

I got the way i done it from m$ How to automate Word from Visual Basic .NET to create a new document so strange it didnt work.

"The problem with your code was that you created a new Document instance that was not attached and carried no template or any other content, ignoring the Document you added from template."


Really? strange, because if i just removed oDoc.Bookmarks.Item("MondayDate").Range.Text = "17/01/2010"

the template opens fine with all the info, thats why im stuffed it not working
 
Johnson said:
I got the way i done it from m$ How to automate Word from Visual Basic .NET to create a new document so strange it didnt work.
Johnson said:
Really? strange, because if i just removed oDoc.Bookmarks.Item("MondayDate").Range.Text = "17/01/2010"

the template opens fine with all the info, thats why im stuffed it not working
Those are different things, are you are misunderstanding that information.

In the first sample there is this code:
VB.NET:
oDoc = oWord.Documents.Add
To me it is quite clear that the oDoc variable needs to point to the Document instance you want to automate.

Later in the "Use a template" they explain where you can specify the template:
VB.NET:
oWord.Documents.Add "<Path to your template>\MyTemplate.dot"
If you want to automate that Document object you either have to connect to it right from the Add function call, or look it up in the Documents collection later (why would you do that? and you certainly didn't attempt that)

About the template based document opening, yes of course it does, if your Word application is visible any app.Documents.Add call will add a document and you can see it. That doesn't change the fact that to automate a Document object you need a reference to it, there is no magic connection that enables any Document instance to beam its intentions to any document you think of. You do need to reference any object you want to modify directly.
 
Later in the "Use a template" they explain where you can specify the template:
VB.NET:
oWord.Documents.Add "<Path to your template>\MyTemplate.dot"
If you want to automate that Document object you either have to connect to it right from the Add function call, or look it up in the Documents collection later (why would you do that? and you certainly didn't attempt that)


Ok yes sorry i did miss understand that information, and i can see what m$ was saying., Il try not make my self look even more silly.

VB.NET:
Dim oDoc As New Word.Document = oWord.Documents.Add "TimeSheetTemplateHere"

that would connect to the template? allowing me to use

VB.NET:
 oWord.Bookmarks.Item("MondayDate").Range.Text = "17/01/2010"

But i get end of statement expected "oWord.Documents.Add "TimeSheetTemplateHere""
 
But i get end of statement expected "oWord.Documents.Add "TimeSheetTemplateHere""
In VB.Net methods are usually called like this: method() The paranthesis denote the parameter list, which may be empty, and in which case the parathesis are optional (though I recommend you leave them, makes code easier to read). In your case the method call contains one string parameter, and the parameter list paranthesis are not optional.

Johnson said:
oWord.Bookmarks.Item("MondayDate").Range.Text = "17/01/2010"
And now you are getting silly, right? oWord, which in your other code points to the Word.Application object, has no Bookmarks property, the Word.Document does.
 
Back
Top