2017-06-29 3 views
0

コードがうまく動作し、ワークシートからPDFを作成しています。しかし、ファイルパスの保存ラジオボタンを押さなくても、これを実行したいと思っています。 それとも、私は正確なファイル名を作成し、実行時に正確なファイル名を作成して、例えばのために保存を選択せず​​にPDFを作成するときに保存ボタンを自動的に選択する方法

enter image description here

Dim wsA As Worksheet 
    Dim wbA As Workbook 
    Dim strTime As String 
    Dim strName As String 
    Dim strPath As String 
    Dim strFile As String 
    Dim strPathFile As String 
    Dim myFile As Variant 
    On Error GoTo errHandler 

    Set wbA = ActiveWorkbook 
    Set wsA = ActiveSheet 
    strTime = Format(Now(), "ddmmyyyy\_hhmm") 

    strPath = wbA.Path 
    If strPath = "" Then 
     strPath = Application.DefaultFilePath 
    End If 
    strPath = strPath & "\" 

    strName = Replace(wsA.Name, " ", "") 
    strName = Replace(strName, ".", "_") 

    strFile = strName & "_" & strTime & ".pdf" 
    strPathFile = strPath & strFile 

    myFile = Application.GetSaveAsFilename _ 
     (InitialFileName:=strPathFile, _ 
      FileFilter:="PDF Files (*.pdf), *.pdf", _ 
      Title:="Select Folder and FileName to save") 

    'export to PDF if a folder was selected 
    If myFile <> "False" Then 
     wsA.ExportAsFixedFormat _ 
      Type:=xlTypePDF, _ 
      Filename:=myFile, _ 
      Quality:=xlQualityStandard, _ 
      IncludeDocProperties:=True, _ 
      IgnorePrintAreas:=False, _ 
      OpenAfterPublish:=False _ 

     'confirmation message with file info 
     MsgBox "PDF file has been created: " _ 
      & vbCrLf _ 
      & myFile 
    End If 

exitHandler: 
    CommandButton2.Visible = True 
    CommandButton1.Visible = False  
    Exit Sub 

errHandler: 
    MsgBox "Could not create PDF file" 
    Resume exitHandler 
End Sub 
+0

保存ダイアログを表示しない場合は、 'ExportAsFixedFormat'の' Filename:= myFileWithFullPath'でフルパスを指定する必要があります。したがって、選択せずに直接ファイルを保存します。ダイアログでパスを選択する必要がある場合は、自分で保存ボタンを押す必要があります。 –

答えて

1

を保存する必要があるということです: -

ActiveSheet.ExportAsFixedFormat _ 
Type:=xlTypePDF _ 
Filename:= destfolder & PDF_Filename _ 
Quality:=xlQualityStandard _ 
IncludeDocProperties:=True _ 
IgnorePrintAreas:=False _ 
OpenAfterPublish:=True 
+0

しかし、@ answerはこの答えで 'ActiveSheet'の代わりに' wsA'を使うべきです。アクティブなワークシートを 'wsA'として最初に定義することは、非常に良い決定でした。 –

+1

@Peh ...はい、activesheetで私は同じ意味...私はちょうどサンプルコードとしてそれを共有... – mohanish

1

あなたは次のように試すこともできます:

Application.SendKeys("%s") 

上記のkここでアクティブなアプリケーションへの入力は、保存のショートカットであるAlt + Sを意味します。

関連する問題