Error when adding a picture
Hi guys :wave:
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 :wave:
Re: Error when adding a picture
i would imagine that as shape is an object, you would need the SET keyword
Re: Error when adding a picture
Thanks :wave:
That worked :thumb:
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 ? :confused:
:wave:
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
Quote:
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
Re: Error when adding a picture
Quote:
Originally Posted by
westconn1
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
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 ?
:wave:
Re: Error when adding a picture
Quote:
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?
Quote:
This looks like the same thing in VB.Net and VB6 for assigning objects. Isn't it ?
i guess
Re: Error when adding a picture
Quote:
Originally Posted by
westconn1
did you test if pp is a shape or inlineshape?
hmm.. How to do that ? :confused:
Re: Error when adding a picture
Quote:
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 +
Re: Error when adding a picture
Quote:
Originally Posted by
westconn1
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 ?
:wave:
Re: Error when adding a picture
from msdn
Quote:
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.
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