Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA Newbie Question...Easy Answer!

Status
Not open for further replies.

Graedon

IS-IT--Management
Sep 20, 2006
13
US
Before 2 days ago I don't even think I had looked at a visual basic script before. So, please bear with me. I'm a complete novice...but I do have a background in php scripting...so I'm not a total moron when it comes to this sort of thing.

Here's what I'm trying to do:
I want to build a script to insert images, inline, within Word and Outlook. This code is going to be primarily used to give emoticons back to the people in my office since we just switched from Eudora to Outlook and there are none. I've written code that works...but, it's alot of repetitious crap that I know can be simplified a LOT. Here's an example:

Sub EmotLargeAngry()
Selection.InlineShapes.AddPicture FileName:= _
"\\Server\IT Files\Macros\Emoticons\Images\Large\Angry.png", _
LinkToFile:=False, SaveWithDocument:=True
End Sub

I have that chunk of code 24 times for small, medium and large icons. I know I'm bound to be able to do some if statements in conjunction with a function that can squeeze this down to at least 1/3 if not less code. I'm just not familiar enough with VB to figure it out.

I need to be able to make individual buttons for each icon in a toolbar. So, somehow I'll need to be able to tell the script which button I pressed if the script were condensed to one function with if statements.

2 other little issues I'm having with this way of doing things...
1. I can only get this to work when Word 2003 is the email editor for Outlook. Some people in our office have Outlook 2003 and Word 2002, so Word is not an option for email editing in their case. How can I get this to work for Outlook independently?
2. When I insert the images in Outlook, they seem fuzzy as though they're being inserted large than their native size. How do I specify an image size in the script?


Thanks!!!
Matt Tyree
 
I can't say much for your second question, but in an effort to help shorten you code, you could probaly do soemthing like:

Sub EmotImage(strImage As String, strSize As String)
Selection.InlineShapes.AddPicture FileName:= _
"\\Server\IT Files\Macros\Emoticons\Images\" & strSize & "\" & strImage & ".png", _
LinkToFile:=False, SaveWithDocument:=True
End Sub

This applies if you have three folders (Small, Medium, and Large respectively). Then you only need one procedure for ALL your images. Your buttons would look something like:

Call EmotImage("Angry", "Large")

This should shrink you code A LOT!

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
Awesome! You da man!
So, to make buttons like that...what do I do? I know how to assign a macro to a button, but how do I do something custom like that?

Thanks again for that code....much better!

Anyone have any ideas for my subsequent questions?

Thanks!
Matt
 
When you put the macros as icons on a toolbar, you can sue available icon faces, edit available icon faces, or bring in images to use as icon faces.

Gerry
My paintings and sculpture
 
Yeah, but how do I assign:
Call EmotImage("<image name>", "<image size>")
to a button without making a macro out of it first? Or do I still have to make a macro out of it and have that bit of code for each icon, small, medium and large?

Fumei, I'm not sure which question you were attempting to answer.

Thanks!
 
Been a long time since I created anything for Word or Outlook. I don't remember off the top of my head how to assign a piece of code to an object there. In Access though, when you are displaying the properties for the object, there is an OnClick event. You can either select a Macro name there or select [Event Procedure]. This will give you a little ellipse at the end of the propety window. Click this ellipse and you are taken to the code window, where you simply put:

Call EmotImage("Angry", "Large")

between the Private ButtonName_OnClick and the End Sub...

Maybe that will help you find the location to enter the code :)

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
Hmmm...I guess Word and Outlook aren't as coder-friendly as Access is. I can definitely see where Access would use such things a lot more.
I don't see anything about OnClick when I go to assign a command to a button in Word or Excel. Suckin!
 
Well Outlook is definitely not code-friendly.

mstrmage1768 - your post works for objects with accessible events. The subject of this thread does not.

Let's get something clear.
Code:
Sub EmotImage(strImage As String, strSize As String)
    Selection.InlineShapes.AddPicture FileName:= _
        "\\Server\IT Files\Macros\Emoticons\Images\" & strSize & "\" & strImage & ".png", _
        LinkToFile:=False, SaveWithDocument:=True
End Sub
What does this do?

It places an InlineShape into the document. This does nothing all to any icons you want on a toolbar.
Graedon said:
Yeah, but how do I assign:
Call EmotImage("<image name>", "<image size>")
to a button without making a macro out of it first?
Simply put...you can't. That instruction IS a macro. You put the macro onto the toolbar. The default display is text (normally).

You can then make adjustments to that:

Make it only an icon, and, as stated, change that icon.

Also, remember that you have to pass the parameters into the Sub mstrmage1768 posted. So if your buttons are going to fire that sub - which would be fine - you will need an inputbox (or something) to GET those parameters.

Gerry
My paintings and sculpture
 
Yeah, I gather all that.

Actually, mstrmage1768's script does exactly what I want. I think you misunderstood what I wanted to do. His script puts my repeated script in one place where I can then have macros for each emoticon pass their variables to it for processing.
Adding images to the icons inside Word or Outlook is something I can do after I get all this mess straight.

To put it simply...

