Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written a program that, amongst other things, checks if a Word document exists and if it does, opens it or just continues processing if it is already open.

However, as soon as I start the program, an instance of Word is opened, I can see this in Task Manager, and it's not as if this instance is used when I open my document later in the program. Therefore I can end up with half-a-dozen Word processes running at the same time ... when I finish with my document & close it, the rest of these Word instances just happily continue running !!!

Can somebody tell me what causes this and how I can stop it from happening, is there any code I need to add here, I'm not sure anything even executes that I could show you !!!
Posted
Comments
Kornfeld Eliyahu Peter 10-Jan-16 12:48pm    
You should use your debugger to find the exact place where Word loaded...Show that code here, and maybe someone will have an idea to help you...
Gary Heath 10-Jan-16 13:05pm    
I put a break on the very first line, before it's even done anything (that I have written) "Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load" ... and the Word instance has started !!!
Kornfeld Eliyahu Peter 10-Jan-16 13:26pm    
What project type you have?
Gary Heath 10-Jan-16 13:36pm    
It's a Windows Forms project originally written under VS2010 but now I am using VS2015 ... I am using Office 2007 and this is a recently (this week) rebuilt Windows 7 x64 Home Professional machine.
Gary Heath 10-Jan-16 13:07pm    
These are my Imports and variable definitions ...

Imports System.IO
Imports System.Text.RegularExpressions
Imports System.Xml
'Imports Microsoft.Office.Core
Imports Microsoft.Office.Interop
'Imports Microsoft.Office.Tools.Word
Imports System.Windows.Forms.VisualStyles.VisualStyleElement
Imports System.Windows.Forms.Form
Imports System.Runtime.InteropServices
Imports System.IO.Compression
Imports System.Reflection
Imports Ionic.Zip 'Used to UnZip files, see http://dotnetzip.codeplex.com/
Imports Microsoft.VisualBasic.FileSystem
Imports System.IO.StreamWriter
Imports System.IO.TextWriter
Imports Outlook = Microsoft.Office.Interop.Outlook

Public Class Form1

Dim myProgramAborted As Boolean = False
Dim myFolder As String
Dim myTempFolder As String
Dim myDataFolder As String
Dim myNlFolder As String
Dim myTemplate As String
Dim myNewsLetter As String
Dim myTables As String
Dim myLeftCB As String
Dim myRARfile As String
Dim myLeague As String
Dim mySession As String
Dim myLastSession As String
Dim mySessionMinusOne As String
Dim mySessionString1 As String
Dim mySessionString2 As String
Dim myArray(,) As String
Dim myItem As String
Dim myCount As Int16 = 0
Dim myMSWord As New Word.Application
Dim myMSDoc As New Word.Document
Dim myArrayLen As Int16
Dim myWordData As String
Dim myWordHeading As String
Dim myWordLine As String
Dim myBlankLine As Boolean
Dim myCupRound As String
Dim myCupMatches As Int16 = 0
Dim myCupDraw As Int16 = 0
Dim myLeagueCupRound As String
Dim myLeagueCupMatches As Int16 = 0
Dim myLeagueCupDraw As Int16 = 0
Dim myTrophyRound As String
Dim myTrophyMatches As Int16 = 0
Dim myTrophyDraw As Int16 = 0
Dim myChampsTrophyRound As String
Dim myChampsTrophyMatches As Int16 = 0
Dim myChampsTrophyDraw As Int16 = 0
Dim myIntlRound As String
Dim myIntlMatches As Int16 = 0
Dim myCupCount As Int16 = 0
Dim myZipFolder As String
Dim myWriterTxt As String
Dim myAsciiTxt As String
Dim myNewResults As String
Dim myAucResTxt As String
Dim myNewsTabTxt As String
Dim myNewTable As String
Dim myNewTableTemp As String
Dim myStatfileTxt As String
Dim myTestRtf As String
Dim myTranstxt As String
Dim myScorers As String
Dim myInTxt As String
Dim myLineWanted As Boolean
Dim myOutLine As String = ""
Dim myOutFile As StreamWriter
Dim myNickNameFile As String
Dim myNotePadNum As Int16 = 0
Dim myNotePadFiles(15) As String
Dim myId As Int16
Dim myIdFile As String
Dim myErrorMsg As String
Dim myMessageBoxStyle As String
Dim myDiv As Int16 = 0
Dim myLine As Int16 = 0
Dim myFixturesTxt As String
Dim myAllTeamsTxt As String
Dim myEuroSpielTxt As String
Dim myOLrecipient As String
Dim myOLsubject As String
Dim myOLbody As String
Dim myLastNewsletter As String
Dim myAllTeamSess As String

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

