Paste in picture, wrap with line, format paragraph before and picture...close to working
I am trying to write a macro to do the following.
Prerequisites: A picture has been copied (using snipping tool); the cursor is at the end of a paragraph.
What I want to do, and what appears to be happening (in parentheses):
1) Mark the current paragraph as "keep with next" (works)
2) Insert a paragraph return/new paragraph (? doesn't seem to work)
3) Paste in the picture (works, but not in a new paragraph - in the paragraph where the cursor was)
4) Format the picture with a line around it (works)
5) Apply a format to the paragraph with the picture (works, but to the wrong paragraph, so the original paragraph is now formatted instead)
This is cobbled together using a recorded macro and information I found online. Since I'm new to VBA, I'm commenting a lot.
Questions:
a) For the "With Selection.ParagraphFormat" inserted by the macro recorder, since all I want is ".KeepWithNext = True", can I remove everything else?
b) Why does my new paragraph for (2) above not seem to work? Or is the "rngSel.Paste" at the wrong spot? Or both?
Sub PastePictureAndCenter()
'
' PastePictureAndCenter Macro
'
' When you insert a picture, it is inserted as an Inline Shape, but you need to select it.
Dim ils As Word.InlineShape
Dim lNrIls As Long
Dim rngDoc As Word.Range
Dim rngSel As Word.Range
Set rngDoc = ActiveDocument.Content
Set rngSel = Selection.Range
rngDoc.End = rngSel.End + 1
' This part from Macro recorder, sets the current paragraph to Keep With Next
With Selection.ParagraphFormat
.LeftIndent = InchesToPoints(0)
.RightIndent = InchesToPoints(0)
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 12
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpace1pt5
.Alignment = wdAlignParagraphLeft
.WidowControl = True
.KeepWithNext = True
.KeepTogether = False
.PageBreakBefore = False
.NoLineNumber = False
.Hyphenation = True
.FirstLineIndent = InchesToPoints(1)
.OutlineLevel = wdOutlineLevelBodyText
.CharacterUnitLeftIndent = 0
.CharacterUnitRightIndent = 0
.CharacterUnitFirstLineIndent = 0
.LineUnitBefore = 0
.LineUnitAfter = 0
.MirrorIndents = False
.TextboxTightWrap = wdTightNone
.CollapsedByDefault = False
End With
' This should insert a paragraph
Selection.InsertParagraphAfter
' This was from the macro recorder, but this might replace a selection
' Selection.TypeParagraph
' Another attempt to add a paragraph
' Selection.Paragraphs.Add
' This counts the number of shapes in the document
lNrIls = rngDoc.InlineShapes.Count
'This pastes in the picture
Set rngSel = Selection.Range
rngSel.Paste
' This selects this picture (as an Inline Shape) that was just inserted
Set ils = rngDoc.InlineShapes(lNrIls + 1)
' This wraps the picture with a line
ils.Line.Visible = msoTrue
Selection.Style = ActiveDocument.Styles("OAR Para No Ind for Picture")
End Sub
|