在多选列表框中为每个名称访问VBA输出PDF

时间:2017-09-21 17:38:03

标签: vba ms-access

我正在尝试为ListBox中的每个名称输出PDF格式的报告。每个报告PDF都必须使用列表中的名称进行标记。

目前,代码将创建第一个PDF并正确命名。之后,下一个报告无法加载,PDF文件名将变为 Name1Name2

更新:EmployeeNameList是我在表单中构建的多选列表框。我没有收到任何错误,但我没有通过我选择的名称编制索引。例如,如果选择了史密斯,约翰和琼斯,亚当,那么就会发生这种情况。生成的PDF标题为“Smith,John 2017 Target Statement”,生成的下一个报告是“Smith,JohnJones,Adam 2017 Target Statement”。为这个令人困惑的问题道歉。

我希望史密斯,约翰和琼斯,亚当成为独立的PDF。

已解决:已调试并进行了以下更改。

strWhere = "'" & ctl.ItemData(varItem) & "'" & ","

谢谢!

Private Sub CreateTargetPDF_Click()

   Dim strWhere As String
   Dim ctl As Control
   Dim varItem As Variant
   Dim ReportName As String
   Dim counter As Byte

   StartingFolder = BrowseForFolder("\\Documents")

   'confirm selection
   If Me.EmployeeNameList.ItemsSelected.Count = 0 Then
      MsgBox "Must select at least 1 Employee"
      Exit Sub
   End If

   'add selected values to string
   Set ctl = Me.EmployeeNameList
   For Each varItem In ctl.ItemsSelected

      strWhere = strWhere & "'" & ctl.ItemData(varItem) & "'" & ","
      'trim trailing comma and '
      strWhere = Left(strWhere, Len(strWhere) - 1)
      ReportName = Replace(strWhere, "'", "")

      'Select report based on Comp Title
      Role = CompTitleList.Value
      ReportFormat = "Rpt_Target" & Role

      'open report
      DoCmd.OpenReport ReportFormat, acViewPreview, , "[Emp_FullName] IN(" & strWhere & ")"

      'print report
      DoCmd.OutputTo acOutputReport, ReportFormat, acFormatPDF, StartingFolder & "/" & ReportName & " - 2017 " & Role & " Target Statement" & ".pdf", False

      'Close Report
      DoCmd.Close acReport, ReportFormat

   Next varItem

End Sub

0 个答案:

没有答案