PDA

View Full Version : Solved: Programmatically inserting pictures Word 2000 (9.0xxxx)



ukemike
05-18-2005, 08:29 AM
Guys,

Total bummer! The help that you guys provided me with on how to insert pictures programmatically from the web was perfect for Office 2003 and Office 2002, but I just found out that my client has office 2000. So they word version they are using is Word 2000 (9.0.2720).
I dont know if you can do it anymore? I recorded a macro while importing a picture but i dont know how to put it all together with the shape-size-image code.

Here is the code I was using:


Dim oShape As Word.Shape
Dim oPic As PicProps
With Application
.ScreenUpdating = False
.ActiveDocument.Bookmarks("bmShape").Select
Set oShape = .Selection.ShapeRange(1)
With oShape
oPic.Height = .Height
oPic.Width = .Width
oPic.Left = .Left
oPic.RLeft = .RelativeHorizontalPosition
oPic.Top = .Top
oPic.RTop = .RelativeVerticalPosition
End With
Set oShape = .ActiveDocument.Shapes.AddPicture _
(FileName:="http://www.vbaexpress.com/images/vbaxlogo.gif")
With oShape
.Height = oPic.Height
.Width = oPic.Width
.RelativeHorizontalPosition = oPic.RLeft
.Left = oPic.Left
.RelativeVerticalPosition = oPic.RTop
.Top = oPic.Top
End With
End With


So basically i was importing an image from the internet and shaping it according to the dimensions of a shape i had in my word document.
I tried to repost this to my old thread but i could figure out how to mark it from being solved to unsolved.

Here is the code of the Macro i recorded in Word 2000 (9.x.x.) of my importing a picture into the document.

Selection.InlineShapes.AddPicture FileName:= _
"http://us.a1.yimg.com/us.yimg.com/i/ww/beta/y3.gif", LinkToFile:=False, _
SaveWithDocument:=True



Any help on this is appreciated.
Thank you
Mike

ukemike
05-18-2005, 08:30 AM
I forgot to mention that i ran it, nothing crashes or anything, but the picture didnt come out.

MOS MASTER
05-18-2005, 09:58 AM
Hi, :D

Your recorded sub is adding an Inlinepicture! This is considerd a range object to Word and Shapes are in the Drawing layer of Word and are flowting!

So they are 2 entirly different objects!

Yes 2000 has a slightly different way of dealing with Shapes and InlineShapes.

I don't have 2000 at hand right now so I'll test it for you when I get home.

Post it A.S.A.P. (So Tommorow) :whistle:
PS..one question. Have you tried to edit the path of the Url to a local path (picture) so you now for sure that Downloading the picture isn't the problem here?

ukemike
05-18-2005, 02:08 PM
Hey,
Thanks for your response MOS.
Everything is O.K. with my downloads.


Thanks
Mike

MOS MASTER
05-18-2005, 02:13 PM
Hey,
Thanks for your response MOS.
Everything is O.K. with my downloads.


Thanks
Mike
Hi Mike, :yes

Ah ok so you did test with a local picture as wel?

I'll do a test for you later not home yet...:whistle:

ukemike
05-19-2005, 08:19 AM
MOS,
Did you find anything?

Thanks
Mike

MOS MASTER
05-19-2005, 10:00 AM
MOS,
Did you find anything?

Thanks
Mike
Hi Mike, :D

Nopes didn't find anything because my little old notebook(on which I have 2000 installed) wouldn't start last night..(Seams to be a power issue) :banghead:

Could you provide me with the testdocument you've used at your client. (Then I'll go and test it on friends computer)

Enjoy!

ukemike
05-19-2005, 10:18 AM
MOS,

Basically i got it figured out mostly.
What is the difference between how Word 2000 and Word 2002 handle ShapeRanges?

I cant select a shaperange for some reason. I drew a shape on the word document but then shaperange.count is 0. I dont get it.

I would provide you with a document, but there is no VBA code in the document. Im basically controlling Word through a C# app. Im translating VBA into --> C#.

Ok gotta go.
Thanks for your help.

Mike

MOS MASTER
05-19-2005, 10:25 AM
Hi Mike, :D

I don't know the difference between Shape handeling of 2000 and 2002. I need to have a 2000 machine for that and don't have that right now.

