Dear Experts:

below code snippet tries to insert a drawing canvas in the current
document. I would like the width of the canvas to match the printable
area of the document (by subtracting the Left and Right Margin from
the page width). Running the macro I get a runtime error 91 on the
"Width"-portion of the code.

The "width" coding is apparently false. How can I amend the code so
that it is working?

Help is much appreciated. Thank you very much in advance.


Sub InsertCanvasWidthPrintableArea()


Dim shpCanvas As Shape
Dim sect As Section

'Add a new drawing canvas to the active document

Set shpCanvas = ActiveDocument.Shapes.AddCanvas( _
Left:=70, _
Top:=75, _

Width:=sect.PageSetup.PageWidth - _
sect.PageSetup.LeftMargin - _
sect.PageSetup.RightMargin, _

Height:=250)

With shpCanvas
.Visible = msoTrue
.Select
End With
Selection.ShapeRange.WrapFormat.Type = wdWrapTopBottom


Selection.ShapeRange.Fill.Solid
Selection.ShapeRange.Line.Weight = 0.75
Selection.ShapeRange.Line.Style = msoLineSingle
Selection.ShapeRange.Line.Visible = msoTrue
Selection.ShapeRange.Line.BackColor.RGB = RGB(255, 255, 255)

End Sub

I know that inserting a canvas through the menu results in the canvas'
width matching exactly the printable area of the document (page width
minus left marging minus right margin). But still I wish to know the
right coding for this if done by VBA.

Re: Adding a drawing canvas via VBA that has the width of the printable area by Jay

Jay
Fri Jul 18 05:05:57 PDT 2008

Offhand, I'd say it's because you declare the variable sect but you never assign
a value to it. That means the expression sect.PageSetup isn't valid because sect
is Null. I'm guessing that what you want is

Set sect = Selection.Sections(1)

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

On Fri, 18 Jul 2008 04:16:06 -0700 (PDT), andreas <andreas.hermle@gmx.de> wrote:

>Dear Experts:
>
>below code snippet tries to insert a drawing canvas in the current
>document. I would like the width of the canvas to match the printable
>area of the document (by subtracting the Left and Right Margin from
>the page width). Running the macro I get a runtime error 91 on the
>"Width"-portion of the code.
>
>The "width" coding is apparently false. How can I amend the code so
>that it is working?
>
>Help is much appreciated. Thank you very much in advance.
>
>
>Sub InsertCanvasWidthPrintableArea()
>
>
>Dim shpCanvas As Shape
>Dim sect As Section
>
> 'Add a new drawing canvas to the active document
>
> Set shpCanvas = ActiveDocument.Shapes.AddCanvas( _
>Left:=70, _
>Top:=75, _
>
>Width:=sect.PageSetup.PageWidth - _
> sect.PageSetup.LeftMargin - _
> sect.PageSetup.RightMargin, _
>
>Height:=250)
>
> With shpCanvas
> .Visible = msoTrue
> .Select
> End With
> Selection.ShapeRange.WrapFormat.Type = wdWrapTopBottom
>
>
> Selection.ShapeRange.Fill.Solid
> Selection.ShapeRange.Line.Weight = 0.75
> Selection.ShapeRange.Line.Style = msoLineSingle
> Selection.ShapeRange.Line.Visible = msoTrue
> Selection.ShapeRange.Line.BackColor.RGB = RGB(255, 255, 255)
>
> End Sub
>
>I know that inserting a canvas through the menu results in the canvas'
>width matching exactly the printable area of the document (page width
>minus left marging minus right margin). But still I wish to know the
>right coding for this if done by VBA.
>
>

Re: Adding a drawing canvas via VBA that has the width of the printable area by Helmut

Helmut
Fri Jul 18 05:15:38 PDT 2008

Hi Andreas,

it's not the width,
it's the missing section object
set sect = selection.sections(1)

Sub InsertCanvasWidthPrintableArea()

Dim shpCanvas As Shape
Dim sect As Section
Set sect = Selection.Sections(1)
'Add a new drawing canvas to the active document

Set shpCanvas = _
ActiveDocument.Shapes.AddCanvas( _
Left:=70, Top:=75, _
Width:=sect.PageSetup.PageWidth - _
sect.PageSetup.LeftMargin - _
sect.PageSetup.RightMargin, _
Height:=250)

With shpCanvas
.Visible = msoTrue
.Select
End With
Selection.ShapeRange.WrapFormat.Type = wdWrapTopBottom