I'll have a button that calls sub ('A') which then gives the variable info to the main sub ('B') which parses the variables and spits out the icon on the page.

And, from what you've said there's no way to get the button itself to send those variables. So, I still have to have a macro for each button.

In the end I haven't really saved myself very much time or code. I still have 72 macros (well, actually, now I've added one for the main sub). I guess the only way this could potentially help is if I ever needed to change something in the main sub. I would only have to change it in one place.

Hmmm, I guess I expected too much out of VBA! :-D

If anyone has an answer for those last two issues, please let me know!

Thanks for the help!!!
Matt
 
No, I actually understood quite well. His script allows you to use variables (image files, and image size) to action as added InlineShapes. And it works doing precisely that. You do not need individual procedures for each image file and size.

You DO need another procedure that will pass the parameters though, as I stated.
And, from what you've said there's no way to get the button itself to send those variables. So, I still have to have a macro for each button.
A button is just a button. It is NOT a commandbutton, with commandbutton like events. The button/icon points to a macro (a procedure).

For example, the Print icon on a standard toolbar has no code itself. It does one thing, and one thing only. It points to a procedure name (Sub FilePrint() - a built-in Word procedure) and fires it. So, taking the same example, if you write a procedure like this in a code module:
Code:
Sub FilePrint()
   Msgbox "Nah, I don't feel like printing now...."
End Sub
Clicking on the Print icon on the toolbar will NOT print...it will display that message. The buttons point to a macro (procedure) name. They do NOT have code, or events.

No, I don't agree that you have not saved any time or code. mstrmage1768's suggestion permits you to use ONE procedure to add any InlineShape, given two valid parameters.

Have three buttons. One for Small, one for Medium, one for Large. OK, there is one parameter taken care of.

Make a userform listing your images. This could be sorted by Angry, Happy...THAT logic you would have to do.

EACH of the three Size buttons displays the SAME userform.

User selects the image, presses OK.
Userforms closes (unloads).
The image file name gets passed back to the Size button procedure, which then passes both the size, and the image name, to the main sub EmotImage.

Gerry
My paintings and sculpture
 
Kind of like this:

ASSUMPTION: you have a userform (say named, frmImageSelector) with the images listed. The path does not have to be displayed to user - they probably don't care. However, the path has to be known to the userform. There are lots of ways of doing that.

You have two global variables: strImage, strSize

Code:
Sub SmallIcon()
   frmImageSelector.Show
....user selects image name from userform
....on Unload writes strImage with that selection
   Call EmotImage(strImage, "Small")
End Sub

Sub MediumIcon()
   frmImageSelector.Show
....user selects image name from userform
....on Unload writes strImage with that selection
   Call EmotImage(strImage, "Medium")
End Sub

Sub LargeIcon()
   frmImageSelector.Show
....user selects image name from userform
....on Unload writes strImage with that selection
   Call EmotImage(strImage, "Large")
End Sub

Of course, this will need some fiddling depending on how you have your image files organized.



Gerry
My paintings and sculpture
 
Okay, now I'm totally confused.

When you first replied to this thread you were talking about the images on the buttons in Outlook...so, I assumed you didn't understand what I was trying to do.

What does this do?

It places an InlineShape into the document. This does nothing all to any icons you want on a toolbar.

That statement is what made me really think you didn't understand because I don't need to know how to put buttons on a toolbar. Placing an inline shape into the document is EXACTLY what I want to do. So, when you tossed it out the window like it wasn't what I was looking for, I was definitely under the impression you misunderstood. My mistake.


Okay, I'm trying to digest some of the things you've said here. And please keep in mind, another day has gone by but I'm still a total newb. So, don't start shooting the big technical terms at me yet. I'm still clueless! :-D

So, I can't assign an action to a button, becasue it's not a commadnbutton...but I can assign a macro to to a button, which is an action...sorta...but, I also don't have to make a macro for each button...And I take it you're saying I need some sort of form for them to choose an image name from.

Here's what I'd LIKE to do, you tell me if this will work at all.

Right now I have a toolbar with 3 menus (Small, Medium and Large) on them that drop down and display the emoticons available. When you click one of them, it runs a corresponding macro that calls the EmotImage() sub and gives it the proper values for strSize and strImage and voila, it puts the image in where your cursor is on the screen. This works, more or less, exactly how I want it to...I just thought I could do it without having a macro for each image assigned to a button.

So, you mention this user form where the user clicks the button for the size they want, a form pops up and they choose the image they want which then sends the value for strImage to the EmotImage() sub.

Does a userform act similar to a dropdown menu? I'm not at all familiar with a user form or how to make one.

I'm sorry for being so dense about this...I'm trying to wrap my head around all this stuff as fast as I can.

I really appreciate the help!

BTW, I see that you're a painter and sculptor...nice looking stuff too! I'm an illustrator too! If you get a chance, check out
Thanks again!
Matt
 
You are confused? Hmmm. Well, I sure am.

OK. You have a toolbar.
It has three menus - Small, Medium, Large.

