Link to home
Start Free TrialLog in
Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America

asked on

windows 8, open a file vba Access

The following sample code at: https://msdn.microsoft.com/en-us/library/office/gg278437(v=office.15).aspx
doesn't work. I guess this code seems belong to Windows 7.
' Specifying 1 as the second argument opens the application in 
' normal size and gives it the focus.
Dim RetVal
RetVal = Shell("C:\WINDOWS\CALC.EXE", 1)    ' Run Calculator.

Open in new window


I know this is expected to open the calculator. Probably it opens in Windows 7 environment but when I tried with windows 8, it didn't work. So far I have come to this conclusion that there have been some changes thus it doesn't open in windows 8.

Question: Having a file full-path, how can I open a file from Access using VBA?
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

>>Having a file full-path, how can I open a file from Access using VBA?

it's the same, using Shell function.
It seems that this has to do with UAC...try to run your application as administrator or disable UAC
Avatar of Mike Eghtebas

ASKER

in Windows 10, the following code works. Could you give your shell version for me to try?
Set excel_app = New Excel.Application
    Set excel_wb = excel_app.workbooks.Open(Me!cboFolder.Column(1) & "\" & str_File, 0)
    excel_app.Visible = True

Open in new window

try:

Shell Application.Path & "\EXCEL.exe """ & Me!cboFolder.Column(1) & "\" & str_File & """", vbNormalFocus

Open in new window

or use ShellExecute API:
Private Declare Function ShellExecute 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
 
Private Const SW_HIDE As Long = 0
Private Const SW_SHOWNORMAL As Long = 1
Private Const SW_SHOWMAXIMIZED As Long = 3
Private Const SW_SHOWMINIMIZED As Long = 2

then:

ShellExecute Application.hwnd, "Open", Me!cboFolder.Column(1) & "\" & str_File, vbNullString, 0&, SW_SHOWNORMAL

Open in new window

for your original scripts, you may change to below, and see if that works?

Set excel_app = CreateObject("Excel.Application")
    Set excel_wb = excel_app.workbooks.Open(Me!cboFolder.Column(1) & "\" & str_File, 0)
    excel_app.Visible = True
Ryan Chong, it works as I have. No need to change it. But, I will try ShellExecute shortly.
FYI:  in windows 8.x Calc.exe has move to %windir%\system32\calc.exe
Boyd,
Dim RetVal
RetVal = Shell("%windir%\system32\calc.exe", 1)    ' Run Calculator.

Open in new window

errors. 53 file not found.
SOLUTION
Avatar of Boyd (HiTechCoach) Trimmell, Microsoft Access MVP 2010-2015
Boyd (HiTechCoach) Trimmell, Microsoft Access MVP 2010-2015
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Boyd,

Last one worked. How can I apply a similar code to open:

"C:\Users\Mike\OneDrive\Documents\_MME_Programing\SQl_Server\70-461Book\666054eBook.pdf"

'and:

C:\Users\Mike\OneDrive\Documents\_MME_Programing\SQl_Server\70-461Book\70-461Summary,docx

'and

"C:\Users\Mike\Documents\EE\Excel_1.xlsx"

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
any reason to accept TheHiTechCoach's comment #a40949477 as the solution? it seems just repeating the suggestion to use Shell function. furthermore, by using Environ("windir") function will not allowed you to open other files in other directories.

I would suggest to accept eghtebas's comment #a40949527 (0 points)  as the solution
@Ryan Chong, , I was not repeating the Shell suggestion but offered a solution to why the posted coded  did not work with Windows 8.  Which did answer the original question.
I got some value from Boyd's post and it is much appreciated.
eghtebas, Thank you. I look forward to assisting you in the future.
no worries cheers
I figured out the solution.