Consulting

Results 1 to 4 of 4

Thread: Picture Format Painter

  1. #1
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    2,790
    Location

    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.
    Greg

    Visit my website: http://gregmaxey.com

  2. #2
    VBAX Wizard
    Joined
    Apr 2007
    Posts
    5,535
    Location
    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?



    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

    ------------------------------------------------------------------------------------------------------------------------
    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s)
    (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    2,790
    Location
    Paul,

    Maybe:

        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
    Greg

    Visit my website: http://gregmaxey.com

  4. #4
    VBAX Wizard
    Joined
    Apr 2007
    Posts
    5,535
    Location
    Works well

    I DO have to learn more about the Selection object (and the Range object)

    Thanks

    Final (for now at least) version:

    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
    Last edited by Paul_Hossler; 06-20-2017 at 07:20 AM.
    Paul

    ------------------------------------------------------------------------------------------------------------------------
    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s)
    (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •