Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / February 2011

Tip: Looking for answers? Try searching our database.

replace picture in content control

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Alex - 01 Feb 2010 12:57 GMT
Hi all.
How can I replace picture in content control without losing content
control's (picture) format? (MS Word 2007)

Thanx
Jay Freedman - 01 Feb 2010 14:53 GMT
> Hi all.
> How can I replace picture in content control without losing content
> control's (picture) format? (MS Word 2007)
>
> Thanx

If you just want to trigger the content control's Change Picture command and
let the user choose the picture, use something like this:

Sub x()
   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
   End If
End Sub

If you know what file you want to insert and just want to do the whole thing
in the macro, use something like this:

Sub y()
   Dim cc As ContentControl
   Set cc = ActiveDocument.ContentControls(1)
   If cc.Type = wdContentControlPicture Then
       If cc.Range.InlineShapes.Count > 0 Then
           cc.Range.InlineShapes(1).Delete
       End If
       ActiveDocument.InlineShapes.AddPicture _
           FileName:="e:\pictures\bunnycakes.jpg", _
           linktofile:=False, Range:=cc.Range
   End If
End Sub

Signature

Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

Alex - 01 Feb 2010 16:10 GMT
Thanks for your response, Jay.

I'm using C#, not VBA. Am I in wrong discussion group?

If I write code in C# similar to yours, then all picture format properties
(e.g. rotation, borders, shadows) are lost after shape.Delete() method.
I need to keep all formatting after the picture has been changed.
Thу only way I found is to copy properties from old shape to the new one.
But this method is not very nice. Besides, I can't find some properties (e.g
rotation angle).

> > Hi all.
> > How can I replace picture in content control without losing content
[quoted text clipped - 30 lines]
>     End If
> End Sub
Jay Freedman - 02 Feb 2010 03:30 GMT
Well, this is the word.VBA.general group. Questions about automating
Word from outside VBA are often posted to the word.programming group,
but there's certainly no enforceable rule.

You're correct about losing non-default formatting from the existing
picture, and that would happen in VBA as well -- I thought you were
concerned about losing the content control.

I don't see any alternative to copying the properties. Write it as a
separate subroutine, and once the work is done you just need to call
it as needed.

Getting the rotation of a picture in a content control is unreasonably
difficult. A picture in the control is an InlineShape, and in the
object model that type doesn't include a Rotation member. (This is a
case of VBA's object model lagging behind changes in the user
interface; in earlier versions you couldn't rotate an inline picture.)
VBA won't let you convert the InlineShape to a Shape. You have to copy
the picture and paste it to a place outside the content control, where
it can be converted to a Shape to get its Rotation value. Try this:

Sub z()
   Dim oPic As InlineShape
   Dim oShp As Shape
   Dim oRg1 As Range, oRg2 As Range
   Dim rot As Single
   
   Set oPic = ActiveDocument.ContentControls(1) _
       .Range.InlineShapes(1)
   Set oRg1 = oPic.Range
   
   ' get a range outside the content control
   Set oRg2 = ActiveDocument.ContentControls(1).Range
   oRg2.Collapse wdCollapseEnd
   oRg2.Move wdCharacter, 1
   
   ' copy the picture from the control and
   ' paste it outside the control
   oRg1.Copy
   oRg2.Paste
   
   ' make it a shape and find its rotation
   Set oShp = oRg2.InlineShapes(1).ConvertToShape
   rot = oShp.Rotation
   
   ' get rid of the leftovers
   oShp.Delete
   oPic.Delete
   
   ' put the new picture in the control
   ' and rotate it
   Set oShp = ActiveDocument.Shapes.AddPicture _
       (FileName:="c:\temp\jay headshot.jpg", _
       Anchor:=oRg1)
   oShp.Rotation = rot
End Sub

>Thanks for your response, Jay.
>
[quoted text clipped - 41 lines]
>>     End If
>> End Sub
Alex - 02 Feb 2010 12:15 GMT
Thank you, Jay.
This code works perfect.

> Well, this is the word.VBA.general group. Questions about automating
> Word from outside VBA are often posted to the word.programming group,
[quoted text clipped - 100 lines]
> >>
> .
Alex - 05 Feb 2010 07:53 GMT
Hi, Jay.
It seems that I hastened to the findings.
First of all, this code fails on some end user computers, but it doesn't on
my developer's machine.
I changed it as follows (C#):

InlineShape sh = ctrl.Range.InlineShapes[1];
Range rng1 = sh.Range;
rng2 = ctrl.Range;
object direction = WdCollapseDirection.wdCollapseEnd;
rng2.Collapse(ref direction);
int i = rng2.Move(ref unit, ref cnt);
rng1.Select(); //this line was added
Application.Selection.Copy();
rng2.Select(); //this line was added
rng2.Paste();
Shape shape = rng2.InlineShapes[1].ConvertToShape();

After adding range selection, the code started to work normally on other
computers.

But then I got another error:
If content control is located in the header (footer), then there is an
exception "Index refers beyond end of list." at:
rng2.InlineShapes[1].ConvertToShape();.

Should I use another way to manipulate content controls in header?

Thanks in advance.
sunil thomas - 03 Feb 2011 22:22 GMT
I was try to replace the picture in the content control. The content control was in the footer of the doc. The code given in the site works when the content control is not in footer.

The below code works even in the footer

ActiveDocument.SelectContentControlsByTag("newfooter").Item(1).Range.InlineShapes.Item(1).Delete
ActiveDocument.InlineShapes.AddPicture _
          FileName:="C:\Sunil\Images\logo3.bmp", _
          linktofile:=False, Range:=ActiveDocument.SelectContentControlsByTag("newfooter").Item(1).Range

Note: newfooter is the name of the picture content control.

> On Monday, February 01, 2010 7:57 AM Alex wrote:

> Hi all.
> How can I replace picture in content control without losing content
> control's (picture) format? (MS Word 2007)
>
> Thanx

>> On Monday, February 01, 2010 9:53 AM Jay Freedman wrote:

>> If you just want to trigger the content control's Change Picture command and
>> let the user choose the picture, use something like this:
[quoted text clipped - 31 lines]
>> Email cannot be acknowledged; please post all follow-ups to the newsgroup so
>> all may benefit.

>>> On Monday, February 01, 2010 11:10 AM Alex wrote:

>>> Thanks for your response, Jay.
>>>
[quoted text clipped - 6 lines]
>>> But this method is not very nice. Besides, I cannot find some properties (e.g
>>> rotation angle).

>>>> On Monday, February 01, 2010 10:30 PM Jay Freedman wrote:

>>>> Well, this is the word.VBA.general group. Questions about automating
>>>> Word from outside VBA are often posted to the word.programming group,
[quoted text clipped - 52 lines]
>>>> oShp.Rotation = rot
>>>> End Sub

>>>>> On Tuesday, February 02, 2010 7:15 AM Alex wrote:

>>>>> Thank you, Jay.
>>>>> This code works perfect.
>>>>>
>>>>> "Jay Freedman" wrote:

>>>>>> On Friday, February 05, 2010 2:53 AM Alex wrote:

>>>>>> Hi, Jay.
>>>>>> It seems that I hastened to the findings.
[quoted text clipped - 25 lines]
>>>>>>
>>>>>> Thanks in advance.

>>>>>> Submitted via EggHeadCafe
>>>>>> Statistics, Probability, Lotteries and Dumb Programmers
>>>>>> http://www.eggheadcafe.com/tutorials/aspnet/041de19a-e704-468f-bd3c-79164fc739f5
/statistics-probability-lotteries-and-dumb-programmers.aspx
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2012 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.