Are you saying you have it working that if you click menu item, say, Small...you get a....what? showing a display of the emoticons? How are you doing that?
So, I can't assign an action to a button, becasue it's not a commadnbutton...but I can assign a macro to to a button, which is an action...sorta...
No sorta about it. A macro is a procedure, and procedures are essentially actions. They tell (instruct) the computer to DO something.

So, absolutely, you assign an action to the button. The part of this I'm having difficulty is the order. As far as I know the macro comes first. You drag a macro to the toolbar and that puts the button (if you choose to use a button) there. Are you saying you are putting "empty" buttons on the toolbar.

Further, it sounds like these are not going on the toolbar itself, they are items in a dropdown from a menu item.

Is it possible to send me an example of what you are doing? It may help if I could see what you are doing. If so, send it to: myhandle at telus dot net. I am spelling the address out as we are not to use parse-able email addresses here.
When you click one of them, it runs a corresponding macro that calls the EmotImage() sub and gives it the proper values for strSize and strImage and voila, it puts the image in where your cursor is on the screen. This works, more or less, exactly how I want it to...I just thought I could do it without having a macro for each image assigned to a button.
You state "corresponding macro", then wonder about not having a macro assigned to each button. Is that not what corresponding means?

If you have individual buttons off a menu/toolbar, then there has to be individual procedures. So, yes, macros for all of them.

Userforms. Userforms are VBA generated dialogs. In fact, they used to be called dialogs. Some really old-timers have problems with "userform". But in any case, it is a built display form. You can put ANYTHING (just about) on a userform. You build one in the VBE.

Help me out here. I need to know more about your files. Say..."Angry". Are all "angry" image files in one folder? Are all "happy" image files in one folder?

As for what I am suggesting...I am going to try to post an image. I am not sure if I will be able to get it.
sampleform.jpg


If it does display....and I made it a readable size.....

This is a userform. It uses a MultiPage to separate your images, by type. Again EACH of your menu items (Small Medium, Large) will display this SAME userform. EACH of the menu items will set a value for a global variable - strSize. Say user selects Small menu item.

strSize is set to "Small". The userform is displayed.

User selects the Angry tab. Selects an image.

User clicks the Insert button. The code for Insert_Click picks up the image name from which ever image control (each image displayed on the userform is a control) the user selects. It sets the value for strImage, and fires Sub EmotImage with the parameters strSize and strImage.

Gerry
My paintings and sculpture
 
Heh heh...okay...I think we're getting closer to being on the same page. Thanks for hanging in there with me. I know I don't always use the correct terminology and that gets confusing. I'll try to explain as clearly as possible:

First, here's an image of what my menu looks like:

From that you can see I have set up 3 (what I call) drop-down menus. Each of the 3 menus have 24 buttons with corresponding macros to call the EmotImage() sub. Yes, that means a macro for each. So, right now I have 72 macros plus the EmotImage() macro.
I haven't put in all the images for the buttons yet. Just the Happy one so far. But I know how to do that "the hard way".
A typical macro for one of these buttons would look like this:
Code:
Sub EmotSmallAngry()
    Call EmotImage("Angry", "Small")
End Sub

Which passes the image info to this:
Code:
Sub EmotImage(strImage As String, strSize As String)

    ' Insert the correct image
    Set oPicture = _
    Selection.InlineShapes.AddPicture _
    (FileName:="\\Server\IT Files\Macros\Emoticons\Images\" & strSize & "\" & strImage & ".png", _
        LinkToFile:=False, SaveWithDocument:=True)
    
    ' Now set the emoticon's size
    If strSize = "Small" Then
    oPicture.Width = 12
    oPicture.Height = 12
    End If
    
    If strSize = "Medium" Then
    oPicture.Width = 18
    oPicture.Height = 18
    End If
    
    If strSize = "Large" Then
    oPicture.Width = 24
    oPicture.Height = 24
    End If
    
End Sub

So, right now, this works for the most part. Just more code than may be necessary. But, it seems to be the only way to do it where the user end can still look the same. I don't want to get into any pop-up menus or anything. This is as close to the old way (when we used Eudora) that I can get.

Bad thing is, only people with Word 2003 can do it this way. I have yet to find a way to make it work in just Outlook 2003 alone.

Thanks again for your help!
Matt
 
But, it seems to be the only way to do it where the user end can still look the same.
I have no idea at all what you are trying to say there.

Huh????

In any case, shrug, you want to do it with 72 macros...well, that is your choice. I suggested an alternative way.

Outlook will indeed be a bit harder. Coding for Outlook I find a bit more of a pain. I do so little of it.

In any case, you seem to have decided on what route you want to take regarding Word, so hopefully this thread did some help.

Good luck.



Gerry
My paintings and sculpture
 
What I mean is, I want the UI to look like the image I posted. The "user end" should look like that. So, I don't want a menu to pop up like the image you posted.

It's not that I have decided on the way I want to do things, per se. But, if there's no other way to have the UI remain looking the way I have it now, then I don't want to change it. See what I mean?

Now if you or anyone else has a way of doing things that I can keep the same UI AND reduce the amount of macros needed to do it, then I'm ALL for it!

Don't mean to sound like I'm not grateful for all the suggestions, because I am exceedingly grateful!!!

Thanks again!!!
Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top