Word Automation in Office 2000 and 2003

J

Jac Tremblay

Hi,
I use Access 2000 and Word 2000 at the office and version 2003 at home.
Through Access, I use automation to create a report. It works fine in 2003
but bugs in the 2000 version after the first report has been produced.
When I run the report in 2000, it works fine the first time. If I do it
again, I get an error message saying that the server control is not available
or has lost it's link (or something like this). See the code below. Does
anyone know about some bug that was corrected in Word 2000? And the way to go
around it.
' ****************************************
' ...
Dim oWord As Word.Application
Dim oDoc As Word.Document
' ...
Set oWord = CreateObject("Word.Application")
oWord.Visible = True
Set oDoc = oWord.Documents.Add
oDoc.Activate
With oDoc.ActiveWindow.Selection
..TypeText Text:="Company Name"
..ParagraphFormat.Style = "Title 1"
..TypeParagraph
..TypeText "Report from " & intYear - 1
..ParagraphFormat.Style = "Title 2"
..TypeParagraph
..TypeText "Sickness - " & vbTab & vbTab & " Diagnostic... "
..TypeText "positive - negative"
..ParagraphFormat.Style = "Title 3"
..TypeParagraph
End With
' Here comes the problem...
' I added this line to test if it would work better...
oDoc.Activate
oWord.ActiveDocument.ActiveWindow.Selection.ParagraphFormat.TabStops.Add _
Position:=CentimetersToPoints(8), _
Alignment:=wdAlignTabDecimal, _
Leader:=wdTabLeaderDots
' The rest works fine...
' Save the document.
oWord.ActiveDocument.SaveAs FileName:="Report " & _
intYear - 1, FileFormat:=wdFormatDocument
' ************ End of code *****************
It seems that the reference to the document created is lost somewhere. How
can this be avoided?
That is why I added the "oWord.ActiveDocument.ActiveWindow.Selection"
I wish someone can tell help me.
Thanks.
 
J

Jezebel

Your problem would go away if you wrote the code without using the Selection
object. There have been some subtle changes with the Selection object itself
and its behaviour according to whether or not the application is active. If
you're running Word from another app (as in your case) it is pretty well
essential to avoid the Selection object (and it makes for faster and better
code anyway) ...

Set oDoc = oWord.Documents.Add
oDoc.Content.InsertAfter "Company Name"
oDoc.Content.InsertParagraphAfter
with oDoc.Paragraphs(oDoc.Paragraphs.Count).Range.ParagraphFormat
.Style = "Title 1"
.TabStops.Add Position:=CentimetersToPoints(8),
Alignment:=wdAlignTabDecimal, Leader:=wdTabLeaderDots
end with


etc

I'm puzzled by the CentimetersToPoints function -- Word has this function,
but I think Access doesn't. Then again, your code wouldn't run even once if
that were a problem.
 
J

Jac Tremblay

Hi, Jezebel,
I will try your suggestion and reply again later during the day. It sounds
good.
Thank you for your comment.
 
J

Jac Tremblay

Hi again Jezebel,
I tried your solution in my application and I still get the same error after
the first report has been produced. The first try is always fine. When I want
to repeat the report, it bugs at the same place. Here is part of the second
version I produced according to your comment.
' ************ Start of code *****************************
Sub TestDeJac()
Dim oWord As Word.Application
Dim oDoc As Word.Document
Set oWord = CreateObject("Word.Application")
oWord.Visible = True
Set oDoc = oWord.Documents.Add
oDoc.Activate
oDoc.Content.InsertAfter "Report title"
oDoc.Paragraphs(oDoc.Paragraphs.Count).Range.Style = "Title 1"
oDoc.Content.InsertParagraphAfter
' *********** Bug is still here *******************
With oDoc.Paragraphs(oDoc.Paragraphs.Count).Range.ParagraphFormat
..TabStops.Add Position:=CentimetersToPoints(8), _
Alignment:=wdAlignTabDecimal, Leader:=wdTabLeaderDots
..TabStops.Add Position:=CentimetersToPoints(10), _
Alignment:=wdAlignTabDecimal, Leader:=wdTabLeaderDots
End With
' *********** Bug is up here ********************
' ...
oWord.ActiveDocument.SaveAs FileName:="Report for the year " & _
intYear - 1, FileFormat:=wdFormatDocument
oDoc.Activate
oDoc.Save
' ...
oDoc.Close SaveChanges:=False
Set oDoc = Nothing
Set oWord = Nothing
End Sub
' ************* End of code ******************************
The error message says that the server reference is gone or unavailable.
By the way, there is no problem with the CentimetersToPoints function as I
have recorded a macro to get the code from. It works fine. I use the french
version of Word 2000.
Thank you again for your comment and even though it does not bring a
solution to my problem, I appreciate your concern.
 
J

Jezebel

Try replacing the CentimetersToPoints statements with constants. There are
28.35 points in a centimeter.
Also replace the Word constants with their actual values (wdAlignTabDecimal
= 3, wdFormatDocument = 0). Access won't know what these constants mean
unless you've added a reference to Word to your VBA project.
 
J

Jac Tremblay

Hi again Jezebel,
The problem lies in the object references that Word cannot carry on for some
reason in the version 2000 of Word. The problem does not occur with Word 2003
(I haven't tried it with 2002, so I can't say).
I am sure that there will be no gain in changing a statement for a constant.
They both give the same result and are both recognized by the Word
application I use through Access. Of course, I added in my project a
reference to Microsoft Word 2000 because otherwise, I wouldn't have been able
to declare an object reference such as:
' ************************
Dim oWord As Word.Application
Dim oDoc As Word.Document
' ************************
I must find another solution.
Thank you anyway for your reply.
 
J

Jezebel

The constants you use in the code have to be recognised by Access, not Word.
If you have a reference to Word in your Access projects References list,
that will work (but if you have such a reference, why are you using late
binding?) -- if not, and if your Access app does not have 'option explicit'
set, then Access will be substituting zeroes in place of the constants.
Which maybe affects one version of Word more than others...
 
J

Jac Tremblay

Hi Jezebel,

I do have Option Explicit declared in Access. And the code runs on first try
every time. So the constants are recognized by both Word and Access. So that
is not the problem. The code even runs other times without problems if I go
to some other form before I rerun it.

The problem lies in the fact that Word looses its reference to the oDoc
object and I do not know why. That's what I want to know to try to correct
the problem.

Thanks again for your reply
 

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