生成 PDF 报告的 VBA 代码

Posted

技术标签:

【中文标题】生成 PDF 报告的 VBA 代码【英文标题】:VBA Code to Produce PDF Reports 【发布时间】:2018-01-18 16:02:40 【问题描述】:

您好,我正在学习访问和 VBA。最近创建了一份报告 (Report1),其中显示了单个课程的调查 cmets。此报告的标准是[Course]。我还创建了一个包含课程列表 (Course List) 的查询,可以循环访问这些课程以生成每门课程的报告。我正在寻找一些可以将这两者链接在一起并为每门课程创建单独的 pdf 的基本代码。

这是我目前所拥有的:

Private Sub GetCourseName()
Dim rst As Recordset
Dim db As Database
Dim strSQL As String

Set db = CurrentDb()
Set rst = db.OpenRecordset("SELECT [Course] FROM [Course List]")

rst.MoveFirst
Do Until rst.EOF
DoCmd.OpenReport "rptReport1", acViewPreview, , "Course = " & rst!Course
  DoCmd.OutputTo acOutputReport, "rptReport1", acFormatPDF, "C:\Users\raj.jk\Documents\Course Evaluations" & rst!Course & ".pdf"

DoCmd.Close acReport, "rptReport1"
rst.MoveNext
Loop
rst.Close
Set rst = Nothing
strSQL = ""
End Sub

【问题讨论】:

该代码有什么问题,您的问题是什么?有用吗? 问题是代码只是打开报告,并没有真正填写查询中的条件或保存任何内容。我认为这行代码主要是问题 DoCmd.OpenReport "Report1", acViewPreview, , Course = rst!Course 【参考方案1】:

推测Course是一个文本值(而不是数值),所以需要在过滤表达式中用单引号括起来:

"Course = '" & rst!Course & "'"

或嵌入双引号:

