Operating Word from VB5 
Author Message
 Operating Word from VB5

I'm fairly new at this game, but have written a fair sized program - a
noise estimation program for pumps.  Having worked out all the
calculations I have a set of end variables that I would like to present
on a word document.  The progress of events I see as being something like
this:

1.      Open Word.
2.      Open a pre-written read-only doc in word with fields in it.
3.      VB program puts each variable into the corresponding field in the
        doc.
4.      Print the doc.
5.      Close word.

Dead easy I'm sure.  I'm not expecting anyone to write the code for me
(though some bigs hints would be cool) but I'd be grateful if someone
could point me in the right direction.
Martina



Sat, 01 Apr 2000 03:00:00 GMT  
 Operating Word from VB5

The following is copied from one of my programs. It is daily use, so it works.
Better yet, I wrote it for Word 7 and the customer switched to Word 8 (Office
97) and it still works.

There are some things in here that realte speciufically to my program, but it
can serve as a guide...

Public Sub ExistingScript()
    Dim sMsg As String
    OpenScript
    ' OpenScript calls a common dialog file open
    ' and gets the script name
    If frmStart.CmDlg1.filename = "" Then GoTo Normal_Exit
    ' progress indicator
    sMsg = "Opening Word file "
    sMsg = sMsg & vbCrLf & frmStart.CmDlg1.FileTitle
    sMsg = sMsg & vbCrLf & "Please wait..."
    frmStart.lblscript.Caption = sMsg
    ' now uses word basic
    Set objWProc = CreateObject("Word.Basic")
    ' if Word is not open, this opens it. If it is open,
    ' the doc is opened in the existing instance
    DoEvents
    ' sPath is a module-wide global -- you can
    ' set the path any way you want
    objWProc.ChDefaultDir sPath, 0
    objWProc.FileOpen (frmStart.CmDlg1.FileTitle)
    ' maximize Word
    objWProc.AppMaximize
    frmStart.lblscript.Caption = ""
    ' minimize this app
    frmStart.WindowState = 1
Normal_Exit:
    Exit Sub
Error_Handler:
    ' ErrorLog is a procedure that I use to log all errors
    ' to a text file. You can leave this out....
    ErrorLog "ExistingScript " & Error
    Resume Next
End Sub

  >I'm fairly new at this game, but have written a fair sized program - a
  >noise estimation program for pumps.  Having worked out all the
  >calculations I have a set of end variables that I would like to present
  >on a word document.  The progress of events I see as being something like
  >this:
  >
  >1.        Open Word.
  >2.        Open a pre-written read-only doc in word with fields in it.
  >3.        VB program puts each variable into the corresponding field in the
  >  doc.
  >4.        Print the doc.
  >5.        Close word.
  >
  >Dead easy I'm sure.  I'm not expecting anyone to write the code for me
  >(though some bigs hints would be cool) but I'd be grateful if someone
  >could point me in the right direction.
  >Martina



Mon, 03 Apr 2000 03:00:00 GMT  
 Operating Word from VB5

Very heartening!
Just as I was about to upgrade to Word8 and do code mods it looks as
if this is not needed.
I thought (from what I have read) that Word8 required
        set objWproc = CreateOject("Word.Application")

It appears that the 'old' Word.Basic still works.
My app is simple - With Wordobject do the following
        .fileopen
        .filesaveas
        .fileclose
        set wordobject=nothing

Thanks John for this.

Anyone else using Word.Basic with Word8?? Any known problems??
Any likely problems??
Further input greatly appreciated.



Quote:
>The following is copied from one of my programs. It is daily use, so it works.
>Better yet, I wrote it for Word 7 and the customer switched to Word 8 (Office
>97) and it still works.

>    ' now uses word basic
>    Set objWProc = CreateObject("Word.Basic")
>    ' if Word is not open, this opens it. If it is open,
>    ' the doc is opened in the existing instance
>    DoEvents
>    objWProc.FileOpen (frmStart.CmDlg1.FileTitle)
>    ' maximize Word
>    objWProc.AppMaximize
>    frmStart.lblscript.Caption = ""
>    ' minimize this app
>    frmStart.WindowState = 1
>Normal_Exit:
>    Exit Sub

