automating word save as different file from template
Here is the code causing the error.
An unhandled exception of type 'System.NullReferenceException' occurred in ClientApp.exe
Code:
Private Sub btnCreateWordDoc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateWordDoc.Click
Dim WordApp As Word.Application
WordApp = New Word.Application
WordApp.Visible = True
Dim myDoc As Word.Document
WordApp.Documents.Add("C:\.netprojects\remoting\ClientApp\template.doc")
myDoc.Bookmarks.Item("bookmark").Range.Text = "test1"
WordApp.Quit(True)
End Sub
?????
Re: System.nullreferenceexception error automating word
Can you tell which line is throwing the exception? You should be able to look at each object and see it's value by highlighting it and pressing Shift+F9. Look for the one that is Nothing, and you will have found your problem.
I haven't used Word, but just from looking at your code, I notice that myDoc is declared as a Word.Document, but then you use myDoc without setting it to any value, so I would expect that it is Nothing at the time this line:
myDoc.Bookmarks.Item("bookmark").Range.Text = "test1"
runs, which would be the problem.
Re: System.nullreferenceexception error automating word
It is indeed the line you expect. myDoc on that line show Nothing. What do I need to do?
Re: System.nullreferenceexception error automating word
Generally, this would work:
Dim myDoc As New Word.Document
However, you may have to do something more than that, because it sure looks like you then add a specific file to a collection in the very next line. I just looked back into a 2003 program I have that created a word file, and I also used this line:
WordApp.Documents.Add()
Though the arguments to Add were a bit different (it's probably overloaded). However, Add aparently returns a document, because converting what I have into what you are doing, I would change your code to this:
vb Code:
Private Sub btnCreateWordDoc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateWordDoc.Click
Dim WordApp As Word.Application
WordApp = New Word.Application
WordApp.Visible = True
Dim myDoc As Word.Document
mydoc = WordApp.Documents.Add("C:\.netprojects\remoting\ClientApp\template.doc")
myDoc.Bookmarks.Item("bookmark").Range.Text = "test1"
WordApp.Quit(True)
End Sub
So give that a try. As long as the Add method is still returning a document in the overloaded form that you are using, as I expect it will, this will set the myDoc variable to an actual instance.
Re: System.nullreferenceexception error automating word
You are correct Shaggy. The .Add function does indeed return a Document object. So accessing the bokmarks collection of a un-set or null document object wil give that error. It should work just fine with Shaggy's c0d :thumb:
Re: System.nullreferenceexception error automating word
Works great. Only now I have more questions. So I will search for answers, if I cannot find them I know where to come for help.
Thanks RobDogg and ShaggyHiker!
Re: [RESOLVED] System.nullreferenceexception error automating word
I want to use the template, fill the bookmarks and then save the file as a different name. Can I do this? Without asking the user to save the file?
Re: automating word save as different file from template
[color=navy]Yes, when opening, specify the dot file and new document argument. Then the new document will be created based upon the template.
to get it to autosave is an issue. Why no just let the user do it?/coor]
Re: automating word save as different file from template
Quote:
Originally Posted by RobDog888
[color=navy]Yes, when opening, specify the dot file and new document argument. Then the new document will be created based upon the template. ]
Thanks. I will look at doing that.
Quote:
Originally Posted by RobDog888
to get it to autosave is an issue. Why no just let the user do it?/coor
This document will be an invoice generated from the system and placed in an invoice folder. The user will then be emailing it out. I think we will changing this to an excel spreadsheet instead. Trying to make this as simple as possible.
Thanks Again.