How do I Dim an MS Word object (Range)?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a word document I run from Access.

I need to declare an MS Word object in Access (Dim r as Range).

Range is not available in Access. Is there a way to Dim r as an MSWord Range?
 
Have you set a reference to Word yet? In any code window, choose Tools >
References and then scroll down to find the reference to MSWord and check
it.

Then you can create Word objects.
Dim objWord As Word.Application
Set objWord = New Word.Application

Then you can do most anything you can program in Word in Access by adding
objWord. in front of it:


--
--Roger Carlson
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L

Al said:
I have a word document I run from Access.

I need to declare an MS Word object in Access (Dim r as Range).

Range is not available in Access. Is there a way to Dim r as an MSWord
Range?
 
Hi Al,

If you set a reference (Tools|References) to the Microsoft Word Object
Library you will then be able to use
Dim r As Word.Range

It will probably work without the "Word.", but including it (a) makes your
code easier to understand and (b) means there's no ambiguity if you ever
need to work with Excel (which also has a Range object) too.

Alternatively, read about "late binding" and use
Dim r As Object

Late binding is much more reliable if your code is going to be distributed
to other computers which may have different versions of Office.
 
This partially works. My code follows:

Dim appword, Docs, prps As Object
Dim oRng As Word.Range
Set oRng = ActiveDocument.Bookmarks("Zip").Range

Set appword = CreateObject("Word.Application")
Set Docs = appword.Documents
Docs.Add strSched

Set oRng = ActiveDocument.Bookmarks("Zip").Range
oRng.End = oRng.Start + 10
ActiveDocument.Bookmarks.Add Name:="ZipCode", Range:=oRng

I get an error on the Set oRng line. The error is:

Runtime error: 5491 'The requested member of the collection does
not exist"

It seems like the ".Range" is causing this. I cannot seem to get past this.
Any ideas?
 
John (and all) thankyou for your reply. I found the problem and was able to
program a solution.

The problem is that according to MS Access documentation, some MS Word
ranges cannot be set from MS Access. If the bookmark is located in an MS
Word table then ranges usually work. This is apparently the problem because
my bookmark was just located on the page where a normal zip code usually
lives right after the state.


So in MS Word I built a 1 cell table and placed in an innocuous part of the
document where it would not affect anything. I selected the cell contents
and set a bookmark named "ZipCode". I hide the table by making the font
hidden, and setting borders and shading to none.

Below the city, state zip line I added a barcode field on a new line that
references the ZipCode bookmark. It looks like this:
{BARCODE ZipCode \b \u \* MERGEFORMAT}

Now this code works:

Dim appword, Docs, prps As Object
Dim oRng As Word.Range
Set oRng = ActiveDocument.Bookmarks("Zip").Range

Set appword = CreateObject("Word.Application")
Set Docs = appword.Documents
Docs.Add strSched

Set oRng = ActiveDocument.Bookmarks("ZipCode").Range
oRng.Text = [strProdZipBus]
ActiveDocument.Bookmarks.Add "ZipCode", oRng

Thanks again John,

Hope this helps you and others!!

Alan
 
Al, just a note:
Dim appword, Docs, prps As Object

That translates as
Dim appword as Variant
Dim Docs as Variant
Dim prps as Object

As you have seen, it normally doesn't make any difference if you
declare variables as Variants instead of Objects. The rules for
'IF' statements are different ('IF appword=Docs'), but you don't
normally do that to objects anyway.

(david)
 
Back
Top