Как экспортировать несколько листов в PDF с помощью ListBox в пользовательской форме - PullRequest
0 голосов
/ 26 апреля 2019

Я пытаюсь экспортировать несколько рабочих листов в один PDF-документ, позволяя пользователю выбирать, какие рабочие листы, используя пользовательскую форму.Я написал код ниже, но продолжаю застрять в строке «Sheets (SheetArray ()). Select», и я не могу понять это.У кого-нибудь есть идеи?

Private Sub CommandButton1_Click()

Dim SheetArray() As Variant
Dim indx As Integer

Dim ws As Worksheet
Dim strPath As String
Dim myfile As Variant
Dim strFile As String
Dim sheetstoprint As String

Set ws = ActiveSheet

strFile = Worksheets("Setup").Range("G8").Text & " Proforma" & ".pdf"

strFile = ThisWorkbook.Path & "\" & strFile

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

If myfile <> "False" Then

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    indx = 0
    For i = 0 To ListBox1.ListCount - 1
            If ListBox1.Selected(i) = True Then
                ReDim Preserve SheetArray(indx)
                SheetArray(indx) = Sheets(ListBox1.List(i, 1)).Index
                indx = indx + 1
            End If
    Next i

    If indx > 0 Then

            Sheets(SheetArray()).Select
                ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
                Filename:=myfile, _
                Quality:=xlQualityStandard, _
                IncludeDocProperties:=True, _
                IgnorePrintAreas:=False, _
                OpenAfterPublish:=True


                '.ExportAsFixedFormat Type:=xlTypePDF, Filename:=myfile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True


    End If
End If
exitHandler:
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Exit Sub


Resume exitHandler
End Sub

Private Sub ListBox1_Click()

End Sub

Private Sub UserForm_Initialize()

Dim wks() As Variant


wks = Array("Cover Page", "Proforma (1)", "Proforma (2)", "Proforma (3)", "Proforma (4)", "Expense Analysis", "Assumptions", _
                        "Payroll Schedule", "Expense Comp")

'Debug.Print wks(16)
For i = 0 To UBound(wks)

        ListBox1.AddItem wks(i)
        ListBox1.List(ListBox1.ListCount - 1, 1) = wks(i)

Next i


End Sub

Надеемся получить выбранные рабочие листы от пользовательской формы для правильного экспорта в виде одного PDF-файла.

...