PDA

View Full Version : insertion of picture with the help of VBA



yogeshwarv
04-19-2006, 02:12 AM
Hello all,
I want to change a text box with an image and so on.
I used this code:

'------------------
Sub Macro4()
'
' Macro4 Macro
' Macro recorded 15-04-2006 by True
'
'
Range("B3").Select
ActiveSheet.Pictures.Insert("C:\My Documents\My Pictures\thinking.gif").Select
Application.ScreenUpdating = False
Selection.ShapeRange.LockAspectRatio = msoFalse
Selection.ShapeRange.Height = 61.5
Selection.ShapeRange.Width = 64.5
Selection.Name = "LG1"
ActiveSheet.Shapes("LG").Select
Selection.Delete
ActiveSheet.Shapes("LG1").Select
Selection.Name = "LG"
Selection.OnAction = "NADA"
Application.ScreenUpdating = False
Range("E3").Select
End Sub
'---------

But I wish an insert picture dialog should to be opened on click and the selected image needs to be replaced with the current shape and place.
I tried to use :
Application.Dialogs(xlDialogInsertPicture).Show
but i am confused how to associate this command with selected picture.
I am new to VBA and seeking your guidance to help me.
Thanks
Yogeshwar

Sir Newbie
04-19-2006, 03:07 AM
Hi yogeshwarv,

As you can probably tell from my user name, I'm no expert.

(Although I currently am the number 1 Space Invaders Champion :))

Having said that... It's nice to know that someone is watching out for you.

Question one...

Do you want a dialog box to appear when the user clicks on a particular cell, so that they can select the picture they want?

I'm not too sure what your objective is.

Maybe you could type the words "insert" and "picture" into your Excel's VBA Answer Wizard and (when the results appear) look at "Insert File Dialog Box".

I'm not too sure to tell you the truth. Don't worry though, the gurus should be along any moment now :)

Chow Bambino!

Sir Newbie
04-19-2006, 03:59 AM
He's baaaack :)

Okay yogeshwarv, so the above advice probably wasn't the best I've ever given to somebody in my life.

Needless to say, I've been thinking about your problem (even though I've got quite a few of my own - Excel ones that is :))

Here's what I came up with...

If you get totally stuck (and I mean as in like quick sand) maybe you could create a command button and then paste the following code into it.


Private Sub CommandButton1_Click()
'Open the dialog box.
fileToOpen = Application _
.GetOpenFilename("All Files (*.*), *.*")
If fileToOpen <> False Then
'Confirm which file will open.
MsgBox "Open " & fileToOpen
End If
End Sub

And (wait for it) if you want a picture...

Just right click on the button, open the "Properties" window and insert a picture from there!

Does that work for you?

Is that what you want?

Probably not.

Wait!

I think I can here someone with some real VBA skills coming nearer!

Gotta go :)

lucas
04-19-2006, 07:05 AM
Malcolm has a script he has been working on that sounds very simular. You might take a look at how he's doing this.

Its Here (http://vbaexpress.com/forum/showpost.php?p=57914&postcount=17)

yogeshwarv
04-21-2006, 11:23 PM
What I wanted is like this.
When someone click on Insert picture box.
A picture insert dialog should open and the selected picture should be replaced with the frame "LG" at the same location and size of frame "LG"
I have succeeded a little bit in doing this but i do not know how to determine the exact location of the frame "LG".
Please go thru with the attachment for detailed explanation
Can you help me?.. Pleeeezzz

lucas
04-22-2006, 08:11 AM
I think this will work for your purpose as a quick solution. Will look at it closer when I can. Maybe someone else has an idea:

Sub Macro4()
Dim insertpic
Application.ScreenUpdating = False
Range("B3").Select
ActiveSheet.Shapes("LG").Select
Selection.Delete
insertpic = Application.Dialogs(xlDialogInsertPicture).Show
If insertpic <> True Then Exit Sub
Selection.Name = "LG1"

Selection.ShapeRange.LockAspectRatio = msoFalse
Selection.ShapeRange.Height = 61.5
Selection.ShapeRange.Width = 64.5
' ActiveSheet.Shapes("LG").Select
' Selection.Delete
ActiveSheet.Shapes("LG1").Select
Selection.Name = "LG"
Selection.OnAction = "NADA"
Range("E3").Select
Application.ScreenUpdating = True
End Sub

yogeshwarv
04-22-2006, 08:49 AM
thanks for your quick reply sir,
ya it works but it does not determine the exact location of the frame if "LG" moves to some other location like in the middle of the activesheet. also it verify whether the frame "LG" exist or not if not then do not execute the code.

I do not want to select cell "B3". I wish whatever cell is active no matter. the macro should change frame "LG" into the picture frame named "LG" at the same location & size whereever it is located in the activesheet.

I will be very grateful to you solve my problem, please...............

Thanks & Regards

Yogeshwar

lucas
04-22-2006, 10:41 AM
I don't understand why your using the frame LG but there must be a reason. If not.....

Option Explicit
Sub Macro4()
Dim insertpic
Application.ScreenUpdating = False

insertpic = Application.Dialogs(xlDialogInsertPicture).Show
'If insertpic <> True Then Exit Sub
' Selection.Name = "LG"
Selection.ShapeRange.LockAspectRatio = msoTrue
Selection.ShapeRange.Height = 61.5
Selection.ShapeRange.Width = 64.5
Range("E3").Select
Application.ScreenUpdating = True
End Sub

yogeshwarv
04-24-2006, 08:08 AM
See
I an new in VBA. but first when I came to know about VBAexpress.com
I started visiting this portal regularily and slowly slowly my interest developed in VBA Excel macro
This is my favorite application though i do not know much about VBA
I want to make a template where anyone can change his picture in the specified frame only,
so that the user can guess where his inserted image will be placed.
This is the reason why i want to use frame "LG". i do not find another option except this.

I have done almost everything. The only thing I am not getting the clue to work with the specified frame.

see... if the frame moves in the left corner or whereever in the current
sheet as i explained in the attachment.
Then The code will only remove the specified frame from its location but insert the requested picture in cell "B3" only.
but i wish the code should place the requested image at the same location where Actual frame "LG" was placed.

I am just stucked.............:banghead: