寫一個另存新檔的巨集,但是需要.pdf file,那麼應怎改寫?
- 帖子
- 5923
- 主題
- 13
- 精華
- 1
- 積分
- 5986
- 點名
- 0
- 作業系統
- win10
- 軟體版本
- Office 2010
- 閱讀權限
- 150
- 性別
- 男
- 來自
- 台灣基隆
- 註冊時間
- 2010-5-1
- 最後登錄
- 2022-1-23
        
|
回復 4# Blade
試試看- Option Explicit
- Sub PrintPDF()
- Dim File_Name As String, xFile As String, xName As String
- xFile = Range("D6")
- xName = Range("M7")
- File_Name = xFile & "-" & xName & ".pdf"
- Do
- File_Name = InputBox("另存新檔", "[檔案存檔]", File_Name)
- If File_Name = "" Then
- Exit Sub
- Else
- If Dir(File_Name) <> "" Then
- If MsgBox("檔案名稱經存在,覆蓋它", vbYesNo) = vbYes Then
- Exit Do
- Else
- File_Name = ""
- End If
- End If
- End If
- Loop While Not UCase(File_Name) Like "*.PDF"
- Application.DisplayAlerts = False
- ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
- xFile & "-" & xName & ".pdf", Quality:=xlQualityStandard, _
- IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
- Application.DisplayAlerts = True
- End Sub
複製代碼 |
|
|
|
|
|
|
|
- 帖子
- 5923
- 主題
- 13
- 精華
- 1
- 積分
- 5986
- 點名
- 0
- 作業系統
- win10
- 軟體版本
- Office 2010
- 閱讀權限
- 150
- 性別
- 男
- 來自
- 台灣基隆
- 註冊時間
- 2010-5-1
- 最後登錄
- 2022-1-23
        
|
回復 8# Blade
Name ,File 是VBA使用的關鍵字 變數,程序名稱要避免使用- Option Explicit
- Sub PrintPDF()
- Dim File_Name As String, xFile As String, xName As String
- xFile = Range("D6")
- xName = Range("M7")
- File_Name = xFile & "-" & xName & ".xlsm"
- 'File_Name = xFile & "-" & xName & ".pdf"
- Do
- File_Name = InputBox("另存新檔", "[檔案存檔]", File_Name)
- If File_Name = "" Then
- Exit Sub
- Else
- If Dir(File_Name) <> "" Then
- If MsgBox("檔案名稱經存在,覆蓋它", vbYesNo) = vbYes Then
- Exit Do
- Else
- File_Name = ""
- End If
- End If
- End If
- 'Loop While Not UCase(File_Name) Like "*.XLSM" 'UCase(File_Name) 大寫 *.XLSM
- Loop While Not LCase(File_Name) Like "*.xlsm" 'LCase(File_Name) 小寫 *.xlsm
- Application.DisplayAlerts = False
- ActiveWorkbook.SaveAs Filename:=File_Name
- File_Name = Replace(LCase(File_Name), "*.xlsm", ".dbf") '副檔名替換為 "dbf"
- ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
- xFile & "-" & xName & ".pdf", Quality:=xlQualityStandard, _
- IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
- Application.DisplayAlerts = True
- End Sub
複製代碼 |
|
|
|
|
|
|
|