'Define file paths

myTempFolder = "K:\Newsletters\Newsletter Templates (Email)\Ribbon\"
myDataFolder = "K:\Newsletters\Application Files\"
myNlFolder = "K:\Newsletters\"

End Sub

Based on your description, few things to check:

- Instead of setting a breakpoint and running the application, use Debug / Step into to go to each line that is executed in the beginning. This way you see the execution of all the code in the beginning

- open the project properties and from the debug sheet check that you haven't defined to start word application when the debugging starts.
 
Share this answer
 
Comments
Gary Heath 10-Jan-16 13:23pm    
When I use "Debug / Step into" with Task Manager running alongside, the WINWORD.EXE *32 instance opens almost immediately.

Opening Project Properties & looking at the DEBUG tab it says "Configuration : Active (Debug) & Platform : Active (x86) ... everything else is blanked out and the only things that are "chosen" are Start Project and Enable the Visual Studio hosting process.
Wendelius 10-Jan-16 13:33pm    
You seem to define a variable for Word interop and instantiate it immediately. Have a look at line

Dim myMSWord As New Word.Application

If you take out New keyword, the creation should not happen anymore. The same applies to the document.
Gary Heath 10-Jan-16 13:37pm    
I am just going to eat, thank you, I am no expert in this so your advice is invaluable, I will look, test and report back later :-) ... thanks Mika ...

Gary Heath 10-Jan-16 16:56pm    
Mika, that worked, thank you, so the "New" in that line actually executes an instance, even though it's on a Dim statement !!! I really should try to learn this stuff in a bit more depth but at 57 with 4 kids in the house is ain't easy !!!

OK, it now fails with "An unhandled exception of type 'System.NullReferenceException' occurred in KA_Newsletter.exe
" ... I am assuming I have to start a NEW occurrence when I get to the following code, but I've had a few beers so will look in the morning ... in the meantime, thank you & this is where it falls over in case you feel like saving me some time :-) !!!!


'Open Word Newsletter, or switch to it if it's already open

Dim myFileOpen As Boolean

myFileOpen = IsFileOpen(myNewsLetter)

If myFileOpen = False Then
myMSDoc = myMSWord.Documents.Open(myNewsLetter) <<< Fails here
myMSWord.WindowState = Word.WdWindowState.wdWindowStateNormal
myMSWord.Visible = True
'myMSWord.ActiveDocument.Bookmarks("\StartOfDoc").Select()
End If
Wendelius 10-Jan-16 23:18pm    
Glad to hear it heled.

Your current problem is that since the Word instance isn't created in the beginning anymore, you need to create it in the code before using. Consider the following.

If myFileOpen = False Then
set myMSWord = New Word.Application << create the instance
myMSDoc = myMSWord.Documents.Open(myNewsLetter) <<< Fails here
myMSWord.WindowState = Word.WdWindowState.wdWindowStateNormal
This ...

You seem to define a variable for Word interop and instantiate it immediately. Have a look at line

VB
Dim myMSWord As New Word.Application


If you take out New keyword, the creation should not happen anymore. The same applies to the document.

Plus this ...

since the Word instance isn't created in the beginning anymore, you need to create it in the code before using. Consider the following.

VB
myMSWord = New Word.Application


... has resolved my problem, many thanks to Mika Wendelius for his help :-)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900