|
-
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.
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
|