--
frank bainbridge
eds ltd


Tue, 04 Apr 2000 03:00:00 GMT  
 Operating Word from VB5

Actually it isn't that easy. I think what you are trying to do is use OLE to
activate MS Word from Visual Basic. A lot depends on versions here.
If you are using Word 6.0 then the fun has just begun. If you are using Word
97, it is a lot easier because the object model will support access to
different parts of the application.
Here is a basic outline for activating OLE. I will use Excel as an example
since it is much more standard.

dim xl as Object
'
set xl = CreateObject("Excel.Application")  ' the magic here is different for
different word variants
' Now you have an object reference and can use the various services of Excel
xl.visible = true     ' so you can see what is going on.
...
Using the OLE reference should help from here.
'
Just remember to set xl = Nothing to clean up the whole thing.

Good luck.

Quote:

> I'm fairly new at this game, but have written a fair sized program - a
> noise estimation program for pumps.  Having worked out all the
> calculations I have a set of end variables that I would like to present
> on a word document.  The progress of events I see as being something like
> this:

> 1.      Open Word.
> 2.      Open a pre-written read-only doc in word with fields in it.
> 3.      VB program puts each variable into the corresponding field in the
>         doc.
> 4.      Print the doc.
> 5.      Close word.

> Dead easy I'm sure.  I'm not expecting anyone to write the code for me
> (though some bigs hints would be cool) but I'd be grateful if someone
> could point me in the right direction.
> Martina



Tue, 04 Apr 2000 03:00:00 GMT  
 Operating Word from VB5



Quote:
>I'm fairly new at this game, but have written a fair sized program - a
>noise estimation program for pumps.  Having worked out all the
>calculations I have a set of end variables that I would like to present
>on a word document.  The progress of events I see as being something like
>this:

>1.  Open Word.
>2.  Open a pre-written read-only doc in word with fields in it.
>3.  VB program puts each variable into the corresponding field in the
>    doc.
>4.  Print the doc.
>5.  Close word.

>Dead easy I'm sure.  I'm not expecting anyone to write the code for me
>(though some bigs hints would be cool) but I'd be grateful if someone
>could point me in the right direction.
>Martina

Martina,

Sounds like your talking about OLE Automation.  The code is quite
simple.

1.      Open Word.

Dim wd as object
Set wd = CreateObject("Word.Basic")

2.      Open a pre-written read-only doc in word with fields in it.

wd.fileopen "C:\MyPath\Myfile.doc"

3.      VB program puts each variable into the corresponding field in

        the doc.

'Assuming you have created form fields in your document

wd.setformresult "MyBookmark1","The text I want to send over"

'Do this for each field

4.      Print the doc.

'This may work

wd.fileprint "C:\MyPath\Myfile.doc"

5.      Close word.

wd.close False
'False is the value to the SaveChanges Argument

That's it.....

Good Luck....

Rob C



Tue, 04 Apr 2000 03:00:00 GMT  
 Operating Word from VB5



Quote:
>Actually it isn't that easy.

An easier (but messier) inelegant way is to use sendkeys to simulate the
manual opening of Word and the typing of the fields.  You may need to check
the version of Word and send different key sequences for different versions.
Type "sendkeys" and hit F1 for help and an example program.

Another hackers' method which I have not tried is to create your Word
document with unique codes in the gaps where your values will go.  Then
search the .doc file and do the substitutions.  Unfortunately this would not
print the document.
--
Peter Hesketh, Mynyddbach, Gwent, UK
"Sex is not the answer.   Sex is the question.  Yes is the answer."



Wed, 05 Apr 2000 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Operating Word from VB5

2. Word operating limits trouble and odd file size growth

3. Window flash from Word Object vb5, word 8

4. HW: Execute MS Word from VB5 and insert text into Word

5. HW: Execute MS Word from VB5 and insert text into Word

6. VB5 HARD ONE:Locking Word OLE session to other Word Docs

7. Operating on field contents

8. Determine Operating System

9. Please Help: Operating System Shutting Access Down?

10. Operating System Shutting Down?

11. MDE Problems under different operating systems

12. Operate Macro By F11 ?

 

 
Powered by phpBB® Forum Software