2017-05-10 8 views
1

ワークブック内のすべてのワークシートをループし、ワークブックと同じパスに個別のpdfsとして保存します。ファイルの名前はワークシート名の名前になります。各ワークシートを個別のPDFとして保存

以下のコードは、 "wsA.ExportAsFixedFort"行まで機能します。私が取得エラーメッセージは次のとおりです。

Run-time error '91': Object variable or With block variable not set 

が、私は問題が何であるかを把握カント...

任意の提案ですか?

Option Explicit 

Sub WorksheetLoop() 

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 
Dim WS_Count As Integer 
Dim I As Integer 

' Set WS_Count equal to the number of worksheets in the active workbook. 
Set wbA = ActiveWorkbook 
WS_Count = wbA.Worksheets.Count 
strPath = wbA.Path 
strTime = Format(Now(), "yyyymmdd\_hhmm") 

'get active workbook folder, if saved 
strPath = wbA.Path 
If strPath = "" Then 
    strPath = Application.DefaultFilePath 
End If 
strPath = strPath & "\" 

' Begin the loop. 
For I = 1 To WS_Count 

    'replace spaces and periods in sheet name 
    strName = Replace(wbA.Worksheets(I).Name, " ", "") 
    strName = Replace(strName, ".", "_") 

    'create default name for savng file 
    strFile = strName & "_" & strTime & ".pdf" 
    myFile = strPath & strFile 

    Debug.Print myFile 

    '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 

Next I 

End Sub 

答えて

1

次のように変更してください:

If myFile <> "False" Then 
      ActiveSheet.ExportAsFixedFormat _ 
        Type:=xlTypePDF, _ 
        FileName:=myFile, _ 
        Quality:=xlQualityStandard, _ 
        IncludeDocProperties:=True, _ 
        IgnorePrintAreas:=False, _ 
        OpenAfterPublish:=False 

次に、どのようにその些細なVBAタスクシート、をループへの道を探してみてください。 一般に、コードwsAは設定されていませんでした。したがって、エラーが発生しました。

+1

優秀、時間と指導に感謝します。それは今働く。 –

+0

@ Boosted_d16 - あなたは歓迎です:) – Vityata

関連する問題