Open Word Docx based on a field number

Lochwood

Registered User.
Local time
Today, 14:33
Joined
Jun 7, 2017
Messages
130
I am trying to automatically open a word document in a folder based on the criteria of a field on the form. I Can get it working so i can get to the folder and then double click to open the file but how can i get the file to open just by clicking the button preferably in read only mode.

Here is my code:

Private Sub Command56_Click()
Const Prep = "\\sbserver\dart$\Document-Library\Doc-Production"
'Const Pub = "\\Servername\DART$\Document-Library\Public"
'Const Res = "\\Servername\DART$\Document-Library\Restricted"
'Const Conf = "\\Servername\DART$\Document-Library\Confidential"
'Const HR = "\\Servername\DART$\Document-Library\HR"
'Const Secure = "\\Servername\DART$\Document-Library\Secure"



'Dim OldPath As String
'Dim NewPath As String
'Dim LoopFile As String
'Dim FileExt() As String
'Dim Doc_Folder As String

'Dim Folder As String
'Dim fso As Object

' Get Main ID from control
'Doc_Folder = Me.Doc_Folder
'Hyperlink = Me.Hyperlink

' OldPath = (Prep)
'NewPath = Replace(Pub & Doc_Number & "", "/", "-")
'LoopFile = Dir(OldPath & "*.*")
'Do While LoopFile <> ""
'FileExt = Split(LoopFile, ".")
'Select Case FileExt(UBound(FileExt))
'Case "docx", "doc"
'Name OldPath & LoopFile As NewPath & LoopFile
' End Select
'LoopFile = Dir
'Loop

'If Document_Classification = "Classified" Then
'Folder = Replace(Conf & Doc_Number, "/", "-")
'Else

'Internal_Document_Library = "No" And Website_Document_Library = "Yes" Then
Folder = Replace(Prep & Doc_Number, "/", "-")

'End If


' Create FileSystemObject
Set fso = CreateObject("Scripting.FileSystemObject")
' Check whether folder exists
If fso.FolderExists(Folder) = False Then
' If not, create it
fso.CreateFolder Folder
End If
' Open it
Shell "explorer.exe " & Folder, vbNormalFocus

End Sub
 
You just want it opened in Word and not do anything with it in Access? If so, there's a MUCH easier solution.

The long version might be (NOTE: UNTESTED AIR CODE)
Code:
Public Sub OpenWordDoc(ByVal FilePath As String)
Dim WordFile As Object
Dim WordApp As Object
    Set WordApp = CreateObject("Word.Application")
    WordApp.Visible = True
    
    Set WordFile = WordApp.Documents.Open(FilePath)
    
    Set WordFile = Nothing
    Set WordApp = Nothing
End Sub
While a shorter version would be
Code:
Public Sub OpenWordDoc(ByVal FilePath As String)
    Shell "winword """ & FilePath & ""
    
End Sub

Either way, just figure out the path to the document you want to open, then pass it to the procedure.
 

Users who are viewing this thread

Back
Top Bottom