Reading Microsoft Word Documents
Hi all,
I'm trying to read a word document for the purpose of obtaining a word count, I realise Word has built in functionality for presenting a word count but I want to write a little app that will omit certain parts of the document from the word count.
So far I have tried this code to open the document but I am getting an error 'Word.Document cannot be found' and 'Microsoft.Office.Interop cannot be found'. I have added a reference to the Microsoft Office 12.0 Object Library under the COM tab. I have Office 2007 installed and I'm using VB2005.
Code:
Imports Microsoft.Office.Interop
Dim appWord As New Microsoft.Office.Core.Application
Dim docWord As New Word.Document
docWord = appWord.Documents.Open("c:\test.doc")
Any ideas?
Re: Reading Microsoft Word Documents
Try this...
Code:
'~~> Set reference to Microsoft 12.0 Object Library
Private Sub cmdOpen_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdOpen.Click
Dim strFileName As String, strFilePath As String, strFileTitle As String
Dim word_server As Word.Application
Application.DoEvents()
strFileName = "c:\test.doc"
strFileTitle = strFileName.Substring(strFileName.LastIndexOf("\") + 1)
strFilePath = strFileName.Substring(0, strFileName.LastIndexOf("\"))
word_server = New Word.Application()
word_server.ChangeFileOpenDirectory (strFilePath)
word_server.Documents.Open(FileName:=strFileTitle, _
ConfirmConversions:=False, ReadOnly:=False, _
AddToRecentFiles:=False, PasswordDocument:="", _
PasswordTemplate:="", Revert:=False, _
WritePasswordDocument:="", WritePasswordTemplate:="", _
Format:=Word.WdOpenFormat.wdOpenFormatAuto)
word_server.Visible = True
'~~> What ever you want to do here...
End Sub
Re: Reading Microsoft Word Documents
Hi koolsid, thanks for the quick reply...
Unfortunately I am still getting the same error (Word.Application is not defined) on the line:
Code:
word_server = New Word.Application()
I have selected the reference to MS Office 12 Object Library, how can I verify this is attached to my project?
Re: Reading Microsoft Word Documents
Quote:
I have selected the reference to MS Office 12 Object Library, how can I verify this is attached to my project?
The moment you type
word_server = New Word.
Intellisense should give you Application()
or when you type
Dim word_server As
Intellisense should give you Word.Application
Edit: How are you selecting the reference?
Re: Reading Microsoft Word Documents
Quote:
I have selected the reference to MS Office 12 Object Library, how can I verify this is attached to my project?
Are you sure you have the correct PIA's (Primary Interop Assemblies)installed on your system?
PIA Download: http://www.microsoft.com/downloads/d...displaylang=en
^^For office '07
Re: Reading Microsoft Word Documents
Quote:
Originally Posted by
BackWoodsCoder
I installed this but nothing has changed - even after a reboot. I'm not sure if they installed correctly though because there was no 'finished' or 'success' message, after running the setup the progress bar advances then the window disappears. Could be a problem with Vista?
Quote:
Originally Posted by koolsid
How are you selecting the reference?
All I do is go to Project>Add Reference>COM>Select Microsoft Office 12.0 Object Library>OK
Re: Reading Microsoft Word Documents
What about the intellisense question in post 4?
Re: Reading Microsoft Word Documents
Hi mate, There's no item in the intellisense list called 'Word'. VS underlines Word.Application with the error 'Word.Application is not defined'.
Re: Reading Microsoft Word Documents
Just a stupid doubt...
How many office versions are installed in your pc? What I mean is do you have 2003 and 2007 together?
If no then can you upload your project?
Re: Reading Microsoft Word Documents
Good point, I have only Office 2007, but I do have MS Office Powerpoint Viewer 2003. Could that be causing the problem?
Re: Reading Microsoft Word Documents
Quote:
All I do is go to Project>Add Reference>COM>Select Microsoft Office 12.0 Object Library>OK
Quote:
Originally Posted by
the182guy
Good point, I have only Office 2007, but I do have MS Office Powerpoint Viewer 2003. Could that be causing the problem?
Ok post a snapshot of the screen when you are adding references....