如何在MS Access中的shell语句中运行以下命令?
"C:\Program Files (x86)\Microsoft Office\Office14\msaccess.exe" "http://stoneplastics/Departments/Quality/Databases/LabelsNewRelease.accdb" /runtime
它在批处理文件中正常工作。 我已经尝试了3个小时让它工作,我遇到了麻烦
答案 0 :(得分:2)
由于您将在Access会话中运行VBA代码,因此您可以使用SysCmd(acSysCmdAccessDir)
来确定MSACCESS.EXE
所在的文件夹。这意味着您不必为多个Access版本和32对64位路径硬编码文件路径,然后使用Dir()
检查那里是否存在MSACCESS.EXE
。
以下是Access 2010中立即窗口会话的工作示例:
strExe = SysCmd(acSysCmdAccessDir) & "MSACCESS.EXE"
strDb = "C:\share\Access\Database2.accdb"
strCommandLine = """" & strExe & """ """ & strDb & """ /runtime"
Debug.Print strCommandLine
"C:\Program Files (x86)\Microsoft Office\Office14\MSACCESS.EXE" "C:\share\Access\Database2.accdb" /runtime
Shell strCommandLine, vbMaximizedFocus
当您将其作为VBA程序进行调整时,它将比您现在拥有的更简单。它更灵活,因为您无需指定查找MSACCESS.EXE
的位置;这是在运行时自动发现的。
答案 1 :(得分:0)
我明白了。这是我使用的代码。 (请注意我更改了访问数据库文件。
If Not Len(Dir("C:\Program Files\Microsoft Office\Office12\msaccess.exe", vbDirectory)) = 0 Then
Call Shell(Chr$(34) & "C:\Program Files\Microsoft Office\Office12\msaccess.exe" & Chr$(34) & " " & Chr$(34) & "http://stoneplastics/Departments/Quality/Databases/ItemInspectionInstructionPrintOutRev3.accdb" & Chr$(34) & " /runtime", vbMaximizedFocus)
End If
If Not Len(Dir("C:\Program Files\Microsoft Office\Office14\msaccess.exe", vbDirectory)) = 0 Then
Call Shell(Chr$(34) & "C:\Program Files\Microsoft Office\Office14\msaccess.exe" & Chr$(34) & " " & Chr$(34) & "http://stoneplastics/Departments/Quality/Databases/ItemInspectionInstructionPrintOutRev3.accdb" & Chr$(34) & " /runtime", vbMaximizedFocus)
End If
If Not Len(Dir("C:\Program Files\Microsoft Office\Office11\msaccess.exe", vbDirectory)) = 0 Then
Call Shell(Chr$(34) & "C:\Program Files\Microsoft Office\Office11\msaccess.exe" & Chr$(34) & " " & Chr$(34) & "http://stoneplastics/Departments/Quality/Databases/ItemInspectionInstructionPrintOutRev3.accdb" & Chr$(34) & " /runtime", vbMaximizedFocus)
End If
If Not Len(Dir("C:\Program Files\Microsoft Office\Office15\msaccess.exe", vbDirectory)) = 0 Then
Call Shell(Chr$(34) & "C:\Program Files\Microsoft Office\Office15\msaccess.exe" & Chr$(34) & " " & Chr$(34) & "http://stoneplastics/Departments/Quality/Databases/ItemInspectionInstructionPrintOutRev3.accdb" & Chr$(34) & " /runtime", vbMaximizedFocus)
End If
If Not Len(Dir("C:\Program Files (x86)\Microsoft Office\Office14\msaccess.exe", vbDirectory)) = 0 Then
Call Shell(Chr$(34) & "C:\Program Files (x86)\Microsoft Office\Office14\msaccess.exe" & Chr$(34) & " " & Chr$(34) & "http://stoneplastics/Departments/Quality/Databases/ItemInspectionInstructionPrintOutRev3.accdb" & Chr$(34) & " /runtime", vbMaximizedFocus)
End If
If Not Len(Dir("C:\Program Files (x86)\Microsoft Office\Office12\msaccess.exe", vbDirectory)) = 0 Then
Call Shell(Chr$(34) & "C:\Program Files (x86)\Microsoft Office\Office12\msaccess.exe" & Chr$(34) & " " & Chr$(34) & "http://stoneplastics/Departments/Quality/Databases/ItemInspectionInstructionPrintOutRev3.accdb" & Chr$(34) & " /runtime", vbMaximizedFocus)
End If
If Not Len(Dir("C:\Program Files (x86)\Microsoft Office\Office15\msaccess.exe", vbDirectory)) = 0 Then
Call Shell(Chr$(34) & "C:\Program Files (x86)\Microsoft Office\Office15\msaccess.exe" & Chr$(34) & " " & Chr$(34) & "http://stoneplastics/Departments/Quality/Databases/ItemInspectionInstructionPrintOutRev3.accdb" & Chr$(34) & " /runtime", vbMaximizedFocus)
End If
If Not Len(Dir("C:\Program Files (x86)\Microsoft Office\Office11\msaccess.exe", vbDirectory)) = 0 Then
Call Shell(Chr$(34) & "C:\Program Files (x86)\Microsoft Office\Office11\msaccess.exe" & Chr$(34) & " " & Chr$(34) & "http://stoneplastics/Departments/Quality/Databases/ItemInspectionInstructionPrintOutRev3.accdb" & Chr$(34) & " /runtime", vbMaximizedFocus)
End If
If Not Len(Dir("C:\Program Files\Microsoft Office 15\root\office15\msaccess.exe", vbDirectory)) = 0 Then
Call Shell(Chr$(34) & "C:\Program Files\Microsoft Office 15\root\office15\msaccess.exe" & Chr$(34) & " " & Chr$(34) & "http://stoneplastics/Departments/Quality/Databases/ItemInspectionInstructionPrintOutRev3.accdb" & Chr$(34) & " /runtime", vbMaximizedFocus)
End If
答案 2 :(得分:0)
不确定你是怎么想的,它没有工作"当您将命令传递给shell时,如dbmitch的回答帖子,并将其封在引号中,如dbmitch在该帖子中演示,但很高兴你得到它的工作。 :)
顺便说一句,因为找到每个可能的访问路径显然是一个要求,为什么不首先使用Dir来找到它?由于Office16(32位)路径是" \ Program Files(x86)\ Microsoft Office \ root \ Office16 \ MSACCESS.EXE"这将有助于防错和未来证明您的代码。和Office 17可能会有所不同。如果您创建一个Shell对象并调用.Exec
方法,则可以获得Command DIR
函数的输出(而不是VBA' DIR
函数... 2非常不同的野兽!)。这将是Access的路径。这还可以防止用户在默认位置以外的位置安装Access / Office时出现任何错误。
Dim oShell As Object 'New WshShell
Dim cmd As Object 'WshExec
Dim strPath as String 'Path to Access
Const WshRunning = 0
On Error GoTo wshError
Set oShell = CreateObject("Wscript.Shell")
'Note the two double quotes "". This causes VBA to insert one double quote.
'Its a little cleaner than " & Chr$(34) & "
'http://stackoverflow.com/questions/9024724/how-do-i-put-double-quotes-in-a-string-in-vba/38456820
Set cmd = oShell.Exec("cmd /c ""dir c:\*MSACCESS.EXE /a:-d /b /d /s""")
Do While cmd.Status = WshRunning
'Do nothing
DoEvents
Loop
strPath = cmd.StdOut.ReadAll
If strPath="" Then
MsgBox "Access was not found."
Exit Sub
End If
'We now have the path to Access so pass it to shell like you've done
Call Shell("""" & strPath & """" & _
" ""http://stoneplastics/Departments/Quality/Databases/ItemInspectionInstructionPrintOutRev3.accdb"" /runtime" _
, vbMaximizedFocus)
Set oShell = Nothing
Set cmd = Nothing
Exit sub
wshError:
On Error Resume Next
Msgbox cmd.StdErr.ReadAll
Resume Next