Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    3 Star Lounger
    Join Date
    May 2002
    Location
    Melbourne, Australia
    Posts
    269
    Thanks
    32
    Thanked 0 Times in 0 Posts

    Word 2010 macro for position of images

    I have recorded a macro for inserting an image into a document as I do it regularly. When I then tried to set the position of the image, I found that this control is greyed out on the toolbar, so I could not include it in my macro.

    The only other alternative is to write the necessary VBA code for this, but I'm not a programmer.

    I would be very grateful if someone could tell me how to select the position of an image to include in my macro.

    Thank you.

  2. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Try something based on:
    Code:
    Sub Demo()
    Application.ScreenUpdating = False
    Dim Rng As Range, Shp As Shape
    With Application.Dialogs(wdDialogInsertPicture)
      .Display
      If .Name <> "" Then
        Set Rng = Selection.Range
        Rng.Collapse
        Set Shp = ActiveDocument.InlineShapes.AddPicture(FileName:=.Name, _
          SaveWithDocument:=True, Range:=Rng).ConvertToShape
        With Shp
          .LockAspectRatio = True
          .Left = CentimetersToPoints(1.5)
          .Top = CentimetersToPoints(0.5)
          .Width = CentimetersToPoints(2.5)
          .WrapFormat.Type = wdWrapBehind
        End With
      GoTo Done
      End If
    End With
    Done:
    Set Rng = Nothing: Set Shp = Nothing
    Application.ScreenUpdating = True
    End Sub
    Of course, if it's always the same image, the dialogue box can be dispensed with and you could use something like:
    Code:
    Sub Demo()
    Application.ScreenUpdating = False
    Dim Rng As Range, Shp As Shape, StrImg As String
    StrImg = "C:\Users\" & Environ("Username") & "\Somefolder\MyPicture.jpg"
    Set Rng = Selection.Range
    Rng.Collapse
    Set Shp = ActiveDocument.InlineShapes.AddPicture(FileName:=StrImg, _
      SaveWithDocument:=True, Range:=Rng).ConvertToShape
    With Shp
      .LockAspectRatio = True
      .Left = CentimetersToPoints(1.5)
      .Top = CentimetersToPoints(0.5)
      .Width = CentimetersToPoints(2.5)
      .WrapFormat.Type = wdWrapBehind
    End With
    Set Rng = Nothing: Set Shp = Nothing
    Application.ScreenUpdating = True
    End Sub
    Note that I've used Word's 'CentimetersToPoints' function and that I've used the 'WrapFormat' property to position the image behind the text. There's also an 'InchesToPoints' function and different 'WrapFormat' properties, if you prefer.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  3. The Following User Says Thank You to macropod For This Useful Post:

    KayPat (2012-12-14)

  4. #3
    3 Star Lounger
    Join Date
    May 2002
    Location
    Melbourne, Australia
    Posts
    269
    Thanks
    32
    Thanked 0 Times in 0 Posts
    Thank you for the code Paul. I really appreciate you taking the time to do this!

    If, as it appears, there is no way to turn the 'Position' function back on in Word 2010 when recording the macro, but it will stay greyed out and I will have to use VBA code for positioning the image, then I need to set out exactly how I would have completed the Position dialogue box, if only I could:

    1. I always use the same image (so that part of your code is fine);
    2. The size of the image does not need to be changed, it is inserted just as it is;
    3. Text wrapping is 'Top and Bottom', which I have set up in Word as the default, so I don't know if that has to be included here, but I don't know the syntax;
    4. Position | Horizontal is 'Centered' relative to 'Page';
    5. Position | Vertical is 'Absolute position' of '0.47 inches' below 'Top Margin'.

    I would be extremely grateful if you could add this into the second code you wrote.

    Thank you very much in anticipation.

    Regards
    Useful

  5. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Try:
    Code:
    With Shp
      .LockAspectRatio = True
      .RelativeHorizontalPosition = wdRelativeHorizontalPositionMargin
      .Left = wdShapeCenter
      .RelativeVerticalPosition = wdRelativeVerticalPositionMargin
      .Top = InchesToPoints(0.47)
    End With
    Note: I'd still suggest setting the sizes also, to ensure things don't get messed up if the image gets edited and the image editor outputs the image at a different resolution or with different metadata.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  6. The Following User Says Thank You to macropod For This Useful Post:

    KayPat (2012-12-14)

  7. #5
    3 Star Lounger
    Join Date
    May 2002
    Location
    Melbourne, Australia
    Posts
    269
    Thanks
    32
    Thanked 0 Times in 0 Posts
    Hi Paul

    Not being a VBA programmer has been my undoing it seems.

    I tried to combine the two pieces of code you so kindly wrote for me, but it 'crashes'. Rather than try and fix the errors, I hope you will be kind enough to combine the bits of code into a single one, because clearly I can't.

    To re-iterate:

    1. I always want to insert the same image (I have worked out the path, so I can replace the data in the code you sent for this);
    2. The size of the image does not change, but I take your point. The Height is 1.03" and the Width is 1.81";
    3. Text wrapping is 'Top and Bottom';
    4. Position | Horizontal is 'Centered' relative to 'Page';
    5. Position | Vertical is 'Absolute position' of '0.47 inches' below 'Top Margin'.

    Thank you in advance.

    Regards
    George

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

    C'mon, it was nothing more complicated than replacing one 'With Shp ... End With' block with the other!! This isn't programming - it's just copy & paste.

    Assuming you're using the second version of the code in my first post, have you updated the 'StrImg' variable to point to the correct filename & path?

    If both of those have been done correctly, you'll need to tell me what happens error message you get when the code crashes and, if a code line gets highlighted, what that line is.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  9. #7
    3 Star Lounger
    Join Date
    May 2002
    Location
    Melbourne, Australia
    Posts
    269
    Thanks
    32
    Thanked 0 Times in 0 Posts
    Hi Paul

    Clearly this is easy for you, but obviously not so much for me. I can copy and paste without a problem, but when I get this error:

    'Run-time error '5152':

    This is not a valid file name.

    running the macro and Debug points to:

    Set Shp = ActiveDocument.InlineShapes.AddPicture(FileName:=S trImg, _
    SaveWithDocument:=True, Range:=Rng).ConvertToShape

    I'm not quite sure what to do.

    I have used your second code, replaced the old 'With Shp ... End With' block with the new one, and completed the 'StrImg' variable with the correct path and file name pointing to the required image. I tried it first in parts as per your code and when I got this error, I tried it replacing the parts with the full path and file name all in one, such as "e:\user\full_path\filename.jpg". That resulted in the same error and each time Debug pointed to the same section of code (as above).

    I wouldn't waste your time, if I wasn't stuck.

    Regards
    George

  10. #8
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Quote Originally Posted by useful View Post
    I can copy and paste without a problem, but when I get this error:

    'Run-time error '5152':
    This is not a valid file name.

    running the macro and Debug points to:
    Set Shp = ActiveDocument.InlineShapes.AddPicture(FileName:=S trImg, _
    SaveWithDocument:=True, Range:=Rng).ConvertToShape

    I'm not quite sure what to do.

    I have used your second code, replaced the old 'With Shp ... End With' block with the new one, and completed the 'StrImg' variable with the correct path and file name pointing to the required image. I tried it first in parts as per your code and when I got this error, I tried it replacing the parts with the full path and file name all in one, such as "e:\user\full_path\filename.jpg". That resulted in the same error and each time Debug pointed to the same section of code (as above).
    Hi George,

    In that case, having me supply you with the completed code wasn't going to change anything - you'd get the same error. That's because I can't provide one vital piece of information: the full path and filename for the image. In the code, you'll see the line:
    StrImg = "C:\Users\" & Environ("Username") & "\Somefolder\MyPicture.jpg"
    That line needs to be edited to point to wherever your image is, and include the actual image name. For example, if it's in your 'MyPictures' folder, you might use:
    StrImg = "C:\Users\" & Environ("Username") & "\Pictures\MyPicture.jpg"
    or, perhaps:
    StrImg = "C:\Users\Useful George\Pictures\MyPicture.jpg"
    where 'MyPicture.jpg' is the image name.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  11. #9
    3 Star Lounger
    Join Date
    May 2002
    Location
    Melbourne, Australia
    Posts
    269
    Thanks
    32
    Thanked 0 Times in 0 Posts
    Hi Paul

    In the end, rather than fix the code, which I couldn't, it 'crashed' with the same error no matter what I did, I have copied the image file to "C:\Users\George" and it now works without error, so we are almost there, but:


    1. Text wrapping is 'In front of text' instead of 'Top and Bottom'
    2. Vertical alignment is below 'Margin' instead of below 'Top Margin'

    and unfortunately, although I have tried, I don't know the correct syntax to change the code.

    I would be grateful for your help ... hopefully I'll be asking for the last time.

    Regards
    George

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

    For the text wrapping, the possible settings are:

    Name - Description
    wdWrapInline - Places shapes in line with text.
    wdWrapNone - Places shape in front of text. See also wdWrapFront.
    wdWrapSquare - Wraps text around the shape. Line continuation is on the oposite side of the shape.
    wdWrapThrough - Wraps text around the shape.
    wdWrapTight - Wraps text close to the shape.
    wdWrapTopBottom - Places text above and below the shape.
    wdWrapBehind - Places shape behind text.
    wdWrapFront - Places shape in front of text.

    You probably want wdWrapTopBottom

    For the vertical alignment, the possible settings are:

    Name - Description
    wdRelativeVerticalPositionLine - Relative to line.
    wdRelativeVerticalPositionMargin - Relative to margin.
    wdRelativeVerticalPositionPage - Relative to page.
    wdRelativeVerticalPositionParagraph - Relative to paragraph.
    wdRelativeVerticalPositionBottomMarginArea - Relative to bottom margin.
    wdRelativeVerticalPositionInnerMarginArea - Relative to inner margin area.
    wdRelativeVerticalPositionOuterMarginArea - Relative to outer margin area.
    wdRelativeVerticalPositionTopMarginArea - Relative to top margin.

    There is no 'Top Margin' setting, so I suspect you might want wdRelativeVerticalPositionPage
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  13. The Following User Says Thank You to macropod For This Useful Post:

    useful (2012-09-03)

  14. #11
    3 Star Lounger
    Join Date
    May 2002
    Location
    Melbourne, Australia
    Posts
    269
    Thanks
    32
    Thanked 0 Times in 0 Posts
    Hi Paul

    With a bit of trial and error, as well as looking things up some more, I have managed to get it working. Thank you for your generous help with this.

    Regards
    George

  15. #12
    New Lounger
    Join Date
    Dec 2012
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    VB Code for writing my path

    Paul,

    Your codes for inserting an image behind text was absolutely great. I got it to work, except being completely new to VB, i have no idea how to write my path that gets my image,(which will be the same all the time)from my network drive. am going to my C drive then T drive and then my jpeg file...How do i write this path???? Please help!!!

  16. #13
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    KayPat,

    If your network drive is mapped to T: use StrImg = "T:\Somefolder(s)\MyPicture.jpg" or
    if you want you can use a UNC like this: StrImg = "\\servername\Somefolder(s)\MyPicture.jpg".
    For example if I was using my NAS \\COMPUTERMENTOR\CMShared\Pictures\filename.jpg".
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  17. #14
    New Lounger
    Join Date
    Dec 2012
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Doesnt work ;-(

    Here is the code i am using. My file i want it to get is called Signature, i would usually get to it from c drive to t drive to signature jpg, what am i doing wrong???


    Sub Demo()
    Application.ScreenUpdating = False
    Dim Rng As Range, Shp As Shape, StrImg As String
    StrImg = "T:\Somefolder(s)\signature.jpg"
    Set Rng = Selection.Range
    Rng.Collapse
    Set Shp = ActiveDocument.InlineShapes.AddPicture(FileName:=S trImg, _
    SaveWithDocument:=True, Range:=Rng).ConvertToShape
    With Shp
    .LockAspectRatio = True
    .Left = CentimetersToPoints(1.5)
    .Top = CentimetersToPoints(0.5)
    .Width = CentimetersToPoints(2.5)
    .WrapFormat.Type = wdWrapBehind
    End With
    Set Rng = Nothing: Set Shp = Nothing
    Application.ScreenUpdating = True
    End Sub

  18. #15
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    KayPat,

    You have to replace the "Somefolder(s)" with the appropriate folder path on your network drive.
    Ex: if I had my network drive mapped to T: I would use T:\CMShared\Pictures\filename.jpg.
    of course replacing filename with an actual filename line your "signature".
    If you are using Win7 or 8 you can navigate to the file in windows explorer then right-click on the address bar and select "Copy address as text" and then paste into your code and just add the filename.
    Last edited by RetiredGeek; 2012-12-14 at 12:27.
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

Page 1 of 2 12 LastLast

Posting Permissions

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