none
Changing image in ContentControl with VBA

    Question

  • If I change the image in a Word content control using the Word "Change Image" commnd (right click in the control and select) the image imported is automatically sized to the size of the CC.

    If I import an image as follows:

    Sub ScratchMacro()
    Dim oCC As ContentControl
    Dim pPicFileName As String
    Set oCC = ActiveDocument.ContentControls(1)
    If oCC.Type = wdContentControlPicture Then
     If oCC.Range.InlineShapes.Count > 0 Then oCC.Range.InlineShapes(1).Delete
     pPicFileName = "C:\Liberty.jpg"
     ActiveDocument.InlineShapes.AddPicture FileName:=pPicFileName, _
                 linktofile:=True, Range:=oCC.Range
    End If
    End Sub

    The CC is automatically resized to the size of the image.  How do I change the image via VBA and the image be scale to the CC dimensions?  I don't want to first select the CC and call the change picture dialog.

    Thanks.


    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm
    Wednesday, January 12, 2011 9:16 PM

Answers

  • All,

    Thanks for your replies.  I am sorry that it took so long to get back to you.  I thought I had posted via Answers and would get a note when someone replied.  I will try some of this and see how I do.
     "Jay Freedman [MVP]" wrote in message news:f2f11e6d-4d5e-47b8-9bd0-db7184bf6794@communitybridge.codeplex.com...

    That works as long as you know in advance the size you want, and you don't care whether the new picture's aspect ratio is preserved.

    For the general case, you should store in variables the height and width of the old picture before you delete it. After you insert the new picture, you need to multiply get its height and width.
    Calculate the fractions (old height/new height) and (old width/new width), and choose the smaller one. Multiply that fraction by the new height and width to get the size that will fit into the same
    space as the old picture.

    For code that does something similar to this, download form_picture.zip from http://jay-freedman.info, unzip the template from it, and look at the FormInsertPicture() macro.

    Jay Freedman
    MS Word MVP  FAQ: http://word.mvps.org


    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm
    Monday, January 17, 2011 9:03 PM

All replies

  • Hi Greg,

    Unfortunately, there’s no way to get the same result via code in a picture ContentControl as you can via right-clicking on the picture ContentControl and choosing “Change Picture…”.

     

    There may be other options to consider:

    1.    Sizing the picture/image before inserting it into the ContentControl via code

    2.    Instead of a ContentControl, consider using an ActiveX control for images where you should be able to size it as needed

    Thanks,

    Bill

    Saturday, January 15, 2011 12:59 AM
  • Hi Greg

    Think this might work - something of Jay's I added in the resize part:

    Sub xChange()
    Dim cc As ContentControl
    Set cc = ActiveDocument.ContentControls(1)
    If cc.Type = wdContentControlPicture Then
    'Debug.Print cc.Range.InlineShapes.Count
    cc.Range.Select
    'Dialogs(wdDialogInsertPicture).Show

    If Dialogs(wdDialogInsertPicture).Show <> 0 Then
           With Selection.Paragraphs(1).Range.InlineShapes(1)
               .LockAspectRatio = msoFalse
               .Height = CentimetersToPoints(0.98)
               'or use InchesToPoints
               .Width = CentimetersToPoints(1.75)
           End With
       End If

    End If
    End Sub
      "Greg Maxey" wrote in message news:338f6c31-7ac9-4e22-90b7-46b181e2f75f@communitybridge.codeplex.com...

    If I change the image in a Word content control using the Word "Change Image" commnd (right click in the control and select) the image imported is automatically sized to the size of the CC.

    If I import an image as follows:

    Sub ScratchMacro()
    Dim oCC As ContentControl
    Dim pPicFileName As String
    Set oCC = ActiveDocument.ContentControls(1)
    If oCC.Type = wdContentControlPicture Then
    If oCC.Range.InlineShapes.Count > 0 Then oCC.Range.InlineShapes(1).Delete
    pPicFileName = "C:\Liberty.jpg"
    ActiveDocument.InlineShapes.AddPicture FileName:=pPicFileName, _
                linktofile:=True, Range:=oCC.Range
    End If
    End Sub

    The CC is automatically resized to the size of the image.  How do I change the image via VBA and the image be scale to the CC dimensions?  I don't want to first select the CC and call the change picture dialog.

    Thanks.


    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm


    janine@ribbonspace.com www.ribbonspace.com Documents and Ribbons. Simple Efficient Office Automation.
    Sunday, January 16, 2011 3:42 AM
  • That works as long as you know in advance the size you want, and you don't care whether the new picture's aspect ratio is preserved.

    For the general case, you should store in variables the height and width of the old picture before you delete it. After you insert the new picture, you need to multiply get its height and width.
    Calculate the fractions (old height/new height) and (old width/new width), and choose the smaller one. Multiply that fraction by the new height and width to get the size that will fit into the same
    space as the old picture.

    For code that does something similar to this, download form_picture.zip from http://jay-freedman.info, unzip the template from it, and look at the FormInsertPicture() macro.


    Jay Freedman
    MS Word MVP  FAQ: http://word.mvps.org
    Sunday, January 16, 2011 6:38 PM
  • All,

    Thanks for your replies.  I am sorry that it took so long to get back to you.  I thought I had posted via Answers and would get a note when someone replied.  I will try some of this and see how I do.
     "Jay Freedman [MVP]" wrote in message news:f2f11e6d-4d5e-47b8-9bd0-db7184bf6794@communitybridge.codeplex.com...

    That works as long as you know in advance the size you want, and you don't care whether the new picture's aspect ratio is preserved.

    For the general case, you should store in variables the height and width of the old picture before you delete it. After you insert the new picture, you need to multiply get its height and width.
    Calculate the fractions (old height/new height) and (old width/new width), and choose the smaller one. Multiply that fraction by the new height and width to get the size that will fit into the same
    space as the old picture.

    For code that does something similar to this, download form_picture.zip from http://jay-freedman.info, unzip the template from it, and look at the FormInsertPicture() macro.

    Jay Freedman
    MS Word MVP  FAQ: http://word.mvps.org


    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm
    Monday, January 17, 2011 9:03 PM