Results 1 to 9 of 9
  1. #1
    3 Star Lounger
    Join Date
    Jun 2004
    Location
    Oxford, Oxfordshire, United Kingdom
    Posts
    203
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Macros and Margins (XP SR3)

    I've just been teaching myself VBA programming while on holiday, and have written a set of procedures that make inserting pictures and captions into a Word file a lot easier--this is something I need for the magazine I edit.

    There are a few teething problems at the end of the procedures.

    I'll isolate here one problem which may illustrate something I just don't get (and may be at the root of a number of other bugs in the routines). The routine given below is designed to produce a caption immediately below a picture placed at the top centre of the page, with word wrap Top and Bottom (a standard procedure in the magazine), and is to be activated once the picture has been clicked on.

    Dim PictureWidth, PictureHeight, DocumentMarginTop
    DocumentMarginTop = ActiveDocument.PageSetup.TopMargin
    PictureHeight = Selection.ShapeRange.Height
    PictureWidth = Selection.ShapeRange.Width
    ActiveDocument.Shapes.AddTextbox(msoTextOrientatio nHorizontal, 0, DocumentMarginTop, PictureWidth, 35#).Select
    ' places the box at the left of the page and under the top margin
    Selection.ShapeRange.RelativeHorizontalPosition = _
    wdRelativeHorizontalPositionMargin
    Selection.ShapeRange.RelativeVerticalPosition = _
    wdRelativeVerticalPositionMargin
    ' these I put in in the hope of coping with the different margin settings on the page
    Selection.ShapeRange.Left = wdShapeCenter
    ' centres the caption box
    Selection.ShapeRange.IncrementTop PictureHeight
    ' places the box below the picture
    this should position the box. What follows removes the lines around the box, and fixes word wrapping
    Selection.ShapeRange.Fill.Transparency = 0#
    Selection.ShapeRange.Line.Transparency = 0#
    Selection.ShapeRange.Line.Visible = msoFalse
    Selection.ShapeRange.LockAnchor = False
    Selection.ShapeRange.WrapFormat.AllowOverlap = True
    Selection.ShapeRange.WrapFormat.DistanceTop = CentimetersToPoints(0)
    Selection.ShapeRange.WrapFormat.DistanceBottom = CentimetersToPoints(0.2)
    Selection.ShapeRange.TextFrame.WordWrap = True
    Selection.ShapeRange.IncrementTop CentimetersToPoints(-0.2)
    ' this line is there because the routine placing the picture at the top centre in the first place has set a 0.3 margin
    Selection.ShapeRange.WrapFormat.Type = wdWrapTopBottom

    (then there follows a procedure for inserting the caption's content).

    What I am finding is that this procedure works fine as regards horizontal positioning, but that the heights on the page at which the caption box is inserted vary
    Sometimes it works as it should
    Sometimes the caption is too low, and the extent of the discrepancy is exactly equal to the height of the picture
    Sometime the caption appears under the top margin and covers over the picture
    Sometimes nothing appears at all.
    Sometimes the caption appears above the picture (and hence overlapping the header)

    These behaviours occur with the same picture being placed on successive pages of the same document--both in standard A4 equal margins, and in the magazine template, which has mirror margins and different values for top and bottom margins.

    Can anyone see what the problem is? I'd be most grateful.

    best wishes

    Philip

    (It may be helpful if I also add the code by which I'm fixing the picture at top centre:

    Selection.ShapeRange.RelativeHorizontalPosition = _
    wdRelativeHorizontalPositionMargin
    Selection.ShapeRange.RelativeVerticalPosition = _
    wdRelativeVerticalPositionMargin
    Selection.ShapeRange.Left = wdShapeCenter
    Selection.ShapeRange.Top = wdShapeTop
    Selection.ShapeRange.LockAnchor = False
    Selection.ShapeRange.WrapFormat.AllowOverlap = True
    Selection.ShapeRange.WrapFormat.DistanceTop = CentimetersToPoints(0)
    Selection.ShapeRange.WrapFormat.DistanceBottom = CentimetersToPoints(0.3)
    Selection.ShapeRange.WrapFormat.DistanceLeft = CentimetersToPoints(0.32)
    Selection.ShapeRange.WrapFormat.DistanceRight = CentimetersToPoints(0.32)
    Selection.ShapeRange.WrapFormat.Type = wdWrapTopBottom
    )



    My guess is that I have the wrong end of the stick about variables

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macros and Margins (XP SR3)

    Try setting

    Selection.ShapeRange.Top = PictureHeight

    If the picture is flush with the top of the page, use wdRelativeVerticalPositionPage instead of wdRelativeVerticalPositionMargin.

  3. #3
    3 Star Lounger
    Join Date
    Jun 2004
    Location
    Oxford, Oxfordshire, United Kingdom
    Posts
    203
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Macros and Margins (XP SR3)

    Thanks for this. I'm not quite sure how I've got this right, but it seems to be working, at least for the moment.

    What I'm finding difficult is the fact that I don't seem, at least not always, to be able to write something like
    Dim PictureTop
    PictureTop = Selection.ShapeRange.Top

    Most of the time, that variable doesn't seem to take.

    many thanks--this board is wonderful

    Philip

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macros and Margins (XP SR3)

    Please keep in mind that Top is relative to the shape's anchor. If a shape is anchored to a paragraph, Top is relative to the top of the paragraph, etc.

  5. #5
    3 Star Lounger
    Join Date
    Jun 2004
    Location
    Oxford, Oxfordshire, United Kingdom
    Posts
    203
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Macros and Margins (XP SR3)

    Ahhh ... Anchors are a secret that Microsoft keeps to itself.

    Nevertheless, I don't quite understand what you say about them. As I understand them--with the help of Woody's book, given the complete absence of documentation from Bill Gates--a picture is ALWAYS anchored to a paragraph. You can specify that the positioning of the picture be relative to page or margin, but it's the anchor which determines which page or margin the picture is linked to--if the anchor moves page, then the picture comes with it.. But that may be abstract now, given that I THINK my caption routines are now working.

    One question about credit boxes if I may. I think I've got a routine (see below) that is working on ordinary A4 with even margins, providing me with a thin box and sideways lettering just to the left of the picture, wherever it is placed.
    But they are still faulty as regards horizontal placing some of the time (not always) when I work with my magazine template. Could this be becuase the template has mirror margins? And if so, is there any way I can compensate for that?
    It also, again only occasionally, has the effect of displacing the original picture horizontally. My guess is that this has nothing to do with the macro, because sometimes when you alter a setting in a picture position manually, it has the effect of sending the whole picture haywire. But I'd be grateful for any light that could be shed. Working with pictures in Word can be really frustrating at times ...

    best wishes and MANY thanks

    Philip

    Routine:

    Selection.ShapeRange.RelativeHorizontalPosition = _
    wdRelativeHorizontalPositionMargin
    Selection.ShapeRange.RelativeVerticalPosition = _
    wdRelativeVerticalPositionMargin
    Selection.ShapeRange.IncrementTop (25)
    Selection.ShapeRange.IncrementTop (-25)
    Dim PictureHeight, PictureWidth, DocumentMarginTop, DocumentMarginLeft
    PictureHeight = Selection.ShapeRange.Height
    PictureWidth = Selection.ShapeRange.Width
    DocumentMarginTop = ActiveDocument.PageSetup.TopMargin
    DocumentMarginLeft = ActiveDocument.PageSetup.LeftMargin
    Dim PictureLeft, PictureTop
    PictureTop = Selection.ShapeRange.Top + DocumentMarginTop
    PictureLeft = Selection.ShapeRange.Left + DocumentMarginLeft
    Dim TextBoxLeft
    TextBoxLeft = PictureLeft + PictureWidth
    ActiveDocument.Shapes.AddTextbox(msoTextOrientatio nUpward, TextBoxLeft, PictureTop, 10, PictureHeight).Select
    Selection.ShapeRange.RelativeHorizontalPosition = _
    wdRelativeHorizontalPositionMargin
    Selection.ShapeRange.RelativeVerticalPosition = _
    wdRelativeVerticalPositionMargin


    .

  6. #6
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts

    Re: Macros and Margins (XP SR3)

    Hi Philip,

    Here's a slightly different approach for you to try. It puts both the selected picture and the caption in the same text box, thus avoiding any issues of pictures and captions not being anchored to the same object.

    Cheers

    Dim PictureWidth, PictureHeight, DocumentMarginTop
    DocumentMarginTop = ActiveDocument.PageSetup.TopMargin
    With Selection
    PictureHeight = .ShapeRange.Height + 50
    PictureWidth = .ShapeRange.Width
    .Cut
    End With
    ActiveDocument.Shapes.AddTextbox(msoTextOrientatio nHorizontal, 0, DocumentMarginTop, PictureWidth, PictureHeight).Select
    With Selection
    .Paste
    .TypeText Text:=Chr(11) & "Your caption goes here"
    With .ShapeRange
    .TextFrame.MarginLeft = 0#
    .TextFrame.MarginRight = 0#
    .TextFrame.MarginTop = 0#
    .TextFrame.MarginBottom = 0#
    .IncrementTop CentimetersToPoints(-0.2)
    .RelativeHorizontalPosition = wdRelativeHorizontalPositionMargin
    .RelativeVerticalPosition = wdRelativeVerticalPositionMargin
    .Left = wdShapeCenter
    .Fill.Transparency = 0#
    With .Line
    .Transparency = 0#
    .Visible = msoFalse
    End With
    .LockAnchor = False
    With .WrapFormat
    .AllowOverlap = True
    .Side = wdWrapBoth
    .DistanceTop = CentimetersToPoints(0)
    .DistanceBottom = CentimetersToPoints(0)
    .DistanceLeft = CentimetersToPoints(0.32)
    .DistanceRight = CentimetersToPoints(0.32)
    .Type = wdWrapTopBottom
    End With
    End With
    End With
    Attached Files Attached Files
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  7. #7
    3 Star Lounger
    Join Date
    Jun 2004
    Location
    Oxford, Oxfordshire, United Kingdom
    Posts
    203
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Macros and Margins (XP SR3)

    Thanks for this, which looks very clever indeed. Yesterday was a public holiday in the UK and I could play--today I must do some work. But two initial questions.

    a) at the moment, this routine on the simplest test document moves the picture to top centre? Can it be told to leave the picture where it is?
    [img]/forums/images/smilies/cool.gif[/img] my credits are written sideways, whereas my captions are written horizontally as normal. Am I right in thinking that this approach will generate either captions or credits but not both?

    best

    Philip

  8. #8
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts

    Re: Macros and Margins (XP SR3)

    <P ID="edit" class=small>(Edited by macropod on 31-Aug-05 08:29. Minor Code Revision)</P>Hi Philip,

    Try the attached and see if you like the results. It uses a table embedded in the text box so that you can have both a certical creadit and horizontal caption.

    You'll probably need to tweak some of the units to suit your fonts/point sizes, etc.

    Cheers
    Attached Files Attached Files
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  9. #9
    3 Star Lounger
    Join Date
    Jun 2004
    Location
    Oxford, Oxfordshire, United Kingdom
    Posts
    203
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Macros and Margins (XP SR3)

    Thanks. I'm just going to go away for three days, and will play with it while I'm away. At the moment, it seems to be losing most of the picture ... but I'll investigate further

    Philp

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •