-
Picture Format Painter
This isn't a question and I hope I'm not breaking a rule by just posting a solution to my own problem. I've seen several questions in the past about working with the picture formatting tools in Word 2007/2010. For lack of something better to do, I've spent the last few days working on a template add-in for Word that would faithfully copy and apply the formatting of one reference image to a target image.
The other day I cried "Eureka" when I discovered (or thought I did) that just simply selecting the reference image and copying the formatting "Selection.CopyFormatting" and then selecting the target image and pasting the formatting "Selection.PasterFormatting" did the job.
Earlier today I learned that that process handled most of the attributes (e.g., borders, shadow, softedge, etc.) it missed some of the more advanced attributres like artistic effect, sharpness, color tone, etc.
After much whailing and gnashing of teeth, I think I have now found a solution:
[VBA]Sub Test()
PaintReferencePictureFormatToTargetPicture ActiveDocument.InlineShapes(1), ActiveDocument.InlineShapes(2)
End Sub
Sub PaintReferencePictureFormatToTargetPicture(ByRef oILSRef As InlineShape, oILSTarget As InlineShape)
Dim i As Long, j As Long
Dim oILS1 As InlineShape
Set oILS1 = ActiveDocument.Sections(2).Range.InlineShapes(1)
'Select it and copy formatting. This grabs most of the attributes (e.g., border, shadow, soften, etc.)
oILSRef.Select
Selection.CopyFormat
'Set the target image.
oILSTarget.Select
Selection.PasteFormat
On Error Resume Next
'Clear all picture effects in the target shape.
For i = oILSTarget.Fill.PictureEffects.Count To 1 Step -1
oILSTarget.Fill.PictureEffects.Item(i).Delete
Next i
'Apply picture effects of source ILS to target ILS
For i = 1 To oILSRef.Fill.PictureEffects.Count
'Insert the effect.
oILSTarget.Fill.PictureEffects.Insert oILSRef.Fill.PictureEffects.Item(i)
'Apply the effect parameters.
For j = 1 To oILSRef.Fill.PictureEffects.Item(i).EffectParameters.Count
'Debug.Print oILSRef.Fill.PictureEffects.Item(i).EffectParameters(j).Name & " " & oILSRef.Fill.PictureEffects.Item(i).EffectParameters(j).Value
oILSTarget.Fill.PictureEffects.Item(i).EffectParameters(j).Value = oILSRef.Fill.PictureEffects.Item(i).EffectParameters(j).Value
'Debug.Print oILSRef.Fill.PictureEffects.Item(i).EffectParameters(j).Name & " " & oILSRef.Fill.PictureEffects.Item(i).EffectParameters(j).Value
Next j
Next i
'oILSTarget.PictureFormat.ColorType = oILSRef.PictureFormat.ColorType
On Error GoTo 0
End Sub
[/VBA]
To test just insert two pictures. Apply all sorts of formatting and execute the code. I'm a one man show so the results are only valid for the limited testing I've done. If anyone uses this process and find a flaw, then please let me know.
-
I know that this an old post, but the information is still good and helped me a lot
I made some changes and it seems to still work (always a good thing), but I'd like it to use the selected picture, not the (1) one
Any ideas for using the selected picture, or any other ideas?
Code:
Sub ApplyFirstPictureFormatting()
Dim oMaster As InlineShape
Dim i As Long, n As Long
If ActiveDocument.InlineShapes.Count < 2 Then Exit Sub
Application.ScreenUpdating = False
Set oMaster = ActiveDocument.InlineShapes(1)
n = ActiveDocument.InlineShapes.Count
For i = 2 To ActiveDocument.InlineShapes.Count
StatusBar = "Processing Inline Picture " & Right(" " & Format(i, "#,##0"), 5) & " (" & Format(i / n, "0.0%") & ")"
If ActiveDocument.InlineShapes(i).Type = wdInlineShapePicture Then
Call PaintReferencePictureFormatToTargetPicture(oMaster, ActiveDocument.InlineShapes(i))
End If
Next
StatusBar = vbNullString
Application.ScreenUpdating = True
End Sub
Private Sub PaintReferencePictureFormatToTargetPicture(ByRef oILSRef As InlineShape, oILSTarget As InlineShape)
Dim i As Long, j As Long
'Select it and copy formatting. This grabs most of the attributes (e.g., border, shadow, soften, etc.)
oILSRef.Select
Selection.CopyFormat
'Set the target image.
oILSTarget.Select
Selection.PasteFormat
On Error Resume Next
'Clear all picture effects in the target shape.
For i = oILSTarget.Fill.PictureEffects.Count To 1 Step -1
oILSTarget.Fill.PictureEffects.Item(i).Delete
Next i
'Apply picture effects of source ILS to target ILS
For i = 1 To oILSRef.Fill.PictureEffects.Count
'Insert the effect.
oILSTarget.Fill.PictureEffects.Insert oILSRef.Fill.PictureEffects.Item(i)
'Apply the effect parameters.
For j = 1 To oILSRef.Fill.PictureEffects.Item(i).EffectParameters.Count
oILSTarget.Fill.PictureEffects.Item(i).EffectParameters(j).Value = oILSRef.Fill.PictureEffects.Item(i).EffectParameters(j).Value
Next j
Next i
Selection.Collapse
DoEvents
On Error GoTo 0
End Sub
-
Paul,
Maybe:
Code:
Set oMaster = Selection.InlineShapes(1)
n = ActiveDocument.InlineShapes.Count
For i = 1 To ActiveDocument.InlineShapes.Count
If Not oMaster.Range.Start = ActiveDocument.InlineShapes(i).Range.Start Then
StatusBar = "Processing Inline Picture " & Right(" " & Format(i, "#,##0"), 5) & " (" & Format(i / n, "0.0%") & ")"
If ActiveDocument.InlineShapes(i).Type = wdInlineShapePicture Then
Call PaintReferencePictureFormatToTargetPicture(oMaster, ActiveDocument.InlineShapes(i))
End If
End If
Next
-
Works well
I DO have to learn more about the Selection object (and the Range object)
Thanks
Final (for now at least) version:
Code:
Sub CopyPictureFormat()
Dim oMaster As InlineShape
Dim i As Long, n As Long
If ActiveDocument.InlineShapes.Count < 2 Then Exit Sub
Application.ScreenUpdating = False
On Error Resume Next
Set oMaster = Selection.InlineShapes(1)
On Error GoTo 0
If oMaster Is Nothing Then
Call MsgBox("No Inline Shape Selected", vbCritical + vbOKOnly, "Apply Formatting to Other Pictures")
Exit Sub
End If
With ActiveDocument
n = .InlineShapes.Count
For i = 1 To .InlineShapes.Count
If Not oMaster.Range.Start = .InlineShapes(i).Range.Start Then
StatusBar = "Processing Inline Picture " & Right(" " & Format(i, "#,##0"), 5) & " (" & Format(i / n, "0.0%") & ")"
If .InlineShapes(i).Type = wdInlineShapePicture Then
Call PaintFormatToPicture(oMaster, .InlineShapes(i))
End If
End If
Next
End With
StatusBar = vbNullString
Application.ScreenUpdating = True
End Sub
Private Sub PaintFormatToPicture(ByRef oILSRef As InlineShape, oILSTarget As InlineShape)
Dim i As Long, j As Long
'Select it and copy formatting. This grabs most of the attributes (e.g., border, shadow, soften, etc.)
oILSRef.Select
Selection.CopyFormat
'Set the target image.
oILSTarget.Select
Selection.PasteFormat
On Error Resume Next
'Clear all picture effects in the target shape.
For i = oILSTarget.Fill.PictureEffects.Count To 1 Step -1
oILSTarget.Fill.PictureEffects.Item(i).Delete
Next I
'Apply picture effects of source ILS to target ILS
For i = 1 To oILSRef.Fill.PictureEffects.Count
'Insert the effect.
oILSTarget.Fill.PictureEffects.Insert oILSRef.Fill.PictureEffects.Item(i)
'Apply the effect parameters.
For j = 1 To oILSRef.Fill.PictureEffects.Item(i).EffectParameters.Count
oILSTarget.Fill.PictureEffects.Item(i).EffectParameters(j).Value = oILSRef.Fill.PictureEffects.Item(i).EffectParameters(j).Value
Next j
Next I
Selection.Collapse
DoEvents
On Error GoTo 0
End Sub