Board logo

標題: [發問] 如何VBA不指定軟體開啟特定檔案 [打印本頁]

作者: morris_lth    時間: 2015-12-29 15:59     標題: 如何VBA不指定軟體開啟特定檔案

之所以要這樣問是因為,下述程式中指定"C:\Program Files\Adobe\Reader 8.0\Reader\AcroRD32.exe"
來開啟PDF檔案,但是此excel 檔案是放在公用資料夾中,開檔的人未必使用的軟體或是路徑會是Reader 8.0,
有可能是9.0,有可能路徑不是C:\........

所以才想詢問是否有方式只給檔案路徑,至於開檔軟體的路徑種類依user本身電腦來決定就行

Private Sub CommandButton1_Click()
Shell "C:\Program Files\Adobe\Reader 8.0\Reader\AcroRD32.exe \\fileserver\PUB\QA\11 SIP\SIP\QCS001-MT1097X.pdf", vbNormalFocus
End Sub
作者: stillfish00    時間: 2015-12-30 10:16

回復 1# morris_lth
try this
ActiveWorkbook.FollowHyperlink "\\fileserver\PUB\QA\11 SIP\SIP\QCS001-MT1097X.pdf"
作者: Joforn    時間: 2015-12-30 10:35

  1. #If VBA7 Then
  2.   Private Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteW" (ByVal hWnd As LongPtr, ByVal lpOperation As LongPtr, ByVal lpFile As LongPtr, ByVal lpParameters As LongPtr, Optional ByVal lpDirectory As LongPtr = 0^, Optional ByVal nShowCmd As ShowCM = 1) As Long
  3. #Else
  4.   Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteW" (ByVal hWnd As Long, ByVal lpOperation As Long, ByVal lpFile As Long, ByVal lpParameters As Long, Optional ByVal lpDirectory As Long=0, Optional ByVal nShowCmd As ShowCM = 1) As Long
  5. #End If

  6. Public Enum ShowCM
  7.   SW_SHOWNORMAL = 1&
  8.   SW_SHOWMINIMIZED = 2&
  9.   SW_SHOWMAXIMIZED = 3&
  10.   SW_SHOWNOACTIVATE = 4&
  11.   SW_SHOW = 5&
  12.   SW_SHOWMINNOACTIVE = 7&
  13.   SW_SHOWNA = 8&
  14.   SW_SHOWDEFAULT = 10&
  15. End Enum


  16. Public Function ShellOpenFile(ByVal FileName As String, Optional ByVal ShowMode As ShowCM = 1)
  17.   If Len(FileName) Then
  18.     ShellOpenFile = ShellExecute(Application.hWnd, StrPtr("open"), StrPtr(FileName), 0, , ShowMode)
  19.   End If
  20. End Function

  21. Sub Test()
  22.   ShellOpenFile "\\fileserver\PUB\QA\11 SIP\SIP\QCS001-MT1097X.pdf" '可以是其它的文件名,只要系統能夠識別的文件類型,都會自動以Windows默認程式開啟指定的文件。比如指定的是.TXT文件,將會自動以Notepad.exe開啟
  23.   ShellOpenFile "https://www.facebook.com/profile.php?id=100010420125898", SW_SHOWMAXIMIZED  '開啟網頁
  24.   ShellOpenFile "MailTo:[email protected]" '發送Email到指定的郵箱地址
  25. End Sub
複製代碼

作者: 准提部林    時間: 2015-12-30 10:55

試試:
VBA.CreateObject("WScript.Shell").Run """\\fileserver\PUB\QA\11 SIP\SIP\QCS001-MT1097X.pdf"""
作者: morris_lth    時間: 2016-1-8 16:36

謝謝,大家的方法都不錯




歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)