I am struggling with the following problem. In a document I have many inline pictures as well as some floating pictures. I would like to write a code which changes the style of a paragraph in which a picture is included. Let's call this style "picture". The only problem is with paragraphs floating pictures, i.e., the ones which have picture with some text around. In this case I would like to leave the style untouched.
Here is the example of how it looks like in Word.
I have written such a code so far:
Sub ApplyPictureStyle()
Application.ScreenUpdating = False
Dim iShp As InlineShape
For Each iShp In ActiveDocument.InlineShapes
With iShp
If .Type = wdInlineShapePicture Then .Range.Style = "Picture"
End With
Next
Application.ScreenUpdating = True
End Sub
As I explained earlier such a code will work for all pictures.
Is it possible to rewrite this code so that it works for inline pictures but omits floating pictures at the same time?
A floating picture is one that doesn't have a wrap style of "Inline with Text". All of the pictures in your screenshot are inline pictures. So to apply different formatting to these pictures you need to determine whether the paragraph only contains a picture or a picture plus text.
You can do this by using the Len
function to count the characters in the paragraph's text. A paragraph that only contains a picture will just have 2 characters, even if the picture has alternate text.
Sub ApplyPictureStyle()
Application.ScreenUpdating = False
Dim iShp As InlineShape
For Each iShp In ActiveDocument.InlineShapes
With iShp
If .Type = wdInlineShapePicture Then
If Len(.Range.Paragraphs(1).Range.Text) = 2 Then
'paragraph only contains a picture
.Range.style = "Picture"
End If
End If
End With
Next
Application.ScreenUpdating = True
End Sub