From: Alex on
Hi all.
How can I replace picture in content control without losing content
control's (picture) format? (MS Word 2007)

Thanx
From: Jay Freedman on
Alex wrote:
> 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


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


From: Alex on
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).

"Jay Freedman" wrote:

> Alex wrote:
> > 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
>
>
> --
> 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.
>
>
> .
>
From: Jay Freedman on
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



On Mon, 1 Feb 2010 08:10:01 -0800, Alex
<Alex(a)discussions.microsoft.com> wrote:

>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).
>
>"Jay Freedman" wrote:
>
>> Alex wrote:
>> > 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
>>
>>
>> --
>> 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.
>>
>>
>> .
>>
From: Alex on
Thank you, Jay.
This code works perfect.

"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,
> 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
>
>
>
> On Mon, 1 Feb 2010 08:10:01 -0800, Alex
> <Alex(a)discussions.microsoft.com> wrote:
>
> >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).
> >
> >"Jay Freedman" wrote:
> >
> >> Alex wrote:
> >> > 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
> >>
> >>
> >> --
> >> 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.
> >>
> >>
> >> .
> >>
> .
>