ActiveDocument object doesn't work

M

Mel_la_Bel

Hello all,

I have a problem with VB.Net.
I want to get the readabilityStatistics from word documents. herefore I have
to activate a doc as an ActiveDocument in the Word.Application. And here is
exactly the problem. I cannot use the ActiveDocument as it is written
everywhere in the MSDN!
Here is what I did:
....
Private Sub TestMSWordFunc(ByVal file As String)
'MS Word declarations
Dim wordApp_Server As Word.Application
Dim wordDoc As Word.Document
Dim wordRange As Word.Range
Dim wordStatistic As Word.ReadabilityStatistic
'Other
Dim fileMod As Boolean
Dim resComputeStatistics As Integer
Dim wordEnum As IEnumerator
Dim wordVal As ValueType

'Open an application server first
wordApp_Server = New Word.Application
wordDoc = wordApp_Server.Documents.Open( _
FileName:=file, _
ConfirmConversions:=False, _
ReadOnly:=False, _
AddToRecentFiles:=False, _
PasswordDocument:="", _
PasswordTemplate:="", _
Revert:=False, _
WritePasswordDocument:="", _
WritePasswordTemplate:="", _
Format:=Word.WdOpenFormat.wdOpenFormatAuto, _
Visible:=False)

'Document must be set active in order to retrieve further information
wordApp_Server.Documents.Add(file)
wordDoc = wordApp_Server.ActiveDocument


Label_ResultFile.Text = file
Label_ResultWords.Text =
wordDoc.ComputeStatistics(Word.WdStatistic.wdStatisticWords)
Label_ResultParagraphs.Text =
wordDoc.ComputeStatistics(Word.WdStatistic.wdStatisticParagraphs)

Label_ResultFK.Text =
wordApp_Server.ActiveDocument.Content.ReadabilityStatistics.Item(6).Value
....

I cannot get to tell the application the open word document!! The last line
always returns zero, as the ActiveDocument is empty,i.e. a different document
than the one I opened!

Can anybody help me?
I am getting crazy. It is probably very easy, but I cannot see the mistake.

Kisses
Mel
 
M

Martin Seelhofer

Hi Mel

Well, I don't see why you reassign your object variable to a
newly created blank document using the following lines...:
wordApp_Server.Documents.Add(file)
--> creates a new blank document and makes it the ActiveDocument
wordDoc = wordApp_Server.ActiveDocument
--> assigns the newly created blank document to the variable
wordDoc
--> the reference to the previously opened doc is lost

.... after having assigned it (correctly) to the Document object returned
by calling the following Open-Method:
wordDoc = wordApp_Server.Documents.Open( _
FileName:=file, _
[...]
Visible:=False)

The way you do it, the reference to the open document is overwritten
with another one to a blank new document. Therefore, remove the
mentioned lines and try again...


Cheers,
Martin
 
M

Mel_la_Bel

Hi Martin,

thanks for answering.
I already thought of that.

I don't understand how to assign the opened file (i.e. the file the user
selected from OpenFileDialog) with Document.Open() to the Application as the
ActiveDocument?! I can only do the ReadabilityStatistics when I have an
active Document!! But the keyword "ActiveDocument" as referd to in all
tutorials I found, is not accepted by the parser.
I modified it this way:
---
#Region "TestMSWordFunc"
Private Sub TestMSWordFunc(ByVal file As String)
'MS Word declarations
Dim wordApp_Server As Word.Application
Dim wordDoc As Word.Document
Dim wordRange As Word.Range
Dim wordStatistic As Word.ReadabilityStatistic
'Other
Dim fileMod As Boolean
Dim resComputeStatistics As Integer
Dim wordEnum As IEnumerator
Dim wordVal As ValueType
Dim activeFile As String

'Open an application server first
wordApp_Server = New Word.Application
wordDoc = wordApp_Server.Documents.Open( _
FileName:=file, _
ConfirmConversions:=False, _
ReadOnly:=True, _
AddToRecentFiles:=True, _
PasswordDocument:="", _
PasswordTemplate:="", _
Revert:=False, _
WritePasswordDocument:="", _
WritePasswordTemplate:="", _
Format:=Word.WdOpenFormat.wdOpenFormatAuto, _
Visible:=False)

'Document must be set active in order to retrieve further information
Try
activeFile = wordApp_Server.ActiveDocument.Name()
Catch
If activeFile = Nothing Then
MsgBox("There is no active file within the application.")
End If
End Try

Label_ResultFile.Text = file
Label_ResultWords.Text =
wordDoc.ComputeStatistics(Word.WdStatistic.wdStatisticWords)
Label_ResultParagraphs.Text =
wordDoc.ComputeStatistics(Word.WdStatistic.wdStatisticParagraphs)
Label_ResultFK.Text =
wordApp_Server.ActiveDocument.Content.ReadabilityStatistics.Item(6).Value
---

The last line will cause the application to crash, because it says that the
method I want to use cannot be invoked as there is no active document window!!
I will create one, invisible window...let' see :eek:)

Kisses
Mel
 
M

Martin Seelhofer

Hi Mel
I don't understand how to assign the opened file (i.e. the file the user
selected from OpenFileDialog) with Document.Open() to the Application as
the
ActiveDocument?! I can only do the ReadabilityStatistics when I have an
active Document!! But the keyword "ActiveDocument" as referd to in all
tutorials I found, is not accepted by the parser.

Well, the trick is that you don't really need an "ActiveDocument", since
methods like ReadabilityStatistics work with ANY object of type Document.
ActiveDocument is simply a global Property of the Application-object
pointing to the currently open (and visible) document. Since your are
opening your document invisibly, there *is* no ActiveDocument.
In your case, you already have a reference to the open document object
in variable wordDoc. So why don't you just use that one? ;-)

[...]
activeFile = wordDoc.Name()
[...]
Label_ResultFK.Text = wordDoc.ReadabilityStatistics.Item(6).Value

By the way, the above line might be written slightly shorter:

Label_ResultFK.Text = wordDoc.ReadabilityStatistics(6).Value


Cheers,
Martin
 
M

Mel_la_Bel

Hi Martin,

I got it working!! :)
You definitely need the active document to work with it and do, e.g. the
readability statistics, for Flesch-Kincaid Grade Level.
In addition visible is not the same as active. These are two different
properties, otherwise there would only be one.

Then I have to call checkGrammar() to get the statistics I want!
Unfortunately, the Grammar Checker appears. I don't want it to popup...That's
boring, you solve one problem and, peng, you get 5 new problems.

Thank you again for helping me!

Ciao and
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top