How do I open specific word documents from Access?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shawnntjr
    New Member
    • Oct 2014
    • 3

    How do I open specific word documents from Access?

    Hi all,

    I am quite new to the VBA language, so please forgive me if I am not using the correct terms.

    I have a table that has a column for File Path (Z:\) and another column for File Name (Test1.doc).

    I am required to create a form where the user inputs a search keyword and it will run against all the File Names and open the document with the best match. However, I'm gonna take this step by step and figure out how to open specific word documents first.

    Code:
    Private Sub Command0_Click()
        Dim wrdApp As Word.Application
        Dim wrdDoc As Word.Document
        Dim filepath As String
    
        'Open Word
        Set wrdApp = CreateObject("Word.Application")
        wrdApp.Visible = True
    
        
        filepath = FPath & FName
    
        'Open the file
        Set wrdDoc = wrdApp.Documents.Open(filepath)
    End Sub
    I am currently using this code to open my word documents, however it only manages to open the Word application, not the document.

    How do I go about correcting this code?
  • twinnyfo
    Recognized Expert Moderator Specialist
    • Nov 2011
    • 3653

    #2
    shawnntjr,

    I have copied and pasted your code and it works perfectly. Are you receiving any errors or is the code just "not working"?

    Have you stepped through the code line by line to see what the code is doing or not doing?

    Comment

    • jforbes
      Recognized Expert Top Contributor
      • Aug 2014
      • 1107

      #3
      Another thing you can try it to Shell to the Word Document instead of creating an instance of Word and then opening the Document.
      Code:
          Dim dReturn As Integer
          dReturn = Shell("explorer.exe " & filepath, vbNormalFocus)
      It's a blunt instrument, but might work for you.

      There is also the FollowHyperlink option to consider if you plan on opening a lot of files from Access in the future: http://allenbrowne.com/func-GoHyperlink.html

      Comment

      • shawnntjr
        New Member
        • Oct 2014
        • 3

        #4
        There're no errors. Microsoft Word will start up, however no files will be opened. My code basically just opens the program.

        Comment

        • twinnyfo
          Recognized Expert Moderator Specialist
          • Nov 2011
          • 3653

          #5
          Again, your code should work. Are you absolutely certain that your File/Path Name are correct?

          Comment

          Working...