2

The question pretty much explains every thing. I want to know if there is a way to change image in a picture content control from an image in another picture content control using vba.

Thanks

S.aad
  • 518
  • 1
  • 5
  • 22

1 Answers1

1

This appears to work. It assumes that the content controls are of the Picture type (which is why you've probably been having difficulties switching the contents). One of the pictures is saved to the Clipboard, while the other is put at the end of the document. The one from the clipboard is pasted into the other control, then that at the end of the document is cut and pasted into the first control.

Dim cc1 As word.ContentControl, cc2 As word.ContentControl
Dim ils1 As word.InlineShape, ils2 As word.InlineShape
Dim doc As word.Document, rngTemp As word.Range

Set doc = ActiveDocument
Set rngTemp = doc.content
rngTemp.Collapse wdCollapseEnd
Set cc1 = doc.Contentcontrols(1)
Set cc2 = doc.Contentcontrols(2)
Set ils1 = cc1.Range.InlineShapes(1)
Set ils2 = cc2.Range.InlineShapes(1)

ils1.Range.Copy
rngTemp.FormattedText = ils2.Range.FormattedText
ils2.Range.Paste
rngTemp.Cut
ils1.Range.Paste
Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
  • Thank you for the response. The pasted image is shrinking to a very small size. How do i retain dimension of the picture content control in which it is being pasted. [Here](http://my.jetscreenshot.com/24060/20151120-6ufi-67kb.jpg) is a screen shot of simply copying from one content control into another of the same size – S.aad Nov 20 '15 at 09:13
  • 1
    In my tests, there was no change in picture or content control size: the content control resized to fit the different picture. So without more information on how to reproduce your situation it's impossible to say why that may be happening. The only thing I can suggest is to query the InlineShape's WIdth and/Or Height property and reapply it after pasting. – Cindy Meister Nov 20 '15 at 14:53
  • Thank you for pointing me in a direction. My content control is inside a text box that is on a shape could that be causing trouble? – S.aad Nov 24 '15 at 05:36
  • When i pasted it and resized it it induced itself an [abnormal width](http://my.jetscreenshot.com/24060/20151124-y23q-15kb.jpg). [Here](http://my.jetscreenshot.com/24060/20151124-g08l-101kb.jpg) is my code. – S.aad Nov 24 '15 at 06:37
  • It could have something to do with "inside a text box that is on a shape" - hard to say at this remove. Can you explain why you're using this kind of "layering"? Would a table work (at least instead of the text box)? Other than that I can only recommend testing as an end-user - trying out various things - until you find a combination that works. – Cindy Meister Nov 24 '15 at 15:39