Results 1 to 10 of 10

Thread: Error when adding a picture

  1. #1

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Question Error when adding a picture

    Hi guys

    This is the code that worked without any problems in Word2007:
    Code:
    Dim pp As Shape
            pp = ActiveDocument.Bookmarks.Item("\StartOfDoc").Range.InlineShapes.AddPicture(FileName:="c:\abc.jpg", LinkToFile:=False, SaveWithDocument:=True).ConvertToShape
    But when I tried in Word2003, it shows an error:
    Code:
    Object variable or with variable not set
    Any ideas ?

    Thanks

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Error when adding a picture

    i would imagine that as shape is an object, you would need the SET keyword
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Error when adding a picture

    Thanks

    That worked

    So, in Word2007, it uses the .Net style and in Word2003, it is VB6 style ?

    I have another problem experiencing with this insertion of image.
    I have a document with an image in it plus some text.

    And I use this code to obtain the Image's Left, Top, Height and Width.
    Code:
    le = ActiveDocument.Shapes(1).Left
    to = ActiveDocument.Shapes(1).Top
    he = ActiveDocument.Shapes(1).Height
    wi = ActiveDocument.Shapes(1).Width
    But when I use this value to insert the image in another document, the image is placed somewhere different than the given Top and Left values. But if I again check the image's Left and Top(of the newly created document), it is the same one as I have given to it. This works fine in 2007.
    Code:
                Dim pp As Shape
                Set pp = ActiveDocument.Bookmarks.Item("\StartOfDoc").Range.InlineShapes.AddPicture(FileName:="C:\abc.jpg", LinkToFile:=False, SaveWithDocument:=True).ConvertToShape
               
                pp.WrapFormat.Type = 5
                pp.LockAspectRatio = msoFalse
                pp.Height = 581.1 'he
                pp.Width = 244.95 'wi
                pp.Left = -21.45 'le
                pp.Top = -21.5 'to
    Any ideas ?


    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Error when adding a picture

    you may need to convert the inlineshape to a shape, as inlineshapes can not be positioned absolutely, as they move in relation to text insertions etc

    So, in Word2007, it uses the .Net style and in Word2003, it is VB6 style ?
    as i still use office 2000 i can not compare what goes on in 2007, but most of my code appears to work for posters using 2007 and 2010
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Error when adding a picture

    Quote Originally Posted by westconn1 View Post
    you may need to convert the inlineshape to a shape, as inlineshapes can not be positioned absolutely, as they move in relation to text insertions etc
    I am already doing it using the ConvertToShape() method. (See the end of that line)

    Quote Originally Posted by westconn1 View Post
    as i still use office 2000 i can not compare what goes on in 2007, but most of my code appears to work for posters using 2007 and 2010
    hmm.. But in 2010, I used without the set keyword and no error was shown. But in 2003, it has to be used with the Set keyword. This looks like the same thing in VB.Net and VB6 for assigning objects. Isn't it ?


    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Error when adding a picture

    I am already doing it using the ConvertToShape() method. (See the end of that line)
    i did not scroll that far

    did you test if pp is a shape or inlineshape?

    This looks like the same thing in VB.Net and VB6 for assigning objects. Isn't it ?
    i guess
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Error when adding a picture

    Quote Originally Posted by westconn1 View Post
    did you test if pp is a shape or inlineshape?
    hmm.. How to do that ?

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Error when adding a picture

    hmm.. How to do that ?
    no need, as it is declared a shape, it has to be

    in that case i do not know why it will not stay in position, check the anchors are as expected, they may default differently to word 2007 +
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Error when adding a picture

    Quote Originally Posted by westconn1 View Post
    no need, as it is declared a shape, it has to be

    in that case i do not know why it will not stay in position, check the anchors are as expected, they may default differently to word 2007 +
    Ok. Which anchor are you talking about ? The "\StartOfDoc" ? And how to do the checking ?


    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Error when adding a picture

    from msdn
    Anchor Property


    Returns a Range object that represents the anchoring range for the specified shape or shape range. Read-only.

    Remarks

    All Shape objects are anchored to a range of text but can be positioned anywhere on the page that contains the anchor. If you specify the anchoring range when you create a shape, the anchor is positioned at the beginning of the first paragraph that contains the anchoring range. If you don't specify the anchoring range, the anchoring range is selected automatically and the shape is positioned relative to the top and left edges of the page.

    The shape will always remain on the same page as its anchor. If the LockAnchor property for the shape is set to True, you cannot drag the anchor from its position on the page.

    If you use this property on a ShapeRange object that contains more than one shape, an error occurs.
    vb Code:
    1. pp.IncrementTop -pp.Top
    will move the picture to the top of the current page
    if you can find which page it is on, you can move it to the page you wish
    Last edited by westconn1; Nov 30th, 2011 at 03:34 PM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Click Here to Expand Forum to Full Width