ShapeRange Selection

  • Thread starter Roderick O'Regan
  • Start date
R

Roderick O'Regan

The following snippet is part of a larger procedure which places
AutoText graphics in a header and then moves them to the correct
location:
ActiveWindow.ActivePane.View.SeekView = wdSeekFirstPageHeader

'enters the autotext graphic located in the attached Template
ActiveDocument.AttachedTemplate.AutoTextEntries(pString).Insert _
Where:=Selection.Range, RichText:=True

'first select the graphic

ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Range.ShapeRange.Select

With Selection.ShapeRange
.RelativeHorizontalPosition = _
wdRelativeHorizontalPositionPage
.RelativeVerticalPosition = _
wdRelativeVerticalPositionPage
.Left = CentimetersToPoints(2.5)
.Top = CentimetersToPoints(1.99)
.WrapFormat.AllowOverlap = True
End With

If I run the template all works correctly.

However, if I open the template and then run this procedure to test
the code I get an error 4605 telling me that the
"RelativeHorizontralPosition method or property is not available
because the drawing operation cannot be applied to the current
selection"

The graphic (ShapeRange) is shown as selected.

I then Exit at the error message dialog, close the Header/Footer view
and try it again. It now works correctly from the second time onwards.

Could someone explain why this is happening, please?

Roderick O'Regan
 
J

Jean-Guy Marcil

Roderick O'Regan was telling us:
Roderick O'Regan nous racontait que :
The following snippet is part of a larger procedure which places
AutoText graphics in a header and then moves them to the correct
location:
ActiveWindow.ActivePane.View.SeekView = wdSeekFirstPageHeader

'enters the autotext graphic located in the attached Template
ActiveDocument.AttachedTemplate.AutoTextEntries(pString).Insert _
Where:=Selection.Range, RichText:=True

'first select the graphic

ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Range.ShapeRange.Select

With Selection.ShapeRange
.RelativeHorizontalPosition = _
wdRelativeHorizontalPositionPage
.RelativeVerticalPosition = _
wdRelativeVerticalPositionPage
.Left = CentimetersToPoints(2.5)
.Top = CentimetersToPoints(1.99)
.WrapFormat.AllowOverlap = True
End With

If I run the template all works correctly.

However, if I open the template and then run this procedure to test
the code I get an error 4605 telling me that the
"RelativeHorizontralPosition method or property is not available
because the drawing operation cannot be applied to the current
selection"

The graphic (ShapeRange) is shown as selected.

I then Exit at the error message dialog, close the Header/Footer view
and try it again. It now works correctly from the second time onwards.

Could someone explain why this is happening, please?

Try not to use the ActivePane and Selection.

Instead, try something like this:

'_______________________________________
Dim rgeHeader As Range
Dim shpHeader As Shape

Set rgeHeader =
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
rgeHeader.Collapse wdCollapseStart

'enters the autotext graphic located in the attached Template
ActiveDocument.AttachedTemplate.AutoTextEntries("Test").Insert _
Where:=rgeHeader, RichText:=True

Set rgeHeader = rgeHeader.Sections(1).Headers(wdHeaderFooterPrimary).Range
'rgeHeader.Select
Set shpHeader = rgeHeader.ShapeRange(1)

With shpHeader
.RelativeHorizontalPosition = _
wdRelativeHorizontalPositionPage
.RelativeVerticalPosition = _
wdRelativeVerticalPositionPage
.Left = CentimetersToPoints(2.5)
.Top = CentimetersToPoints(1.99)
.WrapFormat.AllowOverlap = True
End With
'_______________________________________

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
R

Roderick O'Regan

Thanks Jean-Guy for the help.

Is your solution then, in this case, to use ranges rather than
selections?

It seems very odd how it works in selection - or doesn't! In one
instance it doesn't recognise the Relative position command and then
it does the second time around. Weird!

Roderick
 
J

Jean-Guy Marcil

Roderick O'Regan was telling us:
Roderick O'Regan nous racontait que :
Thanks Jean-Guy for the help.

Is your solution then, in this case, to use ranges rather than
selections?

It seems very odd how it works in selection - or doesn't! In one
instance it doesn't recognise the Relative position command and then
it does the second time around. Weird!

I always avoid the Selection object (Except when there are no other choice,
of course), and I especially want to avoid the ActivePane method to access
headers/footers because too often I had problems when doing so.

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top