Your story on the Shaperange count being null wouldn't surprise me if you are using the record macro version you've posted before.

In that macro you where using an InlineShape so your shaperange count wil be 0 fur sure! (Shaperange don't hold any InlineShapes)

Can't do much more for you now...don't have the app to do it.

Good luck! :whistle:

ukemike
05-19-2005, 10:34 AM
MOS,

Yeah im going to work on it.
When you get a chance, if you could try it on word 2000 i def appreciate it.

Thanks for your other support.

Mike

MOS MASTER
05-19-2005, 10:37 AM
Yeah I'll try it for sure when I get the chance! :friends:

MOS MASTER
05-19-2005, 01:53 PM
Mike one more thought,

It seams strange to me that you can run the code in 2000 without receiving an error or a picture as result?

Have you compiled the code in the VBE (Error/Compile project)

Do you have "Option Explicit" at the top of the code module?

Does this little testsub Work in 2000:

Sub Test()
Dim oShape As Word.Shape
Set oShape = ActiveDocument.Shapes.AddPicture(FileName:="http://us.a1.yimg.com/us.yimg.com/i/ww/beta/y3.gif", LinkToFile:=False, _
SaveWithDocument:=True, Width:=150, Height:=150, _
Left:=100, Top:=100)
MsgBox oShape.Left
Set oShape = Nothing
End Sub


Later..:whistle:

ukemike
05-20-2005, 06:14 AM
MOS,

How are you doing?
The stub does work if i use a local picture ("c:\picture.gif"), it says invalid filename if i use an internet picture.
Also i tried modifying the width and height params of the oShape object and they do not do anything.


Thanx
Mike

MOS MASTER
05-20-2005, 10:51 AM
MOS,

How are you doing?
The stub does work if i use a local picture ("c:\picture.gif"), it says invalid filename if i use an internet picture.
Also i tried modifying the width and height params of the oShape object and they do not do anything.

Thanx
Mike
Hi Mike,

I'm fine thank you.

A little confused though!
Do you mean the Addpicture method of the shape object doesn't work with online pics and the same method does work with Inlineshapes! Hence your previous code:
Selection.InlineShapes.AddPicture FileName:= _
"http://us.a1.yimg.com/us.yimg.com/i/ww/beta/y3.gif", LinkToFile:=False, _
SaveWithDocument:=True


I couldn't find a pc with 2000 that was online for you so I've descided I will install it this weekend on another pc. (Can't live without it) ;)

So a little more patients pleace..:whistle:

ukemike
05-20-2005, 11:09 AM
MOS,

Again thanks you for your help.
Here is what happened.
I took that TestStub you posted (Sub Test()) and put it in a macro in a Word 2000 document. I ran it.
It said File not found.
I switched the file name from URL to a local file location on my C drive. It worked.

Afterwards after it worked I tried to change the shape of this oShape by doing:

Sub Test()
Dim oShape As Word.Shape
Set oShape = ActiveDocument.Shapes.AddPicture _
(Filename:="http://us.a1.yimg.com/us.yimg.com/i/ww/beta/y3.gif", _
LinkToFile:=False, SaveWithDocument:=True, _
Width:=150, Height:=150, Left:=100, Top:=100)
oShape.Height = 50
oShape.Width = 50
MsgBox oShape.Left
Set oShape = Nothing
End Sub

and nothing happened the image still stayed the same size.

I got you a little confused, sorry about it. Something weird happens. I just tested it out.

Basically, I turned on record macro. I went to insert -- > picture -- > from file. In the filename i entered: http://us.a1.yimg.com/us.yimg.com/i/ww/beta/y3.gif and clicked OK. The picture inserted into the document. I stopped macro record and look at the code that it generated:

Selection.InlineShapes.AddPicture FileName:= _
"http://us.a1.yimg.com/us.yimg.com/i/ww/beta/y3.gif", LinkToFile:=False, _
SaveWithDocument:=True

The weird thing is when i opened a new document and ran this macro i got runtime error '5152' which is 'This is not a valid filename'. This is the same problem that poped up when i ran that stub you posted (Sub Test()).
So it looks like there is a problem with downloading images.
Do you think it has to do something with security. I have all the windows defaults on, because this WinXP and Office 2000 is on a Virtual Machine. I just installed it like 5 days ago.

Now it kind of makes sense.

Thanks
Mike

MOS MASTER
05-20-2005, 11:38 AM
Hi Mike,

My hands are bound without 2000 to test so I can't add anything to the subject right now! :( (Allthough I'm sure there's a way to fix this.....)

ukemike
05-20-2005, 12:21 PM
MOS,

No problem. Whenever you have time.
I just wanted to clearify the problem.


Mike

TonyJollans
05-20-2005, 02:18 PM
I have tried this and can confirm everything you say about Word 2000. I can confirm it but I cannot make it work at the moment - I am investigating further.

MOS MASTER
05-20-2005, 02:22 PM
Thank you for joining Tony...I'll install tomorrow. :yes

mdmackillop
05-20-2005, 03:02 PM
Hi Mike,
If you select your code sections and click on the VBA button, it formats it as above and makes it more readable.
Regards
MD

MOS MASTER
05-21-2005, 09:34 AM
Hi Mike, :yes

Like Tony I can confirm the behaviour of Word 2000 towards the AddPicture method with shapes.

This must have something to to with 2000 default preference for Inlineshapes buth it could also be a bug!

I've been trying several approaches for you on my newly installed office 2000 :rofl:

This is the workaround that works the best:
Option Explicit

Private Type PicProps
Height As Double
Width As Double
Top As Double
RTop As Integer
Left As Double
RLeft As Integer
End Type

Sub AddMyPicture()
Dim oInlineShape As Word.InlineShape
Dim oShape As Word.Shape
Dim oPic As PicProps

With Application
.ScreenUpdating = False
.ActiveDocument.Bookmarks("bmShape").Select

Set oShape = .Selection.ShapeRange(1)

With oShape
oPic.Height = .Height
oPic.Width = .Width
oPic.Left = .Left
oPic.RLeft = .RelativeHorizontalPosition
oPic.Top = .Top
oPic.RTop = .RelativeVerticalPosition
End With

Set oInlineShape = .ActiveDocument.InlineShapes.AddPicture _
(FileName:="http://www.vbaexpress.com/images/vbaxlogo.gif", _
LinkToFile:=True, SaveWithDocument:=True)

Set oShape = oInlineShape.ConvertToShape

With oShape
.Height = oPic.Height
.Width = oPic.Width
.RelativeHorizontalPosition = oPic.RLeft
.Left = oPic.Left
.RelativeVerticalPosition = oPic.RTop
.Top = oPic.Top
End With

End With

Set oShape = Nothing
Set oInlineShape = Nothing
End Sub



I'm inserting as InlineShape and converting to shape later to easily positon the shape.

Hope it works as good for you as it did for me! (Test attachment included)

Enjoy! :whistle:

ukemike
05-23-2005, 10:39 AM
Hey guys I figures it out.
I am posing the C# code at the bottom of this post if you are interested. I think u can convert from C# to VBA. its the other way around thats kinda hard.
I am basically copying the image from the internet to my hard drive reading it from there and deleting it after i read it. There is some problem with programmatically importing images from the web into a word document.
Anyway for some reason Im having this weird problem in Word 2000. When i insert this image at full size, the position it and then shrink it down, the text does not move up.
When i insert the image the whole document moves down a little then i move the image to where its suppose to be and then shrink it, but after shrinking it the text does not move up and there is a big gap in the middle of the document right after where the image was inserted.
Anyway idea why or what properties I have to set. I thought there was like some VBA document collapse function??

Ok thanks guys.
Mike

Here is the code:



//imageFilename has to be on the local drive.



object bookmarkIndex = bookmark;

object rangeObject = 1;

object linktofile = false;

object savewithdocument = true;

object missing = System.Reflection.Missing.Value;

Word.Bookmark bm;

Word.Shape Shapes;

try



{

wordFile.Visible = true;

bm = wordFile.ActiveDocument.Bookmarks.Item(ref bookmarkIndex);

bm.Select();



string[] versionParse;

Word.Application dd = new Word.ApplicationClass();

versionParse = dd.Version.ToString().Split('.');



Shapes = wordFile.Selection.Range.ShapeRange.Item(ref rangeObject);

Shapes = wordFile.Selection.Range.ShapeRange.Item(ref rangeObject);



PicProps myPicture = new PicProps();

myPicture.Height = Shapes.Height;

myPicture.Width = Shapes.Width;

myPicture.Top = Shapes.Top;

myPicture.Left = Shapes.Left;

myPicture.RLeft = Shapes.RelativeHorizontalPosition;

myPicture.RTop = Shapes.RelativeVerticalPosition;





Word.Shape OShape = wordFile.ActiveDocument.Shapes.AddPicture(imageFilename, ref linktofile, ref savewithdocument, ref missing, ref missing, ref missing, ref missing, ref missing);

OShape.Top = Convert.ToSingle(400);

OShape.LockAspectRatio = Office.MsoTriState.msoFalse;

OShape.Width = Convert.ToSingle(myPicture.Width);

OShape.Height = Convert.ToSingle(myPicture.Height);

OShape.Left = Convert.ToSingle(myPicture.Left);

OShape.RelativeHorizontalPosition = myPicture.RLeft;

OShape.RelativeVerticalPosition = myPicture.RTop;

ukemike
05-23-2005, 10:41 AM
Oh shoot. i didnt see this post. let me try it.

MOS MASTER
05-23-2005, 10:42 AM
Hi Mike, :D

Doesn't my previous post work for you? :whistle:

ukemike
05-23-2005, 10:45 AM
MOS,

Nope for some reason it doesnt work.
Its all right i dont mind d/l it to the hard drive and then reading it from there.
I definitely appreciate all the help from you guys


Mike

MOS MASTER
05-23-2005, 10:48 AM
MOS,

Nope for some reason it doesnt work.
Its all right i dont mind d/l it to the hard drive and then reading it from there.
I definitely appreciate all the help from you guys


Mike
Did you try my testfile on a 2000 office? (Would be strange is tested on 2000)

I'll look at your code a bitt later have to finish something..

ukemike
05-23-2005, 10:53 AM
Hey,

Yeah i tested on my Office 2000 Virtual machine.


Mike

MOS MASTER
05-23-2005, 11:03 AM
Hey,

Yeah i tested on my Office 2000 Virtual machine.


Mike
I wonder if that has anything to do with it? (Virtual machine)

O well it doesn't matter. :whistle:

MOS MASTER
05-23-2005, 11:23 AM
Hi Mike,

Can you explain to me why its neccesary for you to set reference to Shapes twice..e.g:
Shapes = wordFile.Selection.Range.ShapeRange.Item(ref rangeObject);


By the way you're using Shapes as a object variable I presume? I don't think that's a sensible thing to do because you're overwritting the Shapes collection of the Word Application. (At least I think you are...)

Shapes is part of the object model and therefore not a good name for a variable...But I don't know any C and I might interpreted it wrong.

The problems you are having aren't to clear to me.

But it might be you should use another Wraptype for the shape so it's behind text or in front or whatever you want.

Here's an example:

OShape.WrapFormat.Type = 3

Play with it! :whistle:

ukemike
05-23-2005, 11:37 AM
MOS,
Good catch on that. I just had a extra statement in there.
WrapFormat is exactly what i was looking for. I guess this is it. I finally finished this!


Mike

MOS MASTER
05-23-2005, 11:42 AM
Hi Mike, :yes
Great to see you've made it! You're Welcome! :beerchug:

MOS MASTER
05-23-2005, 11:44 AM
Hi Mike, :D

BTW...What Virual Machine Software are you using?

TonyJollans
05-23-2005, 02:41 PM
Mike,

Your code doesn't seem to show the copy to the hard drive - no big deal, I just couldn't see it in the code (I'm going to teach myself C# one of these days). Just went back and looked again and I see you have a comment about it. Do you copy it on the fly or need it to be there beforehand?

Joost,

I tried your version with the inline shape and it gave the same error as the original on my Word 2000.

MOS MASTER
05-24-2005, 09:19 AM
Hi Tony, :yes

Yeah I would love for some time to teach myself C# as well...(Seams like good fun)

I've tried it again today and it works over here: XP/2000 maybe some other environment issue is causing this. :whistle: