|
-
Jul 2nd, 2007, 07:15 PM
#1
Thread Starter
Fanatic Member
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
?????
Last edited by mojo69; Jul 3rd, 2007 at 05:31 PM.
-
Jul 2nd, 2007, 07:47 PM
#2
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.
My usual boring signature: Nothing
 
-
Jul 2nd, 2007, 08:16 PM
#3
Thread Starter
Fanatic Member
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?
-
Jul 2nd, 2007, 08:27 PM
#4
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.
My usual boring signature: Nothing
 
-
Jul 3rd, 2007, 12:25 AM
#5
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
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Jul 3rd, 2007, 08:08 AM
#6
Thread Starter
Fanatic Member
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!
-
Jul 3rd, 2007, 05:30 PM
#7
Thread Starter
Fanatic Member
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?
-
Jul 3rd, 2007, 05:40 PM
#8
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]
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Jul 6th, 2007, 07:16 AM
#9
Thread Starter
Fanatic Member
Re: automating word save as different file from template
 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.
 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|