Vb and MS Word

I

Immy

Hi There

I seem to be having some problems with my coding and am looking for some help.

I am designing an application in VB 2005 use for editing spelling and
grammar errors in MS Word documents. The application would check and edits,
as well as proof read MS Word documents.

The following is a snippet of the code I am working on:

1 Dim wordapp As New Word.Application
2 Dim doc, temp, temp1 As Word.Document\3
3
4
5
6 'Determinig whether Word is running or not
7 While wordapp.Visible = True
8 doc = wordapp.Templates.Application.ActiveDocument
9
10 wordapp.ActiveDocument.Content.Text = RichTextBox2.Text
11 'doc = wordapp.ActiveDocument
12 'wordapp.ActiveDocument.Content.Text = RichTextBox2.Text
13 ToolStripStatusLabel2.Text = doc.Words.Count
14
15 End While
16
17 If wordapp.Visible = False Then
18 wordapp.Visible = True
19 If wordapp.Visible = True Then
20 temp = wordapp.ActiveDocument
21 End If
22 temp = wordapp.Documents.Add()
23 temp = wordapp.ActiveDocument
24 temp.Content.Text = RichTextBox2.Text
25
26 'doc = wordapp.ActiveWindow
27 'wordapp.Selection.TypeText(RichTextBox2.Text)
28 'wordapp.ActiveDocument.Content.Text = RichTextBox2.Text
29 ToolStripStatusLabel2.Text = doc.Words.Count
30
31 ElseIf wordapp.Visible = True Then
32 temp = wordapp.Documents.Add()
33 doc = wordapp.ActiveDocument
34 'temp = doc
35 'doc.Content.Text = RichTextBox2.Text
36 'doc = wordapp.ActiveDocument 'set doc to activedocument
37 wordapp.Selection.TypeText(RichTextBox2.Text)
38 'ActiveDocument.Content.Text = RichTextBox2.Text
39 ToolStripStatusLabel2.Text = doc.Words.Count
40 End If

The problem appear in line 17 -24 where I am checking to see whether a MS
Word document is open or not. The doccument could be one currently used by
the user or one that have been saved or just a new document.

My question is as follows:
• In line 17 – 21 of my code, I am attempting to see whether or not the MS
Word application is running or not. If the application is runninng and a file
is use, I want that file assign to ActivateDocument.

• On my form there are two rich text boxes: one is for displaying the
paragraph containing the spelling and writing errors. The second, displays
the whole document. My problem is whenever I enter text into the text box,
instead of that typed text into the activated document it appears in a new MS
Word Document.

I would be grateful if anyone can advice me how to overcome this problem.

Thanks,

Immy
 
 
J

Jezebel

Before you go much further with this, may I tactfully suggest you read a
recent text on VB. The code you're posted looks like it was learnt on the
fly, a long time ago. It is horribly bug-ridden, and seems to rest on a
number of wrong assumptions about how VB and Word work. Eg
2 Dim doc, temp, temp1 As Word.Document

You might think you have declared three variables of type Word.document; but
only temp1 is a word.document -- the others are variants.

Since you declare wordapp as 'new', then word will always be running when
you test it. That has nothing to do with its visibility, or whether there
happens to be an active document. To test whether Word is running --

Dim WordApp as Word.Application

on error resume next
Set WordApp = Word.Application
on error goto 0

If WordApp is nothing then
... Word was not running
set WordApp = new Word.Application

else
... Word was running
set doc = WordApp.ActiveDocument
end if


Your textbox text appears in a new document because you put it there --
32 temp = wordapp.Documents.Add()
33 doc = wordapp.ActiveDocument

line 32 creates a new document. The newly-created document becomes the
active document. Line 33 does nothing very much: since doc is actually a
variant, and you assign it to the activedocument without using 'set', you're
actually assigning the content of the document (which is nothing, since it's
a new document). You fill the new document with the content of your textbox
at
 
A

aalaan

Sorry about all this bad news, but another issue is you *can't* write a
program to proof read. That is always a manual process that consists of
checking the final camera ready copy word-for-word against the latest raw
text. You can only really do this well by reading backwards one person to
another.
 
I

Immy

Hi Jezebel

Thanks for answering my post.

I tried your code on my application however it seems that my vb application
cannot detect an active Word document even though one has been opened.

I have adapted your suggestions and modified my code based on those
suggestions. The code is now as follows:

Dim wordapp As Word.Application
Dim doc As Word.Document



'open the document


'Determinig whether Word is running or not
On Error Resume Next
wordapp = New Word.Application
On Error GoTo 0

If wordapp Is Nothing Then
wordapp = CreateObject("Word.Application")
Else
'word is running
doc = wordapp.ActiveDocument
doc.Content.Text = RichTextBox2.Text
End If

I have assigned ‘doc’ to wordapp.ActiveDocument as you suggested. However,
when I run the code an error message appears that says “no Word document
open,†which in fact one is open.

I am using the 2005 Express Edition of Visual Basic and running MS Office
2007. I have tried using the set keyword for setting wordapp to
Word.Application. I don’t think this is required any more in the new version
of Visual Basic.
I would be please to hear your ideas on solving this problem.
 
J

Jezebel

More that you need to relearn about VBA: if you're assigning an object to a
variable, you need to use the 'set' keyword --

set doc = wordapp.ActiveDocument
 

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