Adding a drawing canvas via VBA that has the width of the printablearea

A

andreas

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

Jay Freedman

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
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
 
H

Helmut Weber

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
 
J

Jean-Guy Marcil

andreas said:
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
 
A

andreas

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

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

andreas

Offhand, I'd say it's because you declare the variable sect but you neverassign
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 newsgroupso all
may benefit.
















- Show quoted text -

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

andreas

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

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

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