PDA

View Full Version : Solved: Starting Word from Access



khagerman
03-02-2005, 11:06 AM
I have an Access application distributed to individual customers as a standalone application. The application creates a few reports by opening Word and pushing data from the database to the Word document. To accomplish this Access will open Word if it is not open. If a customer is using Word 2003 there is no problem; it appears that other versions of Word are throwing automation errors. If the customer opens Word first and then minimizes it, everything works. I am using late binding (I think). Any suggestions on where I should look next?

starl
03-02-2005, 12:20 PM
Can you post your code? Especially the part checking for Word and opening it?

ALaRiva
03-02-2005, 12:47 PM
I am using late binding (I think). Any suggestions on where I should look next?

It doesn't sound like you are using late binding.

Late Bind looks like this:

Dim objWord as Object

Set objWord = CreateObject("Word.Application")

Early Binding looks like this:


Dim objWord as Word.Application


If that doesn't help, then show us the code that you are using.

- Anthony

khagerman
03-02-2005, 12:51 PM
OK, here's the code:

Private Sub cmdPhoto_Click()

On Error GoTo Err_cmdPhoto

Dim objWord As Object
Dim intReportID As Integer
Dim strCriteria As String
Dim strClient As String
Dim strClientClaimNumber As String
Dim strInsuredAddress As String
Dim strInsuredName As String

?sets the value for the variables for info to be pushed to
?Word
'what report?
intReportID = Me.ReportID
strCriteria = "[ReportID] = " & intReportID
strClient = IIf(IsNull(DLookup("[ClientName]", "Report2", strCriteria)), _
"", DLookup("[ClientName]", "Report2", strCriteria))
strClientClaimNumber = IIf(IsNull(DLookup("[ClientFileNumber]", "Report2", strCriteria)), _
"", DLookup("[ClientFileNumber]", "Report2", strCriteria))
strInsuredAddress = IIf(IsNull(DLookup("[InsuredFullAddressLong]", "Report2", strCriteria)), _
"", DLookup("[InsuredFullAddressLong]", "Report2", strCriteria))
strInsuredName = IIf(IsNull(DLookup("[InsuredFullName]", "Report2", strCriteria)), _
"", DLookup("[InsuredFullName]", "Report2", strCriteria))
' Launch Word and load the file template
'check if Word is Open, open Word if it is not
If fIsAppRunning("Word") Then
Set objWord = GetObject(, "Word.Application")
Else
Set objWord = CreateObject("Word.Application")
End If
' Opens the template
objWord.Documents.Add _
Application.CurrentProject.Path & "\WorkingPhoto.dot"
strVer = Val(Me.txtLastPhotoMade.Value) + 1
strFileName = NewReportFolder & "PhotoLog" & strVer & ".doc"
objWord.ActiveDocument.SaveAs FileName:=strFileName
Me.txtLastPhotoMade.Value = strVer
With objWord.ActiveDocument.Bookmarks
.Item("Client").Range.Text = strClient
.Item("ClientClaimNumber").Range.Text = strClientClaimNumber
.Item("InsuredAddress").Range.Text = strInsuredAddress
.Item("Insured").Range.Text = strInsuredName
.Item("PhotoSet").Range.Text = strSet
.Item("PhotoSet2").Range.Text = strSet
End With
objWord.ActiveDocument.ActiveWindow.View.Zoom.Percentage = 75
objWord.Visible = True
objWord.Activate
Set objWord = Nothing
Exit_cmdPhoto:
Exit Sub

Err_cmdPhoto:
MsgBox Err.Description
Resume Exit_cmdPhoto

End Sub

I meant to add that fIsAppRunning is from the MVPS Access Web site.

TonyJollans
03-02-2005, 01:18 PM
Personally I'd ditch the fIsAppRunning routine and trap the error if the GetObject fails.

I suspect the fact that it works in 2003 may be conected to using Word in Outlook - I don't entirely understand but I seem to always have Word running since installing 2003.

khagerman
03-02-2005, 01:43 PM
Actually, I've been testing and I think it might be my references. I had a reference to the Microsoft Word 11 Object Library both in the Access app and the Word template. I've removed those references and at least the code still opens the Word doc on my machine. I'm hoping that the references might have been the problem. I really am new at this and don't always understand what references are required. I'd like to remove the Microsoft Office 11 Object Library reference on the Word template but it refuses to allow that.

ALaRiva
03-02-2005, 02:35 PM
Actually, I've been testing and I think it might be my references. I had a reference to the Microsoft Word 11 Object Library both in the Access app and the Word template. I've removed those references and at least the code still opens the Word doc on my machine. I'm hoping that the references might have been the problem. I really am new at this and don't always understand what references are required. I'd like to remove the Microsoft Office 11 Object Library reference on the Word template but it refuses to allow that.

The reference to the Word 11 Object Library is more than likely the problem. Since you are using Late Binding (good) then you don't need any reference to Word set at design time.

Also, I would agree with the other member (TonyJollans) about not using the fIsAppRunning() function and just catch the error on the GetObject() method.

- Anthony

khagerman
03-02-2005, 02:46 PM
Anthony and Tony,

Thanks for the review of the code and the suggestions. As far as catching the error on the GetObject() method, what value will I be looking for?

ALaRiva
03-02-2005, 03:04 PM
Anthony and Tony,

Thanks for the review of the code and the suggestions. As far as catching the error on the GetObject() method, what value will I be looking for?

Have a look here (http://www.utteraccess.com/forums/access/access1805372.html)

HTH
-Anthony

khagerman
03-02-2005, 03:41 PM
Anthony,

I took this part of your example:

Set objWord = GetObject(, "Word.Application")
If Err.Number = 429 Then
Set objWord = CreateObject("Word.Application")
End If

and I get an 'ActiveX can't create object' error message. Did I not take enough code?

Anthony,

Duh....This works fine now:

On Error Resume Next
Set objWord = GetObject(, "Word.Application")
If Err.Number = 429 Then
Set objWord = CreateObject("Word.Application")
End If

On Error GoTo Err_cmdPhoto

Thanks very much for the help.

ALaRiva
03-02-2005, 05:15 PM
Anthony,

I took this part of your example:

Set objWord = GetObject(, "Word.Application")
If Err.Number = 429 Then
Set objWord = CreateObject("Word.Application")
End If

and I get an 'ActiveX can't create object' error message. Did I not take enough code?

Anthony,

Duh....This works fine now:

On Error Resume Next
Set objWord = GetObject(, "Word.Application")
If Err.Number = 429 Then
Set objWord = CreateObject("Word.Application")
End If

On Error GoTo Err_cmdPhoto

Thanks very much for the help.

Tada!!! Glad I could help!

- Anthony

Ken Puls
03-02-2005, 10:23 PM
Hi khagerman, and welcome to VBAX!

As it looks like you got your issue solved, I'm going to mark your thread solved. So you know, though, you can do this yourself next time. Just follow the steps in my signature! :yes