|
-
Jan 12th, 2011, 04:14 AM
#1
Thread Starter
New Member
Can't stop MS Word opening when it's referenced
Hey guys,
Well I decided it was about time I learned about accessing Microsoft Office products via VB.NET. I started with Excel and it all worked fine, I could create a new Excel Application, open a workbook from file and read from the sheet then close it all down again no worries. However, when I turned to Word...things got strange. Using the code provided below, every time I run it an instance of Microsoft Word opens itself on the desktop (with no loaded content). I didn't have this problem when using Excel with similar code, so I have to ask..what gives? Is there a way I can stop MS word opening at all? At the moment it just flickers for a second (it opens when I open the document, then when I call the Quit method it shuts down a split second later)
Here's my code anyway (all relevant parts)
vb Code:
Dim newWordApp As New Word.Application With {.Visible = False, .ShowWindowsInTaskbar = False}
Dim firstDocument As Word.Document = newWordApp.Documents.Open("C:\Users\Peter\Documents\Document1.docx")
'create a new paragraph and add some text to it, then add another paragraph after it.
Dim newPara As Word.Paragraph = firstDocument.Paragraphs.Add()
newPara.Range.Text = "This was a MS Word Test"
newPara.Range.InsertParagraphAfter()
'start closing the document
newWord.Documents.Save()
newWord.Documents.Close(Word.WdSaveOptions.wdDoNotSaveChanges, Word.WdOriginalFormat.wdOriginalDocumentFormat)
newWord.Quit()
'I was told to do this after finishing with a COM object
System.Runtime.InteropServices.Marshal.ReleaseComObject(newWord)
newWord = Nothing
So yeah, any insights would be appreciated. I get the feeling I'm missing a very simple property I can toggle, but I sure can't find it and Google isn't being too helpful as I can't really think of a good search query.
Thanks,
Methodical.
-
Jan 12th, 2011, 06:30 AM
#2
Thread Starter
New Member
Re: Can't stop MS Word opening when it's referenced
For some reason it's solved now, just randomly stopped opening them. Oh well.
-
Jan 12th, 2011, 06:44 AM
#3
Re: Can't stop MS Word opening when it's referenced
You are asking it to create a New Word.Application, which is as the name suggests creating a new version of Word. If you check your task manager you will see that it does the same thing for Excel.
I have read that you can just make it invisible, but it has side effects. Make sure you change the alerts setting as well during automation.
Code:
(Note I can only try this for Excel atm, but should be similar!)
newWord.Visible = False
newWord.DisplayAlerts = False
PS: I just googled "hide word.application" .
-
Jan 12th, 2011, 07:58 AM
#4
Thread Starter
New Member
Re: Can't stop MS Word opening when it's referenced
I know it opens a new instance in the taskmanager under "open processes", but for Excel it didn't actually open the window. It's no longer doing it for Word either so I'll mark this resolved though I have no idea what changed haha.
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
|