2017-05-10 5 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