Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / December 2006

Tip: Looking for answers? Try searching our database.

Vb and MS Word

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Immy - 04 Dec 2006 02:00 GMT
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

Jezebel - 04 Dec 2006 04:43 GMT
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

> Hi There
>
[quoted text clipped - 71 lines]
>
> Immy
aalaan - 04 Dec 2006 05:40 GMT
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.

> 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
[quoted text clipped - 116 lines]
>>
>> Immy
Immy - 05 Dec 2006 03:53 GMT
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.

> 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
[quoted text clipped - 112 lines]
> >
> > Immy
Jezebel - 05 Dec 2006 04:20 GMT
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

> Hi Jezebel
>
[quoted text clipped - 165 lines]
>> >
>> > Immy
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.