"Course = """ & rst!Course & """"

【讨论】:

谢谢!我认为这有助于解决一个问题,但另一个问题是由于某种原因代码没有将参数传递给报告,因为我仍然被提示输入课程。【参考方案2】:

我猜你的报告设计有问题。这不是一项容易解决的任务,但也许导出报告的设计会很有帮助。看看这个声明Application.SaveAsText acReport, "rptReport1", "C:\rptReport1_design_exported_as_text.txt"

我试图重新创建您的数据库。我使用了这些说明:



Private Sub CodeToProduceTextReports__01()
' create table and insert some testing values
    CurrentDb.Execute "CREATE TABLE [Course List] (CourseID autoincrement, Course varchar(50), Course_Title varchar(50), Course_Location varchar(50))"
    CurrentDb.Execute "INSERT INTO [Course] (Course, Course_Title, Course_Location) VALUES " & _
            "('alfa', 'first Greek lettre', 'aa')"
    CurrentDb.Execute "INSERT INTO [Course] (Course, Course_Title, Course_Location) VALUES " & _
            "('beta', 'second Greek lettre', 'bb')"
    CurrentDb.Execute "INSERT INTO [Course] (Course, Course_Title, Course_Location) VALUES " & _
            "('gamma', 'thirth Greek lettre', 'cc')"
End Sub
Private Sub CodeToProduceTextReports__02()
' importing the design from the report from a saved version in text format
    Application.LoadFromText acReport, "rptReport1", "C:\rptReport1_design_exported_as_text.txt"
End Sub
Private Sub CodeToProduceTextReports__03()
' --03-- print the displayed data of the imported report to a text-file
' for my testings I needed to switch from PDF to TXT
    DoCmd.OutputTo acReport, "rptReport1", acFormatTXT , "C:\Data Displayed By Report.txt", False, ""
End Sub
Private Sub CodeToProduceTextReports__04()
' --04-- export the design of the report to a text-file
    Application.SaveAsText acReport, "rptReport1", "C:\design of report in text format.txt"
End Sub
Private Sub CodeToProduceTextReports__05()
' --05-- routine to print a file for each course
    ...
    rst.MoveFirst
    Do Until rst.EOF
        DoCmd.OpenReport "rptReport1", acViewPreview, , "Course = '" & rst!Course & "'"
        ' for my testings I needed to switch from PDF to TXT
        DoCmd.OutputTo acOutputReport, "rptReport1", acFormatTXT, "Course Evaluations" & rst!Course & ".txt"
        DoCmd.Close acReport, "rptReport1"
        rst.MoveNext
    Loop
    ...
End Sub

要执行这些指令,您需要文件"C:\rptReport1_design_exported_as_text.txt" 的内容。


Version =19VersionRequired =19Checksum =-3122728Begin Report    LayoutForPrint = NotDefault
    AllowDesignChanges = NotDefault    DefaultView =0
    TabularFamily =0
    DateGrouping =1
    GrpKeepTogether =1
    PictureAlignment =2
    DatasheetGridlinesBehavior =3
    GridY =10
    Width =5669
    ItemSuffix =3
    Left =270
    Top =600
    Right =7725
    Bottom =7140
    DatasheetGridlinesColor =12632256
    RecSrcDt = Begin
        0x8d11b150f50de540
    End
    GUID = Begin
        0xebeaeb4b353b1e41965fcce57dee6850
    End
    NameMap = Begin
        0x0acc0e5500000000b21f4d48d7501c47975fad457306950f0000000006bb61eb ,
        0x3f0ee540000000000000000043006f00750072007300650020004c0069007300 ,
        0x7400000000000000a1f51f8cd9d88f40b7d811fdc68024a207000000b21f4d48 ,
        0xd7501c47975fad457306950f43006f00750072007300650000000000000030e5 ,
        0x8a48064ac944ba5f2d69ec0cfe2507000000b21f4d48d7501c47975fad457306 ,
        0x950f43006f0075007200730065005f005400690074006c006500000000000000 ,
        0x3f6cd73810d7ed4dacc51df5fba38c4a07000000b21f4d48d7501c47975fad45 ,
        0x7306950f43006f0075007200730065005f004c006f0063006100740069006f00 ,
        0x6e00000000000000000000000000000000000000000000000c00000002000000 ,
        0x0000000000000000000000000000
    End
    RecordSource ="Course List"
    DatasheetFontName ="Arial Narrow"
    PrtMip = Begin
        0xae050000ae050000ae050000ae0500000000000025160000a506000001000000 ,
        0x010000006801000000000000a10700000100000001000000
    End
    PrtDevMode = Begin
        0x0000000000000000000000000000000000000000000000000000000000000000 ,
        0x010400049c009000032f000002000900000000006400010001002c0101000100 ,
        0x2c01010000004c65747465720000000000000000000000000000000000000000 ,
        0x0000000000000000000000000000000000000000000000000000000000000000 ,
        0x000000000000000000000000000000000000000000000000000000007769646d ,
        0x10000000010000000000000000000000fe00000001000000000000002c010000 ,
        0x0000000000000000000000000000000000000000000000000000000000000000 ,
        0x0000000000000000000000000000000000000000000000000000000000000000 ,
        0x0000000000000000000000000000000000000000000000000000000000000000 ,
        0x000000000000000000000000
    End
    PrtDevNames = Begin
        0x080036005d000100000000000000000000000000000000000000000000000000 ,
        0x0000000000000000000000000000000000000000000000000000000000000000 ,
        0x00000000000000000000000000000000000000000000000000000000004d6963 ,
        0x726f736f667420446f63756d656e7420496d6167696e67205772697465722050 ,
        0x6f72743a00
    End
    Begin
        Begin Label
            BackStyle =0
            TextFontFamily =2
            FontName ="Arial"
        End
        Begin TextBox
            FELineBreak = NotDefault
            OldBorderStyle =0
            TextFontFamily =2
            Width =1701
            LabelX =-1701
            FontName ="Arial"
        End
        Begin Section
            KeepTogether = NotDefault
            Height =1701
            Name ="Details"
            GUID = Begin
                0xbbf704242af93b45b77934d4cf2c2e95
            End
            Begin
                Begin TextBox
                    IMESentenceMode =3
                    Left =1870
                    Top =113
                    Name ="Course"
                    ControlSource ="Course"
                    GUID = Begin
                        0xc8704b4483a48440b68813fa30cc9d70
                    End
                    Begin
                        Begin Label
                            Left =169
                            Top =113
                            Width =630
                            Height =225
                            Name ="Bijschrift0"
                            Caption ="Course:"
                            GUID = Begin
                                0xb620b2a6a8332d42a52b1dc58425b1be
                            End
                        End
                    End
                End
                Begin TextBox
                    IMESentenceMode =3
                    Left =2097
                    Top =453
                    TabIndex =1
                    Name ="Course_Title"
                    ControlSource ="Course_Title"
                    GUID = Begin
                        0xd33ee5134f828846ad63db31ed83c4e4
                    End
                    Begin
                        Begin Label
                            Left =396
                            Top =453
                            Width =1005
                            Height =225
                            Name ="Bijschrift1"
                            Caption ="Course_Title:"
                            GUID = Begin
                                0x44b12ea59ae514488e333d8451fbf68a
                            End
                        End
                    End
                End
                Begin TextBox
                    IMESentenceMode =3
                    Left =2381
                    Top =850
                    TabIndex =2
                    Name ="Course_Location"
                    ControlSource ="Course_Location"
                    GUID = Begin
                        0x139fdfe9aad7034aa7ad2f137e63e486
                    End
                    Begin
                        Begin Label
                            Left =680
                            Top =850
                            Width =1290
                            Height =225
                            Name ="Bijschrift2"
                            Caption ="Course_Location:"
                            GUID = Begin
                                0x8f36ef586867bb4faf9cbc80efd0a570
                            End
                        End
                    End
                End
            End
        End
    End
End

【讨论】:

以上是关于生成 PDF 报告的 VBA 代码的主要内容,如果未能解决你的问题,请参考以下文章

从 Excel VBA 生成包含表单域的 PDF

如何使用 angular9 和 Jasmine 为所有组件生成 .pdf 格式的单元测试用例代码覆盖率报告

VBA MS 项目将多个自定义报告以 PDF 格式保存到位置

根据查询中的每条记录将报告打印为 PDF

访问报告打印输出(或 PDF)缺少打印预览中可见的信息

python使用fpdf生成数据报告pdf文件