I have some code I found and modified as shown below that opens a Word document. However the window is behind the Access application window that has the button with the code to open the Word application and document. I've searched and so far can't find the code to bring the Word application to be the top window; it is on the task bar. I will need to do the same with Excel files and .pdf files.
Perhaps there is a more generic way to open these file that does not require opening each specific application (like Word). I tried to use ShellExecute but get a compile error; I would prefer ShellExecute if it worked. Perhaps I'm missing a reference library but don't know which one. In any case, the application window needs to be on top. Any help with code examples would be much appreciated!
Dim CourseDoc As String
Dim myApp As Object
'Path to the word document
'CourseDoc = "c:\Doc1.doc"
If Len(Me.LinkToCourse & vbNullString) = 0 Then ' Checks for null
MsgBox "Invalid file to open.", vbCritical, "Invalid File"
Else
CourseDoc = Me.LinkToCourse
End If
If Dir(CourseDoc) = "" Then
MsgBox "Document not found."
Else
'Create an instance of MS Word
Set myApp = CreateObject(Class:="Word.Application")
myApp.Visible = True
'Open the Document
myApp.Documents.Open FileName:=CourseDoc
If Not (myApp.ActiveWindow Is Nothing) Then 'only if there's a window
myApp.ActiveWindow.Activate
End If
End If
Thank you!
x
Perhaps there is a more generic way to open these file that does not require opening each specific application (like Word). I tried to use ShellExecute but get a compile error; I would prefer ShellExecute if it worked. Perhaps I'm missing a reference library but don't know which one. In any case, the application window needs to be on top. Any help with code examples would be much appreciated!
Dim CourseDoc As String
Dim myApp As Object
'Path to the word document
'CourseDoc = "c:\Doc1.doc"
If Len(Me.LinkToCourse & vbNullString) = 0 Then ' Checks for null
MsgBox "Invalid file to open.", vbCritical, "Invalid File"
Else
CourseDoc = Me.LinkToCourse
End If
If Dir(CourseDoc) = "" Then
MsgBox "Document not found."
Else
'Create an instance of MS Word
Set myApp = CreateObject(Class:="Word.Application")
myApp.Visible = True
'Open the Document
myApp.Documents.Open FileName:=CourseDoc
If Not (myApp.ActiveWindow Is Nothing) Then 'only if there's a window
myApp.ActiveWindow.Activate
End If
End If
Thank you!
x