Home

Search this site:
 

Horshack’s
very
own site

 

This is Bavaria, Germany!
You will find links to Perl,
bootdisks for system administration,
a lot of Linux,
Windows administration
and some personal belongings.
Remotecontrol Winword97 with Visual Basic

VBA code snippets

A word of wisdom: This page was written about 10 years ago, in the meantime I prefer to write documents like this with lout which is a text formatting system which works very well in batch-jobs and certainly with perl.

These pages have been written for WindowsNT/95 with Visual Basic 5 and Winword 97 and later.This code will not work with Winword 6 oder Winword 95.

Office automation means to call Winword (or other applications) from Visual Basic and let the program do things you could do only hard with Visual Basic. I will show you how to do this:

How to connect to Winword 97 (does not work with WW6/95)

This is the project like Visual Basic sees it. Winword has to be created (started) as an object by Visual Basic

Dim wwd as Word.Application

Don't DIM it as Object (dim wwd as object) which also would work! When you dim the variable as a thing, here a Word.Application, what it will be later you can browse through the object-hierarchie within the editor of Visual Basic.

Set wwd = CreateObject("Word.Application")
' until now the object is hidden, but you can see it in your task window!
wwd.visible=true
wwd.activate

There was an object word.basic which is there until now in Winword97, but it's only there for compatibility. New code should be developed in VBA and so with Word.Application.

Ok, but what if we don't want to open a new instance of Winword each time? We have to check wether there is an object with this name. But if there is NOT... an error will be raised, but that's no trouble if we know it:

Dim wwd as Object:

' call Winword, if it's there take the open one, if not open a new Winword
On Error Resume Next
Set wwd = GetObject(, "Word.Application")
If Err.Number > 0 Then
    Set wwd = CreateObject("Word.Application")
End If
' Handle errors like always again
On Error GoTo 0

constants declaration: wdxxx

When translating VBA-macros from Winword97 to Visual Basic code there are a lot of wdxxx-constants unknown to Visual Basic. The constants for Wordprogramming like

wdGotoBookmark
wdCharacter
[...]

are defined here.

VBA-Winword 97: open document

Open document with template:

wwd.Documents.Add Template:= "D:\Programs\Microsoft Office\templates\head_office.dot",
NewTemplate:= False

And without a winword-template, simply white paper:

wwd.documents.add , NewTemplate:=False

VBA-Winword 97: textmarks

Go to textmark:

wwd.Selection.GoTo What:=wdGoToBookmark, Name:="ThisLetterGoesTo"

VBA-Winword 97: mark and work

You need this when you don't like that your new text puts the rest of the page down for each line you write into the letter...

Mark until end of line and delete:

wwd.Selection.EndKey Unit:=wdLine, Extend:=wdExtend
wwd.Selection.Delete Unit:=wdCharacter, Count:=1

Mark until the end of the line and overwrite with given words:

wwd.Selection.EndKey Unit:=wdLine, Extend:=wdExtend
wwd.Selection.TypeText Text:="Richard Lippman"
wwd.Selection.MoveDown Unit:=wdLine, Count:=1

VBA-Winword 97: autotext

Include Autotext "WelcomeNewMember", which exists in the document, at the cursor-point:

wwd.NormalTemplate.AutoTextEntries("WelcomeNewMember").Insert Where:=Selection.Range

How to say Good bye to Winword

When executing this code Winword is gone! Don't do this if your user wants to work with your text manually.

wwd.Quit
' Set the Word OLE Object to nothing to free the appWd object memory:
Set wwd = Nothing

To try it out: cut & paste

A little project to cut and paste is found here:

' in a Visual Basic project
' open winword, a new document and print some text
Const wdCharacter = 1
Const wdExtent = 1
Const wdExtend = 1
Const wdGoToBookmark = -1
Const wdLine = 5
Const wdCell = 12
Const wdTableFormatSimple2 = 2
Const wdAlignParagraphRight = 2
Const wdYellow = 7
Const wdToggle = 9999998
Const wdAlignParagraphCenter = 1

Public Sub Winword97Test()
Dim wwd as Object

' call Winword, if it's there take the open one, if not open a new Winword
On Error Resume Next
Set wwd = GetObject(, "Word.Application")
If Err.Number > 0 Then
Set wwd = CreateObject("Word.Application")
End If
' Handle errors like always again
On Error GoTo 0

wwd.visible=true
wwd.activate
wwd.Documents.Add Template:= "D:\Programs\Microsoft Office\templates\head_office.dot", NewTemplate:= False
wwd.Selection.TypeText Text:="Richard Lippmann"
' Only if Winword should be closed
' wwd.Quit
' Set the Word OLE Object to nothing to free the appWd object memory,
' but Winword stays open!
' Set wwd = Nothing

Are you a guru because of knowing all the VBA-basic by heart?

I am not. And I will show you how you can find out all the weird stuff Winword needs to work.All the things you need after wwd.activate is given to me by Winword itself (this is translated from german Winword so the menu does not have to be 100% what you see but you see the way):

You see it's easy. There are a few things to do. All the constant 'variables' like wdExtent are not know in Visual Basic, so you have to find them out by working with the "immediate windows". When being in the Visual Basic Editor of Winword press Ctrl-g and type
print wdExtent
What you find out it what you need to define in Visual Basic like this

(Samplecode for download here: winwordremote.zip)

Impressum