Open Word Document from Filepath in Table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AdrianJ1980
    New Member
    • Dec 2007
    • 13

    Open Word Document from Filepath in Table

    Hi all,

    Apologies if this has been discussed before, but my numerous searches (google and otherwise) have come up with nothing.

    What i need to do is open a word document (from Access 2003), from a filepath stored in a table, depending on the values returned in 2 combo boxes on a form. This is what i've got:

    Code:
    Sub Run_Letters()
    
    Dim strPath As String
    Dim strSQL As String
    
    strSQL = "SELECT tblLetter.filePath & tblLetter.fileName " & _
    "FROM tblLetter" & _
    "WHERE tblLetter.Fund = '" & [Forms]![frmMainMenu]![cboSelectFund] & "' AND " & _
    "tblLetter.LetterName = '" & [Forms]![frmMainMenu]![cboSelectLetter] & "';"
    
    DoCmd.RunSQL strSQL = strPath
    
    Set oApp = CreateObject("Word.Application")
    oApp.Visible = True
    oApp.Documents.Open strPath
    
    End Sub
    I recognise that the runSQL won't work because it's not an action query, but i'm stuck for an alternative.

    Any advice would be greatly appreciated.
    Thanks
  • Delerna
    Recognized Expert Top Contributor
    • Jan 2008
    • 1134

    #2
    Add a reference to the Microsoft DAO 3.6 Object library.

    [Code=vb]
    Sub Run_Letters()
    Dim strPath As String
    Dim strSQL As String
    strSQL = "SELECT tblLetter.fileP ath & tblLetter.fileN ame " & _
    "FROM tblLetter" & _
    "WHERE tblLetter.Fund = '" & [Forms]![frmMainMenu]![cboSelectFund] & "' AND " & _
    "tblLetter.Lett erName = '" & [Forms]![frmMainMenu]![cboSelectLetter] & "';"




    'HERE IS THE BIT I HAVE ADDED TO YOUR CODE
    '============== =============== =========
    Dim db As DAO.Database
    Dim rst As DAO.Recordset
    Set db = CurrentDb
    Set rst = db.OpenRecordse t(strSQL)
    strPath= rst.Fields(0)
    '^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^




    Set oApp = CreateObject("W ord.Application ")
    oApp.Visible = True
    oApp.Documents. Open strPath


    'AND THIS BIT
    '===========
    Set rst = Nothing
    Set db = Nothing
    '^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^
    End Sub
    [/code]

    Hope this helps you

    Comment

    • AdrianJ1980
      New Member
      • Dec 2007
      • 13

      #3
      Another example of why this is the best programming forum on the web!!!

      It works perfectly, thanks so much for your help.


      Regards

      Adrian

      Comment

      Working...