Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

open a specific word doc from access

Status
Not open for further replies.

chiefvj

Technical User
Feb 4, 2005
73
0
0
US
I would like to open a specific Word document from Access. I can open Word from access but I would like to store the location of specific doc in an Access table and open them from Acces.An ideas...thx
 
dim a
a = Shell("WinWordExePath PathOfYourDocFile", 1)
 
You could save the entries as Hyperlinks within a the table.
 
Or use this

Code:
Dim strName As String
Dim sh As Object

strName = "PathOfFileYouWantToOpen"

Set sh = CreateObject("WScript.Shell")
sh.Run Chr(34) & strName & Chr(34), 1

F_exit:
Set sh = Nothing
Exit Sub

HTH

Mark...
 
To continue Taff07's thoughts, you can use FollowHyperlink for any registered documents:
hl = "C:\Data\Tek-Tips.rtf"
FollowHyperlink hl
 
I placed the code on the onclick event of a command botton....code does not work. It ends up exiting the application without opening Word. thx
 
chiefvj

Can we have a look at the code ?

And also how the document is stored in the table.

Mark...
 
The following code opens Word but generate an error message in Word "unable to find the document"

Private Sub Command0_DblClick(Cancel As Integer)
Dim stAppName As String
Dim stPathA As String
Dim stPathB As String
Dim stCombinedPath As String

stPathA = SysCmd(acSysCmdAccessDir)
stAppName = "WINWORD.EXE "
stPathB = "C:\Documents and Settings\Fredrick\My Documents\timesheet access programs\fox-otfolder\ACCESS ROUTINES FOR VISUAL BASIC\ea.doc"
stCombinedPath = stPathA & stAppName & stPathB
Call Shell(stCombinedPath, 1)

End Sub
'********************************************

this code doesnt even open word and exits from Access.

Private Sub Command1_Click()
On Error GoTo Err_Command1_Click


Dim strName As String
Dim sh As Object

strName = "C:\Documents and Settings\Fredrick\My Documents\timesheet access programs\fox-otfolder\ACCESS ROUTINES FOR VISUAL BASIC\ea.doc"

Set sh = CreateObject("WScript.Shell")
sh.Run Chr(34) & strName & Chr(34), 1

F_exit:
Set sh = Nothing

Exit_Command1_Click:
Exit Sub

Err_Command1_Click:
MsgBox Err.Description
Resume Exit_Command1_Click

End Sub
 
What about:
Code:
stPath = "C:\Documents and Settings\Fredrick\My Documents\timesheet access programs\fox-otfolder\ACCESS ROUTINES FOR VISUAL BASIC\ea.doc"
If Dir(stPath)<>"" Then
   FollowHyperlink stPath
Else
   MsgBox "Cannot find " & strpath
End If

As a PS, I have found it useful to store the common part of the path in a separate table, to make it easy to move the files to a new location.
 
thanks it works..... you are the one!!!!!====:). I don't want to be a glutton but any ideas how to store the path and Word doc names in a table so that i can dont have to hard code each word doc????
thx
 
Maybe:
Code:
Sub BuildDocTable(strPath)
'References Office 9.0 Object Library
'Fields in tblDocumentListing:
'DocID: Autonumber
'FileFullPath: Text, 255
'CrDate: Date
'AcDate: Date
'ModDate: Date
'FileSize: Long Integer

Dim lngFileIndex As Long
Dim vFile
Dim vFilePath
Dim vDateCreated
Dim vDateLastAccessed
Dim vDateLastModified
Dim vFileSize As Long
Dim strSQL As String
Dim vMaxFileSize As Long
Dim vMaxPathLen As Integer
Dim fs

If Right(strPath, 1) <> "\" Then
    strPath = strPath & "\"
End If

'File system object to get file details
Set fs = CreateObject("Scripting.FileSystemObject")
vMaxFileSize = 2147483647 '2,147,483,647, max of long
vMaxPathLen = 255 'max of text

'Look for Word documents in strPath
With Application.FileSearch
    .NewSearch
    .LookIn = strPath
    .SearchSubFolders = True
    .FileType = msoFileTypeWordDocuments
    .FileName = "*.doc"
    .MatchTextExactly = True
    
    'If files are found ...
    If .Execute() > 0 Then
        'Get the details
        For lngFileIndex = 1 To .FoundFiles.Count
            vFilePath = .FoundFiles(lngFileIndex)
            Set vFile = fs.GetFile(.FoundFiles(lngFileIndex))
            vDateCreated = vFile.DateCreated
            vDateLastAccessed = vFile.DateLastAccessed
            vDateLastModified = vFile.DateLastModified
            vFileSize = FileLen(.FoundFiles(lngFileIndex))
            Set vFile = Nothing
            'If there is a problem, ...
            If Len(vFilePath) > vMaxPathLen Or vFileSize > vMaxFileSize Then
                Debug.Print vFilePath
            Else
            'Otherwise insert details into tblDocumentListing
            'Uncomment the next line, and its match below,
            'to get rid of "You are about To ..." warning
            'message.
            'DoCmd.SetWarnings False
                strSQL = "INSERT INTO tblDocumentListing " _
                & "( FileFullPath, CrDate, AcDate, ModDate, FileSize ) " _
                & "SELECT '" & vFilePath & "'," _
                & MakeUSDate(vDateCreated) & "," _
                & MakeUSDate(vDateLastAccessed) & "," _
                & MakeUSDate(vDateLastModified) & "," _
                & vFileSize
            End If
            'Uncomment the two SetWarnings to get rid of
            '"You are about To ..." warning message.
            'DoCmd.SetWarnings False
            DoCmd.RunSQL strSQL
            'DoCmd.SetWarnings True

        'Next file
        Next lngFileIndex
    End If
End With
End Sub

Function MakeUSDateX(X As Variant)
'Needed for SQL
    If Not IsDate(X) Then Exit Function
    MakeUSDate = "#" & Month(X) & "/" & Day(X) & "/" & Year(X) & "#"
End Function

Alternatively, you could use Application.FileSearch to look in the designated directory every time, and list file names in a combo box or such like.
 
thanks very much for your help. This is exactly what I needed. It work swell..thx
 
You are welcome. I guess you have noticed that this:
'Uncomment the next line, and its match below,
'to get rid of "You are about To ..." warning
'message.
'DoCmd.SetWarnings False

About half way down is a mistake in commenting and should be deleted.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top