I am creating a Monthly Report function that outputs to a Word document to a daily task Access database. However I’m seeing odd behavior when creating the bullet points.
*** I know that I can create reports but I don’t want to =) The formatting didn’t look the same***
Code explanation:
I have a button on a form that kicks off Sub btnCreateReport_Click()
Within that I created a Sub to clean up the paragraph / bullet creation called Sub fnNewPara
Additionally, I created another function that will eventually pull the text data from a database called Function fnGetActivities()
I use ParaIsSubBullet as a counter of how many indents I’m going to implement at a given line.
It’s easy to see how it’s skewed in the attached image.
Problem being is that if I just pass a blank string or blank space as text it formats perfectly.
If I add any text then it skews after the first iteration of the sub sub bullet.
side by side comparison of the text output
Here is the code:
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
If ParaIsOutdent = True Then
wdDoc.Paragraphs(n).Range.ListFormat.Listoutdent
End If
End Sub
Function fnGetActivities() As String
fnGetActivities = "a" ' adding text to the sub sub bullet creates odd behavior not linked to this function
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") ' arrays ind start at 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 ' Make Word application visible
Set wdDoc = wdApp.Documents.Add
'#########################################################
n = 1
s = 0
l = 0
' fnNewPara (n, Text, Font, FontSize, Underline, Bold, IsBullet, IsSubBullet,IsOutdent,NumofBulletIndents, Align, ObjectDef)
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
'#########################################################
' Save the document
'wdDoc.SaveAs myFileDest
' Optionally, close Word application
'wdApp.Quit
' Release the objects
Set subPara = Nothing
Set para = Nothing
Set wdDoc = Nothing
Set wdApp = Nothing
Charles is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.