2017-03-03 9 views
2

だから、私はExcelシート上のコマンドボタンをPDFにExcelのシートを変換するには、このシンプルで小さなコードを持っている:エクセルPDF問題

Sub Save_Excel_As_PDF() 

    ActiveSheet.ExportAsFixedFormat _ 
        Type:=xlTypePDF 

End Sub 

問題は、私は手動で最初の段階を経る必要があるということです最初の手順を実行した後、ボタンが機能するように(Save as、PDFなど)をクリックします。

私はこれをどこにでも保存して、ボタンをクリックして最初の手動手順をすべて実行することなくPDFを作成したかったのです。これを行うためにこのコードを修正できますか?

+0

'ActiveSheet.ExportAsFixedFormat xlTypePDF'を実行すると、手動で何もしないように求められます。私は出力*がどこに行くか分かりませんが、エラーや警告、または実行時間を中断させるものはありません。具体的に何をしようとしていますか? –

答えて

3

FileNameパラメータを指定せずに、PDFはあなたのDocumentsフォルダに保存されます。あるフォルダでマニュアルSave Asを実行した後、次回は同じフォルダに作成されます。

あなたはFileNameパラメータを指定することで、ワークシートの名前と同じ名前で、あなたのワークブックと同じフォルダにファイルを作成することができ、すべてでこれを必要といけない:

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _ 
    FileName:=ThisWorkbook.Path & "\" & ActiveSheet.name 

あなたは別のものを指定することができます名前または他のフォルダThisWorkbook.Pathより。

+1

AWESOME!ありがとうございました! – Lucho

+0

@ルチョあなたは歓迎です:) –

1

が、これは私の作品を推測:

Sub Macro1() 

ChDir "C:\Users\Shyamsundar.Shankar\Desktop" 
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="C:\Users\Shyamsundar.Shankar\Desktop\Sheet1.pdf", Quality:=xlQualityStandard 

End Sub 
0

このスクリプトでは、すべてのExcelファイルをPDFファイルに変換します。

Sub Convert_Excel_To_PDF() 

    Dim MyPath As String, FilesInPath As String 
    Dim MyFiles() As String, Fnum As Long 
    Dim mybook As Workbook 
    Dim CalcMode As Long 
    Dim sh As Worksheet 
    Dim ErrorYes As Boolean 
    Dim LPosition As Integer 

    'Fill in the path\folder where the Excel files are 
    MyPath = "c:\Users\yourpath_here\" 

    FilesInPath = Dir(MyPath & "*.xl*") 
    If FilesInPath = "" Then 
     MsgBox "No files found" 
     Exit Sub 
    End If 

    Fnum = 0 
    Do While FilesInPath <> "" 
     Fnum = Fnum + 1 
     ReDim Preserve MyFiles(1 To Fnum) 
     MyFiles(Fnum) = FilesInPath 
     FilesInPath = Dir() 
    Loop 

    With Application 
     CalcMode = .Calculation 
     .Calculation = xlCalculationManual 
     .ScreenUpdating = False 
     .EnableEvents = False 
    End With 

    If Fnum > 0 Then 
     For Fnum = LBound(MyFiles) To UBound(MyFiles) 
      Set mybook = Nothing 
      On Error Resume Next 
      Set mybook = Workbooks.Open(MyPath & MyFiles(Fnum)) 
      On Error GoTo 0 

      If Not mybook Is Nothing Then 

       LPosition = InStr(1, mybook.Name, ".") - 1 
       mybookname = Left(mybook.Name, LPosition) 
       mybook.Activate 

       'All PDF Files get saved in the directory below: 
       ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= 
        "C:\Users\your_path_here\" & mybookname & ".pdf", 
        Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _ 
        :=False, OpenAfterPublish:=False 

      End If 

      mybook.Close SaveChanges:=False 

     Next Fnum 
    End If 

    If ErrorYes = True Then 
     MsgBox "There are problems in one or more files, possible problem:" _ 
      & vbNewLine & "protected workbook/sheet or a sheet/range that not exist" 
    End If 

    With Application 
     .ScreenUpdating = True 
     .EnableEvents = True 
     .Calculation = CalcMode 
    End With 

End Sub 
関連する問題