You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Access通过VBA生成Word项目符号时出现异常行为问题

Access生成Word报表项目符号格式错乱问题

我正在为日常任务的Access数据库开发月度报表生成功能,输出到Word文档,但创建项目符号时出现异常行为。(注:我知晓可直接创建Access报表,但格式不符合需求)

代码逻辑说明

表单上的按钮触发btnCreateReport_Click()过程,其中封装了用于创建段落/项目符号的fnNewPara子过程,以及后续从数据库提取文本数据的fnGetActivities()函数。使用ParaIsSubBullet作为当前行缩进次数的计数器。

异常现象

格式错乱表现为:若传入空字符串或空格,项目符号格式完全正常;但只要给段落添加文本,在第一层子项目符号迭代后就会出现格式偏移。

相关代码

Sub fnNewPara( _
n As Integer, _
ParaText As Variant, _
ParaFont As String, _
ParaFontSize As Integer, _
paraUnderline As Boolean, _
paraBold As Boolean, _
ParaIsBullet As Boolean, _
ParaIsSubBullet As Integer, _
ParaAlign As Integer, _
wdDoc As Object _
)

    wdDoc.Paragraphs.Add
    wdDoc.Paragraphs(n).Range.Text = ParaText & vbNewLine
    wdDoc.Paragraphs(n).Range.Font.Name = ParaFont
    wdDoc.Paragraphs(n).Range.Font.Size = ParaFontSize
    wdDoc.Paragraphs(n).Range.Font.Underline = paraUnderline
    wdDoc.Paragraphs(n).Range.Font.Bold = paraBold
    wdDoc.Paragraphs(n).Format.Alignment = ParaAlign
    
    If ParaIsBullet = True Then
        wdDoc.Paragraphs(n).Range.ListFormat.ApplyBulletDefault
    End If
    
    If ParaIsSubBullet > 0 Then
        Do Until ParaIsSubBullet = 0
            wdDoc.Paragraphs(n).Range.ListFormat.ListIndent
            ParaIsSubBullet = ParaIsSubBullet - 1
        Loop
    End If
    
    ' 注:原代码中ParaIsOutdent未在参数中定义,此处保留原逻辑
    If ParaIsOutdent = True Then
        wdDoc.Paragraphs(n).Range.ListFormat.ListOutdent
    End If

End Sub

Function fnGetActivities() As String
    fnGetActivities = "a" ' 给子子项目符号添加文本会触发异常,该函数本身无问题
End Function

Private Sub btnCreateReport_Click()
 
    Dim wdApp As Object
    Dim wdDoc As Object
    Dim ParaFontSize As Integer
    Dim ParaAlign As Integer
    Dim ParaText As String
    Dim ParaFont As String
    Dim strFont As String
    Dim paraUnderline As Boolean
    Dim paraBold As Boolean
    Dim ParaIsBullet As Boolean
    Dim ParaIsSubBullet As Boolean
    Dim n As Integer
    Dim l As Integer
    Dim s As Integer
    Dim strLocation As Variant
    Dim strSection As Variant
    Dim strYear As String
    Dim strMonth As String
    
    strSection = Array("Section1", "Section2", "Section3")  ' 数组索引从0开始
    strLocation = Array("Location1", "Location2", "Location3")
    
    myFileName = "TestReport.docx"
    myFileDest = Application.CurrentProject.Path & "\output\" & myFileName

    strYear = "2024"
    strMonth = "June"
    strFont = "Times new roman"
    
    Set wdApp = CreateObject("Word.Application")
    wdApp.Visible = True ' 显示Word应用程序
    Set wdDoc = wdApp.Documents.Add
       
    '#########################################################
    
    n = 1
    s = 0
    l = 0

    ' fnNewPara 参数:n, 文本, 字体, 字号, 下划线, 加粗, 是否项目符号, 子项目缩进次数, 对齐方式, Word文档对象
    fnNewPara n, "Monthly Activity Report for " & strMonth & " " & strYear, "Times new roman", 12, True, True, False, 0, 1, wdDoc
    n = n + 1

    Do Until s > 2
        fnNewPara n, strSection(s), "Times new roman", 12, True, True, True, 1, 0, wdDoc
        n = n + 1
        
        Do Until l > 2
            fnNewPara n, strLocation(l), strFont, 12, False, False, True, 2, 0, wdDoc
            n = n + 1
            fnNewPara n, fnGetActivities, strFont, 12, False, False, True, 3, 0, wdDoc
            n = n + 1
            l = l + 1
        Loop
        s = s + 1
        l = 0
    Loop

    '#########################################################
    
    ' 保存文档
    'wdDoc.SaveAs myFileDest
    
    ' 可选:关闭Word应用
    'wdApp.Quit
    
    ' 释放对象
    Set subPara = Nothing
    Set para = Nothing
    Set wdDoc = Nothing
    Set wdApp = Nothing
    
End Sub

内容的提问来源于stack exchange,提问作者Charles

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 04:23:13