Selection.ShapeRange.Fill.Solid
Selection.ShapeRange.Line.Weight = 0.75
Selection.ShapeRange.Line.Style = msoLineSingle
Selection.ShapeRange.Line.Visible = msoTrue
Selection.ShapeRange.Line.BackColor.RGB = RGB(255, 255, 255)

End Sub


--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP

RE: Adding a drawing canvas via VBA that has the width of the printabl by JeanGuyMarcil

JeanGuyMarcil
Fri Jul 18 05:33:01 PDT 2008

"andreas" wrote:

> Dear Experts:
>
> below code snippet tries to insert a drawing canvas in the current
> document. I would like the width of the canvas to match the printable
> area of the document (by subtracting the Left and Right Margin from
> the page width). Running the macro I get a runtime error 91 on the
> "Width"-portion of the code.
>
> The "width" coding is apparently false. How can I amend the code so
> that it is working?
>

You are using values drawn from a Section object called "sect", but you
never define that object.
Also, you do not need the selection object to manipulate the canvas, neither
do you need to set it to Visible.... You create a canvas object, so use that
to manipulate it. Much faster and more efficient.

Try this:


Sub InsertCanvasWidthPrintableArea()

Dim shpCanvas As Shape
Dim sect As Section

Set sect = Selection.Sections(1)

Set shpCanvas = ActiveDocument.Shapes.AddCanvas( _
Left:=70, _
Top:=75, _
Width:=sect.PageSetup.PageWidth - _
sect.PageSetup.LeftMargin - _
sect.PageSetup.RightMargin, _
Height:=250)

With shpCanvas
.WrapFormat.Type = wdWrapTopBottom
.Fill.Solid
With .Line
.Weight = 0.75
.Style = msoLineSingle
.Visible = msoTrue
.BackColor.RGB = RGB(255, 255, 255)
End With
End With

End Sub


Re: Adding a drawing canvas via VBA that has the width of the by andreas

andreas
Sun Jul 20 09:45:12 PDT 2008

On Jul 18, 2:15=A0pm, Helmut Weber <red....@t-online.de> wrote:
> Hi Andreas,
>
> it's not the width,
> it's the missing section object
> set sect =3D selection.sections(1)
>
> Sub InsertCanvasWidthPrintableArea()
>
> Dim shpCanvas As Shape
> Dim sect As Section
> Set sect =3D Selection.Sections(1)
> 'Add a new drawing canvas to the active document
>
> Set shpCanvas =3D _
> =A0 =A0ActiveDocument.Shapes.AddCanvas( _
> =A0 =A0Left:=3D70, Top:=3D75, _
> =A0 =A0Width:=3Dsect.PageSetup.PageWidth - _
> =A0 =A0sect.PageSetup.LeftMargin - _
> =A0 =A0sect.PageSetup.RightMargin, _
> =A0 =A0Height:=3D250)
>
> =A0 =A0 With shpCanvas
> =A0 =A0 =A0 =A0 .Visible =3D msoTrue
> =A0 =A0 =A0 =A0 .Select
> =A0 =A0 End With
> =A0 =A0 Selection.ShapeRange.WrapFormat.Type =3D wdWrapTopBottom
>
> =A0 =A0 Selection.ShapeRange.Fill.Solid
> =A0 =A0 Selection.ShapeRange.Line.Weight =3D 0.75
> =A0 =A0 Selection.ShapeRange.Line.Style =3D msoLineSingle
> =A0 =A0 Selection.ShapeRange.Line.Visible =3D msoTrue
> =A0 =A0 Selection.ShapeRange.Line.BackColor.RGB =3D RGB(255, 255, 255)
>
> =A0 =A0End Sub
>
> --
>
> Greetings from Bavaria, Germany
>
> Helmut Weber, MVP WordVBA
>
> Vista Small Business, Office XP

Hey Helmut, Thank you. It is working. Regards, Andreas

Re: Adding a drawing canvas via VBA that has the width of the by andreas

andreas
Sun Jul 20 09:45:55 PDT 2008

On Jul 18, 2:05=A0pm, Jay Freedman <jay.freed...@verizon.net> wrote:
> Offhand, I'd say it's because you declare the variable sect but you never=
assign
> a value to it. That means the expression sect.PageSetup isn't valid becau=
se sect
> is Null. I'm guessing that what you want is
>
> =A0 =A0Set sect =3D Selection.Sections(1)
>
> --
> Regards,
> Jay Freedman
> Microsoft Word MVP =A0 =A0 =A0 =A0FAQ:http://word.mvps.org
> Email cannot be acknowledged; please post all follow-ups to the newsgroup=
so all
> may benefit.
>
>
>
> On Fri, 18 Jul 2008 04:16:06 -0700 (PDT), andreas <andreas.her...@gmx.de>=
wrote:
> >Dear Experts:
>
> >below code snippet tries to insert a drawing canvas in the current
> >document. I would like the width of the canvas to match the printable
> >area of the document (by subtracting the Left and Right Margin from
> >the page width). Running the macro I get a runtime error 91 on the
> >"Width"-portion of the code.
>
> >The "width" coding is apparently false. How can I amend the code so
> >that it is working?
>
> >Help is much appreciated. Thank you very much in advance.
>
> >Sub InsertCanvasWidthPrintableArea()
>
> >Dim shpCanvas As Shape
> >Dim sect As Section
>
> > =A0 =A0'Add a new drawing canvas to the active document
>
> > =A0 =A0Set shpCanvas =3D ActiveDocument.Shapes.AddCanvas( _
> >Left:=3D70, _
> >Top:=3D75, _
>
> >Width:=3Dsect.PageSetup.PageWidth - _
> > =A0 =A0sect.PageSetup.LeftMargin - _
> > =A0 =A0sect.PageSetup.RightMargin, _
>
> >Height:=3D250)
>
> > =A0 =A0With shpCanvas
> > =A0 =A0 =A0 =A0.Visible =3D msoTrue
> > =A0 =A0 =A0 =A0.Select
> > =A0 =A0End With
> > =A0 =A0Selection.ShapeRange.WrapFormat.Type =3D wdWrapTopBottom
>
> > =A0 =A0Selection.ShapeRange.Fill.Solid
> > =A0 =A0Selection.ShapeRange.Line.Weight =3D 0.75
> > =A0 =A0Selection.ShapeRange.Line.Style =3D msoLineSingle
> > =A0 =A0Selection.ShapeRange.Line.Visible =3D msoTrue
> > =A0 =A0Selection.ShapeRange.Line.BackColor.RGB =3D RGB(255, 255, 255)
>
> > =A0 End Sub
>
> >I know that inserting a canvas through the menu results in the canvas'
> >width matching exactly the printable area of the document (page width
> >minus left marging minus right margin). But still I wish to know the
> >right coding for this if done by VBA.- Hide quoted text -
>
> - Show quoted text -

hey Jay. Yes you are right. That is exactly what I wanted. It is
working fine. Thank you. Regards, Andreas

Re: Adding a drawing canvas via VBA that has the width of the by andreas

andreas
Sun Jul 20 09:46:31 PDT 2008

On Jul 18, 2:33=A0pm, Jean-Guy Marcil
<JeanGuyMar...@discussions.microsoft.com> wrote:
> "andreas" wrote:
> > Dear Experts:
>
> > below code snippet tries to insert a drawing canvas in the current
> > document. I would like the width of the canvas to match the printable
> > area of the document (by subtracting the Left and Right Margin from
> > the page width). Running the macro I get a runtime error 91 on the
> > "Width"-portion of the code.
>
> > The "width" coding is apparently false. How can I amend the code so
> > that it is working?
>
> You are using values drawn from a Section object called "sect", but you
> never define that object.
> Also, you do not need the selection object to manipulate the canvas, neit=
her
> do you need to set it to Visible.... You create a canvas object, so use t=
hat
> to manipulate it. Much faster and more efficient.
>
> Try this:
>
> Sub InsertCanvasWidthPrintableArea()
>
> Dim shpCanvas As Shape
> Dim sect As Section
>
> Set sect =3D Selection.Sections(1)
>
> Set shpCanvas =3D ActiveDocument.Shapes.AddCanvas( _
> =A0 =A0 Left:=3D70, _
> =A0 =A0 Top:=3D75, _
> =A0 =A0 Width:=3Dsect.PageSetup.PageWidth - _
> =A0 =A0 sect.PageSetup.LeftMargin - _
> =A0 =A0 sect.PageSetup.RightMargin, _
> =A0 =A0 Height:=3D250)
>
> With shpCanvas
> =A0 =A0 .WrapFormat.Type =3D wdWrapTopBottom
> =A0 =A0 .Fill.Solid
> =A0 =A0 With .Line
> =A0 =A0 =A0 =A0 .Weight =3D 0.75
> =A0 =A0 =A0 =A0 .Style =3D msoLineSingle
> =A0 =A0 =A0 =A0 .Visible =3D msoTrue
> =A0 =A0 =A0 =A0 .BackColor.RGB =3D RGB(255, 255, 255)
> =A0 =A0 End With
> End With
>
> End Sub

Jean, Guy,
great thank your for the valuable advice. It works fine. Regards,
Andreas