maro to open word doc

dmacleod

New member
Local time
Today, 08:42
Joined
Aug 26, 2008
Messages
7
Macro to open word doc

Can someone help me with a macro? I have only been working on access databases for a couple of weeks now but learning fast. I would like to use a macro to open a word doc. I can open the doc with the following code( Application.FollowHyperlink "C:\Template.doc") . I need to do this with a macro so that I can link this event to a button click that also does a make table query. Can anyone help?
 
Last edited:
Hi...

You should try adding some VB code on the ON EVENT as opposed to the macro...

try this green code (just copy and paste to your DB)


DoCmd.SetWarnings False ' turn the warnings off - or delete this line
DoCmd.RunSQL ("Select * FROM table1;") ' Copy and paste your SQL here or use ' DoCmd.OpenQuery "YourQueryNameHere"
DoCmd.SetWarnings True ' turn warnings back on

ShellExecute 0, "Open", "C:\Template.doc", "", "", 0


Thats the easiest way.... really its easier than macros when you start getting the hang of it.. .any probs and let me know...
 
Thanks Just what I was looking for.
The only issue I have so far is when I click on this button I get the following Compile error "Sub or function not defined" with the "ShellExecute" highlighted.
 
Sorry... was working from memory...

Create a new Module called something like modShellExecute and add this too it.


PHP:
Option Compare Database
Option Explicit

'************ Code Start **********
'This code was originally written by Dev Ashish.
'It is not to be altered or distributed,
'except as part of an application.
'You are free to use it in any application,
'provided the copyright notice is left unchanged.
'
'Code Courtesy of
'Dev Ashish

Private Declare Function apiShellExecute Lib "shell32.dll" Alias _
    "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile _
    As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal _
    nShowCmd As Long) As Long

'***App Window Constants***
Public Const WIN_NORMAL = 1         'Open Normal
Public Const WIN_MAX = 2            'Open Maximized
Public Const WIN_MIN = 3            'Open Minimized

'***Error Codes***
Private Const ERROR_SUCCESS = 32&
Private Const ERROR_NO_ASSOC = 31&
Private Const ERROR_OUT_OF_MEM = 0&
Private Const ERROR_FILE_NOT_FOUND = 2&
Private Const ERROR_PATH_NOT_FOUND = 3&
Private Const ERROR_BAD_FORMAT = 11&

'***************Usage Examples***********************
'Open a folder:     ?fHandleFile("C:\TEMP\",WIN_NORMAL)
'Call Email app:    ?fHandleFile("mailto:dash10@hotmail.com",WIN_NORMAL)
'Open URL:          ?fHandleFile("http://home.att.net/~dashish", WIN_NORMAL)
'Handle Unknown extensions (call Open With Dialog):
'                   ?fHandleFile("C:\TEMP\TestThis",Win_Normal)
'Start Access instance:
'                   ?fHandleFile("I:\mdbs\CodeNStuff.mdb", Win_NORMAL)
'****************************************************

Function fHandleFile(stFile As String, lShowHow As Long)
Dim lRet As Long, varTaskID As Variant
Dim stRet As String
    'First try ShellExecute
    lRet = apiShellExecute(hWndAccessApp, vbNullString, stFile, vbNullString, _
        vbNullString, lShowHow)
            
    If lRet > ERROR_SUCCESS Then
        stRet = vbNullString
        lRet = -1
    Else
        Select Case lRet
            Case ERROR_NO_ASSOC:
                'Try the OpenWith dialog
                varTaskID = Shell("rundll32.exe shell32.dll,OpenAs_RunDLL " & _
                    stFile, WIN_NORMAL)
                lRet = (varTaskID <> 0)
            Case ERROR_OUT_OF_MEM:
                stRet = "Error: Out of Memory/Resources. Couldn't Execute!"
            Case ERROR_FILE_NOT_FOUND:
                stRet = "Error: File not found.  Couldn't Execute!"
            Case ERROR_PATH_NOT_FOUND:
                stRet = "Error: Path not found. Couldn't Execute!"
            Case ERROR_BAD_FORMAT:
                stRet = "Error:  Bad File Format. Couldn't Execute!"
            Case Else:
        End Select
    End If
    fHandleFile = lRet & IIf(stRet = "", vbNullString, ", " & stRet)
End Function
'************ Code End **********
 
You could do it just by


PHP:
Dim stAppName As String

 stAppName = _
     """C:\WINDOWS\explorer.exe"" /e, C:\Program Files\Microsoft Office\OFFICE11\Excel.exe"

Call Shell(stAppName, 1)

You may need to adjust the link to the link to your excel slightly...
 
You could do it just by


PHP:
Dim stAppName As String

 stAppName = _
     """C:\WINDOWS\explorer.exe"" /e, C:\Program Files\Microsoft Office\OFFICE11\Excel.exe"

Call Shell(stAppName, 1)

You may need to adjust the link to the link to your excel slightly...

Not a good solution because it requires you to know what version your users are on and it doesn't work if some are on another version.

A quick way to open an Excel workbook without needing to know what version is installed is to use

Code:
FollowHyperlink "PathToYourExcelWorkBook.xls"

Another way is to use Excel COM programming:

Code:
Dim objXL As Object
Dim xlWB As Object

Set objXL = CreateObject("Excel.Application")
objXL.Visible = True
Set xlWB = objXL.Workbooks.Open("PathToYourExcelWorkbook.xls")

To just open Excel you can use the first two lines:

Code:
Dim objXL As Object

Set objXL = CreateObject("Excel.Application")
objXL.Visible = True

I could get more detailed as well but that should help.
 
hi, thnx the help, but i've been trying to make it works and i don't know why but it doesn't run.
 
ok... i did it... but... excel just open and it doesn't create a new file... if i want that, what i have to add in the command line?
 
ok... i did it... but... excel just open and it doesn't create a new file... if i want that, what i have to add in the command line?

It would be:
Code:
Dim objXL As Object
Dim xlWB As Object

Set objXL = CreateObject("Excel.Application")
objXL.Visible = True
Set xlWB = objXL.Workbooks.AddNew
 

Users who are viewing this thread

Back
Top Bottom