none
inserting a picture into word and changing the position RRS feed

  • Question

  • Hi,

    I'm using vb.net to create a word document

    I've inserted a picture using the following code:

      Dim s As Word.InlineShape
                    s = oWord.Selection.InlineShapes.AddPicture(PictureLocation, False, True)
                  

    I can now set some properties of my picture e.g.

    s.width = 100

    But I can't set the left position of the picture. By default it lines the picture up with the left margin of the text in the document but i want it aligned with the table margin which is 0.21cm to the left of this

    When I recorded a macro to do it I got

    selection.shaperange.left = centimetrestopoints(-0.21)

    but I'm not sure how incorporate this into my code

    Thanks in advance for any help

    • Moved by Youen Zen Monday, March 25, 2013 7:02 AM From Visual Basic
    Sunday, March 24, 2013 5:10 PM

Answers

  • Hi Chris,

    oPara5.Range.PageSetup.LeftMargin = 20
    changes the page margin, not the paragraph indent. try something based on (vba):

    With oDoc
      .Range.Paragraphs.Last.Range.InsertParagraphAfter
      Set oPara5 = .Paragraphs.Last
      oPara5.Range.ParagraphFormat.LeftIndent = 20
      Set s = oPara5.Range.InlineShapes.AddPicture(PictureLocation, False, True)
      s.Width = 426
    End With


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by Seekaye Friday, March 29, 2013 11:15 AM
    Monday, March 25, 2013 9:31 PM

All replies

  • Hi Seekaye,

    I'll move this thread to Word for developers forum.

    Thanks,


    Shanks Zen
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, March 25, 2013 6:59 AM
  • Hi Seekaye,

    Inline shapes don't have a 'left' property; that is governed by the paragraph they're part of. Accordingly, you need to either: (a) adjust the paragraph format; or (b) convert the inlineshape to a shape so you can access the shape's 'left' property.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, March 25, 2013 9:33 AM
  • Hi,

    Thanks for your reply and sorry for posting in the wrong forum.

    I've tried to put the picture in a paragraph and set the left margin of the paragraph as follows:

     Dim oPara5 As Word.Paragraph
                    oPara5 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item("\endofdoc").Range)
                    oPara5.Range.InsertParagraphBefore()
                    s = oWord.Selection.InlineShapes.AddPicture(PictureLocation, False, True)
                    s.Width = 426
                    oPara5.Range.PageSetup.LeftMargin = 20
                    oPara5.Range.InsertParagraphAfter()

    Unfortunately this moves the margin of the entire document to 20.

    Is there any way I can move the margin of just the picture/this paragraph?

    Or would your second suggestion of converting the inlinesshape to a shape be easier?

    Many thanks for your help

    Chris


    Monday, March 25, 2013 6:02 PM
  • Hi Chris,

    oPara5.Range.PageSetup.LeftMargin = 20
    changes the page margin, not the paragraph indent. try something based on (vba):

    With oDoc
      .Range.Paragraphs.Last.Range.InsertParagraphAfter
      Set oPara5 = .Paragraphs.Last
      oPara5.Range.ParagraphFormat.LeftIndent = 20
      Set s = oPara5.Range.InlineShapes.AddPicture(PictureLocation, False, True)
      s.Width = 426
    End With


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by Seekaye Friday, March 29, 2013 11:15 AM
    Monday, March 25, 2013 9:31 PM
  • That's exactly what was needed.

    Many thanks for your help

    Chris

    Friday, March 29, 2013 11:16 AM
  • Hi Paul ,

    I have been searching Thread to set image position into word document.

    below is my code to insert image in to ms word using vb.net code.

    logoimg = Doc.Content.Paragraphs.Add
    logoimg.Range.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphJustifyHi
    logoimg.Range.InlineShapes.AddPicture(Environment.CurrentDirectory & "\Common\logo.jpg")

    I am lookin for code to make this image's position absolute.

    How can I set the absolute position of an image with top and left set to 0 with text through property 

    Any help would be very appreciated!

    Friday, March 27, 2015 1:48 PM
  • Similar post:

    https://social.msdn.microsoft.com/Forums/en-US/11cb1309-c9cf-4b62-9513-1031cd85f3b2/set-image-position-absolute-position-text-through-in-ms-word-using-vbnet-code?forum=worddev

    -----------------------------------------

    Free .NET Barcode Generator & Scanner supporting over 40 kinds of 1D & 2D symbologies.

    Monday, March 30, 2015 7:28 AM
  • I am lookin for code to make this image's position absolute.

    How can I set the absolute position of an image with top and left set to 0 with text through property

    You cannot make an inlineshape's postion absolute on the page - it must always be related to the paragraph it's inserted into. You can insert the inlineshape anywhere within the paragraph, but it's the paragraph's format and position on the page that provide the overriding boundaries of the inlineshape's positioning. If you need an absolute position on the page, you should use a shape object.

    Cheers
    Paul Edstein
    [MS MVP - Word]


    PS: I've been away for 3 1/2 months, hence the delay in replying.
    Friday, June 5, 2015 11:36 AM