|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
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 |
#2
|
|||
|
|||
And when I paste in code, how do I keep the formatting, e.g., so the indentation is correct?
|
#3
|
|||
|
|||
When you record a macro you will get a lot of code that you don't need. That code can safely be deleted.
As you didn't move the selection after adding the new paragraph your subsequent code was executed on the original selection. When you post code in a question here you need to use the code format button (#) The only code you actually need is below. Code:
Sub PastePictureAndCenter() Dim target As Range Set target = Selection.Range With target .ParagraphFormat.KeepWithNext = True .InsertParagraphAfter .Move wdParagraph .Style = ActiveDocument.Styles("OAR Para No Ind for Picture") .Paste .InlineShapes(1).Line.Visible = msoTrue End With End Sub |
#4
|
|||
|
|||
Wow, thank you so much! I cannot tell you how much time your contribution and this macro saves me. I do these operations over a hundred times, probably into the hundreds of times per year. The same set of (1)-(5) above, over and over... And the macro recorder does not help, as it doesn't work well with inserted pictures.
I took an online class in VBA, got about 3/4 of the way through it, then decided to start writing code to help me and members of my small company with repetitive tasks. The class was great, for providing a foundation, but doesn't answer the specific questions I have. If anyone does something similar, I just made an addition to add a new paragraph after picture insertion with a different style. Works great. Code:
Sub PastePictureAndCenter() Dim target As Range Set target = Selection.Range With target .ParagraphFormat.KeepWithNext = True .InsertParagraphAfter .Move wdParagraph .Style = ActiveDocument.Styles("OAR Para No Ind for Picture") .Paste .InlineShapes(1).Line.Visible = msoTrue .Move wdParagraph .InsertParagraphAfter .Style = ActiveDocument.Styles("OAR Para No Ind") End With End Sub |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Wrap Text Inside Picture | marconexcel | Drawing and Graphics | 2 | 02-02-2018 05:01 PM |
Wrap TABLES around picture? | ShankedS | Word Tables | 2 | 11-16-2015 04:27 PM |
Format Picture toolbar button not working in Word 2007 | WaltR | Word | 2 | 08-19-2015 05:19 PM |
How do you paste a graph from Excel into Word as picture without line appearing? | swygant | Word | 10 | 07-01-2014 03:23 PM |
"format picture" - arrows show as no line atop picture | marbeth | Word | 3 | 07-08-2011 02:16 PM |