Consulting

Results 1 to 12 of 12

Thread: Solved: Starting Word from Access

  1. #1

    Solved: Starting Word from Access

    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?


  2. #2
    Can you post your code? Especially the part checking for Word and opening it?

  3. #3
    VBAX Regular
    Joined
    Jul 2004
    Location
    San Bernardino, California
    Posts
    69
    Location
    Quote Originally Posted by khagerman
    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:

    [vba]Dim objWord as Object

    Set objWord = CreateObject("Word.Application")[/vba]

    Early Binding looks like this:

    [vba]
    Dim objWord as Word.Application
    [/vba]

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

    - Anthony

  4. #4

    Code shown

    OK, here's the code:

    [vba]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[/vba]

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

  5. #5
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    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.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  6. #6

    Maybe it was the references?

    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.

  7. #7
    VBAX Regular
    Joined
    Jul 2004
    Location
    San Bernardino, California
    Posts
    69
    Location
    Quote Originally Posted by khagerman
    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

  8. #8
    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?

  9. #9
    VBAX Regular
    Joined
    Jul 2004
    Location
    San Bernardino, California
    Posts
    69
    Location
    Quote Originally Posted by khagerman
    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

    HTH
    -Anthony

  10. #10

    Now an error message

    Anthony,

    I took this part of your example:

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

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

    Anthony,

    Duh....This works fine now:

    [vba] 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[/vba]

    Thanks very much for the help.

  11. #11
    VBAX Regular
    Joined
    Jul 2004
    Location
    San Bernardino, California
    Posts
    69
    Location
    Quote Originally Posted by khagerman
    Anthony,

    I took this part of your example:

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

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

    Anthony,

    Duh....This works fine now:

    [vba] 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[/vba]

    Thanks very much for the help.
    Tada!!! Glad I could help!

    - Anthony

  12. #12
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    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!
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •