延續VBA / 透過Excel VBA下載在GoogleCharts製作QrCode的圖檔
進一步將下載的圖檔轉換成PDF
並且使用FileDialog物件
來設定資料選取對話視窗來選擇要存檔的位置
而轉換成PDF的方式是參考網路的教學
在VBA的使用WScript.Shell 執行PDFCreator的images2pdfc.exe
或者用內建的Shell來執行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
Public Sub downloadPic() '課程代碼 courseId = InputBox("請輸入課程代碼") courseName = InputBox("請輸入課程名稱") 'QR Code 路徑 myURL = "https://chart.googleapis.com/chart?chs=120x120&cht=qr&chld=M|3&chl=https://inservice.edu.tw/NAPP/CourseView.aspx?cid=" & courseId '工作簿所在路徑 strSavePath = ThisWorkbook.path '設定FileDialog物件 Dim myFolder As FileDialog '選取資料夾對話窗 msoFileDialogFolderPicker Set myFolder = Application.FileDialog(msoFileDialogFolderPicker) '設定初始路徑 myFolder.InitialFileName = "%UserProfile%\Desktop\" '建立 XMLHTTP物件來連線 Set WinHttpReq = CreateObject("Microsoft.XMLHTTP") 'WinHttpReq.Open "GET", myURL, False, "username", "password" WinHttpReq.Open "GET", myURL, False WinHttpReq.send If WinHttpReq.Status = 200 Then Set oStream = CreateObject("ADODB.Stream") oStream.Open oStream.Type = 1 oStream.Write WinHttpReq.responseBody oStream.SaveToFile strSavePath & "\" & courseName & ".png", 2 ' 1 = 不複寫, 2 = 複寫 oStream.Close myFolder.Show myFolder.SelectedItems.Item (1) Dim cmd01 As String '路徑要用字串形式-前後用雙引號包住 不然有中文 或空白 會產生錯誤 'cmd01 = """" & strSavePath & "\images2pdfc\images2pdfc.exe" & """" & " /i """ & strSavePath & "\" & courseName & ".png""" & " /e """ & strSavePath & "\" & courseName & ".pdf""" cmd01 = """" & strSavePath & "\images2pdfc\images2pdfc.exe" & """" & " /i """ & strSavePath & "\" & courseName & ".png""" & " /e """ & myFolder.SelectedItems.Item(1) & "\" & courseName & ".pdf""" '自訂的RunCmd Call RunCmd(cmd01, 1, True) Debug.Print cmd01 Dim cmd02 As String 'cmd02 = """" & strSavePath & "\images2pdfc\images2pdfc.exe" & """" & " /i """ & strSavePath & "\" & courseName & ".png""" & " /e """ & strSavePath & "\" & courseName & ".pdf""" cmd02 = """" & strSavePath & "\images2pdfc\images2pdfc.exe" & """" & " /i """ & strSavePath & "\" & courseName & ".png""" & " /e """ & myFolder.SelectedItems.Item(1) & "\" & courseName & ".pdf""" 'excel內建的Shell 'Call Shell(cmd02, vbHide) 'Debug.Print cmd02 MsgBox "下載成功" End If End Sub |
自訂的RunCmd(),直接引用網路的教學範例,但是將參數順序調整跟WScript.Shell.Run()一致
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
Function RunCmd(strCMD As String, Optional windowStyle As Integer = 1, Optional waitOnReturn As Boolean = True) '若無法執行,須引用 "Windows Script Host Object Model" ' (工具 > 設定引用項目 >勾選) ' 使用 WScript.Shell 方式 ' 參數: ' strCMD 執行字串 ' windowStyle 視窗樣式,1為顯示 0不顯示 ' waitOnReturn 是否等待返回 Dim wsh As Object Set wsh = VBA.CreateObject("WScript.Shell") Dim errorCode As Integer On Error GoTo ErrZone errorCode = wsh.Run(strCMD, windowStyle, waitOnReturn) If errorCode = 0 Then ' MsgBox "OK!" Else MsgBox "執行錯誤" & vbCrLf & "代碼:" & errorCode & vbCrLf & "執行程式:" & strCMD Exit Function End If Exit Function ErrZone: MsgBox "WScript.Shell發生錯誤:" & vbCrLf & Err.Number & ":" & Err.Description Resume Next End Function |
#11 ~ 16 跟 #34 ~ 35
FileDialog物件的相關設定
其中預設的檔案選取對話視窗設定在桌面 %UserProfile%\Desktop\
#37 ~ 45
設定傳入 WScript.Shell的路徑字串
#47 ~ 53
設定傳入 Shell的路徑字串
必須要注意的是傳入的路徑字串必須前後用雙引號包住
也就是傳入這樣格式的字串 “images2pdfc路徑“ /i “圖檔路徑“ /e “存檔路徑“
因此要注意字串的連結,哪個引號是要一起傳入的符號,哪個是表示字串變數用的
同時也要留意字串內有無空格都是有意義的
這樣才能避免因為檔案路徑中有中文或者空格所造成的錯誤
例如:
cmd02 = """" & strSavePath & "\images2pdfc\images2pdfc.exe" & """" & " /i """ & strSavePath & "\" & courseName & ".png""" & " /e """ & myFolder.SelectedItems.Item(1) & "\" & courseName & ".pdf"""
轉譯輸出之後會是
"工作簿所在的資料夾\images2pdfc\images2pdfc.exe" /i "工作簿所在的資料夾/下載的圖檔.png" /e "存檔的資料夾/轉存PDF.pdf"