• Word 2010 macro for position of images

    Author
    Topic
    #484861

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

    Viewing 11 reply threads
    Author
    Replies
    • #1345799

      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") & "SomefolderMyPicture.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
      [Fmr MS MVP - Word]

      • #1346848

        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

    • #1346850

      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
      [Fmr MS MVP - Word]

    • #1347594

      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

    • #1347595

      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
      [Fmr MS MVP - Word]

    • #1347600

      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:=StrImg, _
      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:userfull_pathfilename.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

      • #1347652

        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:=StrImg, _
        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:userfull_pathfilename.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”) & “SomefolderMyPicture.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”) & “PicturesMyPicture.jpg”
        or, perhaps:
        StrImg = “C:UsersUseful GeorgePicturesMyPicture.jpg”
        where ‘MyPicture.jpg’ is the image name.

        Cheers,
        Paul Edstein
        [Fmr MS MVP - Word]

    • #1347656

      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:UsersGeorge” 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

    • #1347657

      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
      [Fmr MS MVP - Word]

    • #1347712

      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

      • #1362694

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

    • #1362697

      KayPat,

      If your network drive is mapped to T: use [noparse]StrImg = “T:Somefolder(s)MyPicture.jpg”[/noparse] or
      if you want you can use a UNC like this: [noparse]StrImg = “\servernameSomefolder(s)MyPicture.jpg”[/noparse].
      For example if I was using my NAS [noparse]\COMPUTERMENTORCMSharedPicturesfilename.jpg”[/noparse].:cheers:

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

      • #1362698

        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:=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

    • #1362700

      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 [noparse]T:CMSharedPicturesfilename.jpg[/noparse].
      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.:cheers:

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

      • #1362703

        As you can see, i am completely clueless, maybe thats the reason girls dont do this ;-( here is what i am doing, just as you said, i know i am still getting the path wrong..Is there Anyway to make this easier or better for me to understand? This is the only part i am having an issue with. It will not let me copy and paste the path either!!!

        Sub Demo()
        Application.ScreenUpdating = False
        Dim Rng As Range, Shp As Shape, StrImg As String
        StrImg = “T:CMSharedPicturessignature.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

      • #1362705

        I thought it would be easier considering i am using the same image all the time. The only other requirement is that the image is on a network drive !! does this make for an easier solution??

    • #1362716

      When you use Windows Explorer to navigate to the image folder and click in the address bar, what do you see in the address bar? That is the folder path. Simply copy & insert that, plus “” and your image name after ‘StrImg = ‘ and the code should work.

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    • #1362747

      KayPat,

      Ok, lets try again.

        [*]Open Notepad.
        [*]Open Windows Explorer (not Internet Explorer).
        [*]Navigate to the folder with your “Signature.jpg” file.
        [*]Right-click on the address bar as shown below and select “Copy address as text”
        32607-CopyAddresws
        [*]Click on the Notepad icon on the taskbar.
        [*]Click in the Notepad window to get you cursor there.
        [*]Press Ctrl+v
        32608-Notepad
        [*]Close Windows Explorer.
        [*]Open the Visual Basic Editor (VBE) in Word and locate your code.
        [*]Click on the NotePad icon on the taskbar and press Ctrl+a then Ctrl+c
        [*]Click on the Word VBE window where your code is and highlight the line with the path info as shown in blue.
        StrImg = “T:CMSharedPicturessignature.jpg.”
        [*]Press Ctrl+v.
        [*]This should now have placed the proper drive/path information in your code.:cheers:

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

    Viewing 11 reply threads
    Reply To: Word 2010 macro for position of images

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: