Microsoft Office Forums Add a text below pictures

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-13-2019, 07:15 AM
Dzib Dzib is offline Add a text below pictures Windows 10 Add a text below pictures Office 2019
Novice
Add a text below pictures
 
Join Date: Jul 2019
Posts: 29
Dzib is on a distinguished road
Default Add a text below pictures

Hi,

I have this little code to add borders to pictures in a word doc and a little text below (like a caption with a number but couldn't find out how to reset captions index each time the macro is run so I made it this way)

Sub FormatImages()

Dim i As Integer
Dim oInlineShp As InlineShape

i = 0

For Each oInlineShp In ActiveDocument.InlineShapes
With oInlineShp
If .Borders(wdBorderTop).LineStyle = wdLineStyleNone Then
.Borders(wdBorderLeft).LineStyle = wdLineStyleSingle
.Borders(wdBorderLeft).LineWidth = wdLineWidth225pt
.Borders(wdBorderRight).LineStyle = wdLineStyleSingle
.Borders(wdBorderRight).LineWidth = wdLineWidth225pt
.Borders(wdBorderTop).LineStyle = wdLineStyleSingle
.Borders(wdBorderTop).LineWidth = wdLineWidth225pt
.Borders(wdBorderBottom).LineStyle = wdLineStyleSingle
.Borders(wdBorderBottom).LineWidth = wdLineWidth225pt
i = i + 1
.Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Range.InsertAfter vbCr & "Foto " & i & vbCr
.Range.Font.Bold = True
End If
End With
Next

End Sub

It works fine except that I would like to format (bold) the text inserted... I can't find out how to do that.
Range.font.bold = true doesn't work.



[Code] doesn't work from my smartphone, sorry

Edit: I just found a solution using the sélection method, extending the selection 2 lines down and formatting the whole selection 😉
Reply With Quote
  #2  
Old 09-13-2019, 11:23 PM
gmayor's Avatar
gmayor gmayor is offline Add a text below pictures Windows 10 Add a text below pictures Office 2016
Expert
 
Join Date: Aug 2014
Posts: 2,899
gmayor is just really nicegmayor is just really nicegmayor is just really nicegmayor is just really nice
Default

It is a question of setting the range appropriately e.g.

Code:
Sub FormatImages()
Dim i As Integer
Dim oInlineShp As InlineShape
Dim oRng As Range
    For i = 1 To ActiveDocument.InlineShapes.Count
        Set oInlineShp = ActiveDocument.InlineShapes(i)
        With oInlineShp
            If .Borders(wdBorderTop).LineStyle = wdLineStyleNone Then
                .Borders(wdBorderLeft).LineStyle = wdLineStyleSingle
                .Borders(wdBorderLeft).LineWidth = wdLineWidth225pt
                .Borders(wdBorderRight).LineStyle = wdLineStyleSingle
                .Borders(wdBorderRight).LineWidth = wdLineWidth225pt
                .Borders(wdBorderTop).LineStyle = wdLineStyleSingle
                .Borders(wdBorderTop).LineWidth = wdLineWidth225pt
                .Borders(wdBorderBottom).LineStyle = wdLineStyleSingle
                .Borders(wdBorderBottom).LineWidth = wdLineWidth225pt
                Set oRng = .Range
                oRng.InsertAfter vbCr
                oRng.End = oRng.Paragraphs.Last.Range.Next.Paragraphs(1).Range.End - 1
                oRng.Collapse 0
                oRng.ParagraphFormat.Alignment = wdAlignParagraphCenter
                oRng.Text = "Foto " & i
                oRng.Font.Bold = True
            End If
        End With
    Next i
    Set oInlineShp = Nothing
    Set oRng = Nothing
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 09-14-2019, 12:27 AM
Dzib Dzib is offline Add a text below pictures Windows 10 Add a text below pictures Office 2019
Novice
Add a text below pictures
 
Join Date: Jul 2019
Posts: 29
Dzib is on a distinguished road
Default

I know it's better to use range than selection so I guess I'll use your solution.
Thanks gmayor for your help.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Add a text below pictures Pictures not appearing when placed behind text Ray200 Drawing and Graphics 3 04-20-2016 06:52 AM
Add a text below pictures putting pictures within text ketanco Word 2 04-05-2016 10:49 AM
Add a text below pictures Problems with pictures and text flow Jennifer Murphy Drawing and Graphics 2 02-25-2016 11:44 AM
Asking about In Text Pictures, and Text surrounding azbaby543 Word 0 02-25-2016 11:22 AM
How to remove all pictures but keep text boxes gn4619 Word VBA 2 10-22-2015 08:19 PM


All times are GMT -7. The time now is 03:33 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2019, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2019 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft