Chat GPT macro that counts pictures in my document
Hello everyone.
In order to make my work easier i need a macro that counts all the pictures in my document and inserts the count as text in a specified location. This will save me some time, because now i count the pictures manually, one by one.
So, in order to make the macro i used chat gpt and this is the code it generated, after some time i interacted with the A.I. :
Sub Nrfilefoto()
Dim imageCount As Integer
imageCount = 0
For Each shape In ActiveDocument.Shapes
If shape.Type = msoPicture And shape.Anchor.Information(wdActiveEndAdjustedPageNu mber) <> 1 Then
imageCount = imageCount + 1
ElseIf shape.Type = msoGroup Then
For Each subShape In shape.GroupItems
If subShape.Type = msoPicture And subShape.Anchor.Information(wdActiveEndAdjustedPag eNumber) <> 1 Then
imageCount = imageCount + 1
End If
Next
ElseIf shape.Type = msoAutoShape Then
If shape.Anchor.Information(wdActiveEndAdjustedPageNu mber) <> 1 Then
imageCount = imageCount + 1
End If
End If
Next
For Each InlineShape In ActiveDocument.InlineShapes
If InlineShape.Type = wdInlineShapePicture And InlineShape.Range.Information(wdActiveEndAdjustedP ageNumber) <> 1 Then
imageCount = imageCount + 1
ElseIf InlineShape.Type = wdInlineShapeCallout And InlineShape.Range.Information(wdActiveEndAdjustedP ageNumber) <> 1 Then
imageCount = imageCount + 1
End If
Next
Dim rng As Range
Set rng = ActiveDocument.Bookmarks("foto").Range
rng.Text = imageCount
ActiveDocument.Bookmarks.Add "foto", rng
Set rng = ActiveDocument.Bookmarks("file").Range
Dim pageCount As Integer
pageCount = ActiveDocument.ComputeStatistics(wdStatisticPages)
rng.Text = pageCount
ActiveDocument.Bookmarks.Add "file", rng
End Sub
So, this script counts the number of images and pages in my document and sets the text for two inserted bookmarks to the values of the count.
This is the A.I. description of the code :
VBA (Visual Basic for Applications) script used to count the number of images and pages in an active Word document. It first initializes a variable called "imageCount" to zero and then iterates through all the "Shapes" in the active document, incrementing the "imageCount" variable each time it finds a shape that is a picture, group, or AutoShape and not on the first page. Next, it iterates through all the "InlineShapes" in the active document, incrementing the "imageCount" variable each time it finds an inline shape that is a picture or callout and not on the first page. Finally, it sets the text of the "foto" and "file" bookmarks in the active document to the values of the "imageCount" and "pageCount" variables, respectively.
Now, my problem is this code doesn't count pictures that are inserted in shapes, like callout or rectangle shapes. Instead, it adds to the count all the shapes (callout, rectangle, circle) inserted in my document, no matter if they have pictures inserted inside them or not.
Does anyone have any idea of how to make this code work? Thank you in advance !
Sorry for the lenght of this post!
|