Good morning
In creating a report I use bookmarks to add string data from the loop in my code:
The first bookmark below the first sub heading isn't being found in the collection
I am a trainee and have tried everything I can possibly think of. Can anyone assist please?
I look forward to hearing from you. Many thanks.
Code
Dim strDocName As String
Dim strReport1, strReport2 As String
Dim i As Integer
Dim docReport As Document
Dim rngReport As Range
Dim bmk As Bookmarks
Set docReport = Documents.Add
Set rngReport = docReport.Range
' rngReport.Text = "MACRO USAGE" & vbCr
With rngReport
.Text = "MACRO USAGE" & vbCr
.Font.Bold = True
.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.EndKey
Selection.MoveEnd Unit:=wdLine, Count:=2
End With
rngReport.Collapse direction:=wdCollapseEnd
Selection.EndKey
' rngReport.Text = vbCr & "Documents with 50+ fields" & vbCr
With rngReport
.Text = vbCr & "Documents with 50+ fields" & vbCr
.Font.Bold = True
.ParagraphFormat.Alignment = wdAlignParagraphLeft
End With
Selection.EndKey
rngReport.Collapse direction:=wdCollapseEnd
rngReport.Bookmarks.Add ("bmkReport1")
rngReport.Collapse direction:=wdCollapseEnd
' rngReport.Text = vbCr & "Documents with less than 50 fields" & vbCr
With rngReport
.Text = vbCr & "Documents with less than 50 fields" & vbCr
.Font.Bold = True
.ParagraphFormat.Alignment = wdAlignParagraphLeft
.Collapse direction:=wdCollapseEnd
Selection.EndKey
End With
rngReport.Collapse direction:=wdCollapseEnd
rngReport.Bookmarks.Add ("bmkReport2")
rngReport.Collapse direction:=wdCollapseEnd
For i = 1 To Documents.Count
strDocName = Documents(i).Name
'If a Document has 50 fields or more in it then the client would
'like the macro to automatically update all the fields in the
'document.
If Documents(i).Fields.Count >= 50 Then
strReport1 = "> 50 " & strDocName & vbCr
Documents(i).Fields.Update
' rngReport.Bookmarks("bmkReport1").Range.Text = strReport1
With rngReport.Bookmarks("bmkReport1").Range
' .Bookmarks.Add ("strReport1")
.Select
.Text = strReport1
.Font.Bold = False
.Paragraphs.Alignment = wdAlignParagraphLeft
End With
Else
If i < 20 Then
'only the first 20 fields are updated, any other's should be ignored.
strReport2 = "< 20 " & strDocName & vbCr
Documents(i).Fields.Update
' rngReport.Bookmarks("bmkReport2").Range.Text = strReport2
With rngReport.Bookmarks("bmkReport2").Range
.Select
.Text = strReport2
.Font.Bold = False
.ParagraphFormat.Alignment = wdAlignParagraphLeft
End With
End If
End If
Next i
Display More