返回列表 上一主題 發帖

寫一個另存新檔的巨集,但是需要.pdf file,那麼應怎改寫?

回復 4# Blade
試試看
  1. Option Explicit
  2. Sub PrintPDF()
  3.     Dim File_Name As String, xFile As String, xName As String
  4.     xFile = Range("D6")
  5.     xName = Range("M7")
  6.     File_Name = xFile & "-" & xName & ".pdf"
  7.     Do
  8.         File_Name = InputBox("另存新檔", "[檔案存檔]", File_Name)
  9.         If File_Name = "" Then
  10.             Exit Sub
  11.         Else
  12.             If Dir(File_Name) <> "" Then
  13.                 If MsgBox("檔案名稱經存在,覆蓋它", vbYesNo) = vbYes Then
  14.                     Exit Do
  15.                 Else
  16.                     File_Name = ""
  17.                 End If
  18.             End If
  19.         End If        
  20.     Loop While Not UCase(File_Name) Like "*.PDF"
  21.    Application.DisplayAlerts = False
  22.    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
  23.         xFile & "-" & xName & ".pdf", Quality:=xlQualityStandard, _
  24.         IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
  25.     Application.DisplayAlerts = True
  26. End Sub
複製代碼

TOP

回復 6# Blade
有何問題嗎?

TOP

回復 8# Blade
Name ,File  是VBA使用的關鍵字  變數,程序名稱要避免使用
  1. Option Explicit
  2. Sub PrintPDF()
  3.     Dim File_Name As String, xFile As String, xName As String
  4.     xFile = Range("D6")
  5.     xName = Range("M7")
  6.     File_Name = xFile & "-" & xName & ".xlsm"
  7.     'File_Name = xFile & "-" & xName & ".pdf"
  8.     Do
  9.         File_Name = InputBox("另存新檔", "[檔案存檔]", File_Name)
  10.         If File_Name = "" Then
  11.             Exit Sub
  12.         Else
  13.             If Dir(File_Name) <> "" Then
  14.                 If MsgBox("檔案名稱經存在,覆蓋它", vbYesNo) = vbYes Then
  15.                     Exit Do
  16.                 Else
  17.                     File_Name = ""
  18.                 End If
  19.             End If
  20.         End If
  21.     'Loop While Not UCase(File_Name) Like "*.XLSM"   'UCase(File_Name) 大寫 *.XLSM
  22.     Loop While Not LCase(File_Name) Like "*.xlsm"   'LCase(File_Name) 小寫 *.xlsm
  23.     Application.DisplayAlerts = False
  24.     ActiveWorkbook.SaveAs Filename:=File_Name
  25.     File_Name = Replace(LCase(File_Name), "*.xlsm", ".dbf")  '副檔名替換為 "dbf"
  26.     ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
  27.         xFile & "-" & xName & ".pdf", Quality:=xlQualityStandard, _
  28.         IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
  29.     Application.DisplayAlerts = True
  30. End Sub
複製代碼

TOP

回復 10# Blade
每次發單時,另存後都是格式 INV12345_會員編號1_客戶名稱1.pdf
到了下一張單時,她又忘了更改INV12345,因此會出現 INV12345_會員編號2_客戶名稱2.pdf
請問:為何會自動加1??


  1. Option Explicit
  2. Sub Ex()
  3.     ChDrive "C:\"            '轉換使用中的磁碟機
  4.     ChDir "C:\test"          '改變使用中目錄或檔案夾。
  5.     ChDir "D:\test"          '改變非使中磁碟機目錄或檔案夾
  6.     MsgBox CurDir            '傳回使用中磁碟機:的的路徑
  7.     ChDrive "D:\"            '轉換使用中的磁碟機
  8.     MsgBox CurDir            '傳回使用中磁碟機:的的路徑
  9.     '** 如這樣使用中的磁碟機不是d
  10.     If Mid(CurDir, 1, 1) <> "d" Then ChDrive "d:\"
  11.     ChDir "d:\Account book\INV\"
  12.    
  13.    
  14.    ' **** 或是加上路徑****
  15.     ActiveWorkbook.SaveAs Filename:="d:\Account book\INV\" & File_Name

  16.     File_Name = Replace(LCase(File_Name), "*.xlsm", ".dbf")  '副檔名替換為 "dbf"

  17.     ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
  18.         "d:\Account book\INV\" & File_Name, Quality:=xlQualityStandard, _
  19.         IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
  20. '    ***************
  21. End Sub
複製代碼

TOP

回復 12# Blade
  1. If Dir("存放資料夾全部路徑\" & "[發票編號]" & "*.pdf ") <> "" Then
  2.         MsgBox "發票編號 已開出"
  3.         Exit Sub
  4.     End If
複製代碼

TOP

本帖最後由 GBKEE 於 2012-12-22 07:19 編輯

回復 14# Blade
  1. If Dir("d:\Account book\INV\*" &  xSNo  & "*.pdf ") <> "" Then
複製代碼

TOP

本帖最後由 GBKEE 於 2012-12-22 07:20 編輯

回復 16# Blade
  1.      If Dir("d:\Account book\INV\*" & xSNo  & "*.pdf ")<> "" Then
  2.      MsgBox "發票編號   "& xSNo&"   已開出"
  3.     Exit Sub
複製代碼

TOP

回復 18# Blade
上傳存檔這工作表,是程式碼 看看

TOP

回復 20# Blade
是17#的程式碼有誤 已修正

TOP

回復 22# Blade
20#  If Dir("d:\Account book\INV\*" & " xSNo " & "*.pdf ") <> "" Then
多了兩個"   ,   " xSNo " 為字串-> "d:\Account book\INV\* xSNo  *.pdf "  

正確:
If Dir("d:\Account book\INV\*" & xSNo & "*.pdf ") <> "" Then
如 xSNo="test"
字串="d:\Account book\INV\*" & "test"& "*.pdf "

TOP

        靜思自在 : 犯錯出懺悔心,才能清淨無煩惱。
返回列表 上一主題