Automation with Word 2000

G

Guest

Hi,
I posted a question to the Word programming newsgroup but get no satisfying
answer. I decided to post it here hoping to get at least an explanation. Here
is the problem:
I use Access 2000 and Word 2000 at the office. Through Access, I use
automation to create a report. When I run the report in 2000, it works fine
the first time. If I run 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.
' ****************************************
' ...
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
End With
' Here comes the problem...
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 got an answer from Jezebel stating that maybe I should not use the
Selection object. I tried the suggestion and came out with this code:
' ************ 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
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.
Again I say: the report works fine the first time it is called but bugs if
it is called again in the same session. If close the application and reopen
it, it works fine the first time... I use the french version of Word 2000.
I hope someone can give me some encouragement before Christmas.
Thanks
 
R

RoyVidar

Jac Tremblay wrote in message
Hi,
I posted a question to the Word programming newsgroup but get no satisfying
answer. I decided to post it here hoping to get at least an explanation. Here
is the problem:
I use Access 2000 and Word 2000 at the office. Through Access, I use
automation to create a report. When I run the report in 2000, it works fine
the first time. If I run 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.
' ****************************************
' ...
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
End With
' Here comes the problem...
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 got an answer from Jezebel stating that maybe I should not use the
Selection object. I tried the suggestion and came out with this code:
' ************ 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
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.
Again I say: the report works fine the first time it is called but bugs if
it is called again in the same session. If close the application and reopen
it, it works fine the first time... I use the french version of Word 2000.
I hope someone can give me some encouragement before Christmas.
Thanks


By a quick glance, what is probably the culprit, is the unqualified
reference to the Word method CentimetersToPoints. Try
qualifying like this

Position:=oWord.CentimetersToPoints(8), _

I'd probably use the document variable in stead of referencing
through your word application object.
 
G

Guest

Hi Roy,
That is a good point. I will try it and come back with an answer later...
Unfortunately, it will not be today nor tomorrow because of the Christmas
holidays.

I wish you, Roy, and all active members of the Microsoft newsgroups a happy
Christmas.

Thank you for this KISS 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