PDA

View Full Version : Select Case- insert image into word



josie
04-04-2012, 05:14 AM
Hi,

I recieved this code kindley from MacroShadow and I have customised the code.

When I run the code it jumps out at the first case and goes straight to the Case Else statement.

I have tested with each Case.

I can't flaw this code at all. Can anyone see what my problem is?

Thank you all again for any help! Jo

Option Explicit
Sub InsertPictures()

Dim strResponse As String

strResponse = InputBox("Which Unit do you belong to?", "Type Your Unit")

If strResponse = "" Then
Exit Sub
End If

Select Case Unit
Case "Accounting"
ActiveDocument.InlineShapes.AddPicture _
FileName:="C:\Users\josie1\Desktop\thirdview\bg_title_acc_sel.png", _
LinkToFile:=False, SaveWithDocument:=True, Range:=Selection.Range
Case "Business"
ActiveDocument.InlineShapes.AddPicture _
FileName:="C:\Users\josie1\Desktop\thirdview\bg_bs_title_sel.png", _
LinkToFile:=False, SaveWithDocument:=True, Range:=Selection.Range
Case "Financial"
ActiveDocument.InlineShapes.AddPicture _
FileName:="C:\Users\josie1\Desktop\thirdview\bg_fp_title_sel.png", _
LinkToFile:=False, SaveWithDocument:=True, Range:=Selection.Range
Case Else
MsgBox ("Your unit is currently not supported" _
& vbCrLf & "Please contact the IT department."), vbCritical, "Unsuported Unit"
End Select

End Sub

Paul_Hossler
04-04-2012, 05:29 AM
Surprised that 'Option Explicit' didn't tell you that Unit was undefined

1. I think you need to Select Case strResponse

2. I usually test on LCase(strResponse) just to be flexible on user input


Option Explicit
Sub InsertPictures()
Dim strResponse As String
strResponse = InputBox("Which Unit do you belong to?", "Type Your Unit")
If strResponse = "" Then Exit Sub
Select Case LCase(strResponse) '<------------ and the Case values are lower case
Case "accounting"
ActiveDocument.InlineShapes.AddPicture _
FileName:="C:\Users\josie1\Desktop\thirdview\bg_title_acc_sel.png", _
LinkToFile:=False, SaveWithDocument:=True, Range:=Selection.Range
Case "business"
ActiveDocument.InlineShapes.AddPicture _
FileName:="C:\Users\josie1\Desktop\thirdview\bg_bs_title_sel.png", _
LinkToFile:=False, SaveWithDocument:=True, Range:=Selection.Range
Case "binancial"
ActiveDocument.InlineShapes.AddPicture _
FileName:="C:\Users\josie1\Desktop\thirdview\bg_fp_title_sel.png", _
LinkToFile:=False, SaveWithDocument:=True, Range:=Selection.Range
Case Else
MsgBox ("Your unit is currently not supported" _
& vbCrLf & "Please contact the IT department."), vbCritical, "Unsuported Unit"
End Select
End Sub


Paul

PS - Don't forget to use the [VBA] tag button; it makes the code much more readable

BoatwrenchV8
04-04-2012, 05:41 AM
Hello,
You will want strResponse after your Case statement, unit is an undefined variable. Also, making strResponse lowercase and changing the case of what you are trying to match up in your Select Case would make the macro run better since "Accounting" is different than "accounting" and so on. Also, if they misspell a word, it will go straight to the case else statement.

Rich



Sub InsertPictures()
Dim strResponse As String
strResponse = InputBox("Which Unit do you belong to?", "Type Your Unit")
'Make strResponse lowercase so user's entry is consistent.
strResponse = LCase(strResponse)
If strResponse = "" Then
Exit Sub
End If
Select Case strResponse
Case "accounting"
ActiveDocument.InlineShapes.AddPicture _
FileName:="C:\Users\josie1\Desktop\thirdview\bg_title_acc_sel.png", _
LinkToFile:=False, SaveWithDocument:=True, Range:=Selection.Range
Case "business"
ActiveDocument.InlineShapes.AddPicture _
FileName:="C:\Users\josie1\Desktop\thirdview\bg_bs_title_sel.png", _
LinkToFile:=False, SaveWithDocument:=True, Range:=Selection.Range
Case "financial"
ActiveDocument.InlineShapes.AddPicture _
FileName:="C:\Users\josie1\Desktop\thirdview\bg_fp_title_sel.png", _
LinkToFile:=False, SaveWithDocument:=True, Range:=Selection.Range
Case Else
MsgBox ("Your unit is currently not supported" _
& vbCrLf & "Please contact the IT department."), vbCritical, "Unsuported Unit"
End Select
End Sub

josie
04-04-2012, 05:47 AM
Hi Paul,

Wow thankyou so much. I entered in option explicit just before I posted and tested after I posted.

Unit was picked up by option explicit and I am just begining with VBA and I just couldn't work out why.

Thank you again so much. I really appreciate your reply! Jo

fumei
04-04-2012, 09:14 AM
This is one of the cases where a userform is appropriate. In fact, it is a main reason why userforms were introduced.

The userform has a frame with three option buttons (captioned Accounting, Business and Financial). Because they are in a frame only one can be selected. As a good pracice there is a default - in this case Accounting.

User selects one of the options and click the commandbutton. The commandbutton inserts the image and closes the userform. Done. The user can not make a spelling mistake because they are not typing anything.

Here is the code for the commandbutton. In the example the user selection returns a messagebox - this would have to be changed to the correct AddPicture code (commented out here).
Option Explicit

Private Sub cmdOK_Click()
Select Case True
Case fraUnit.Controls("optAccounting")
MsgBox "will use the Accounting image"
' ActiveDocument.InlineShapes.AddPicture _
' FileName:="C:\Users\josie1\Desktop\thirdview\bg_title_acc_sel.png", _
' LinkToFile:=False, SaveWithDocument:=True, Range:=Selection.Range
Case fraUnit.Controls("optBusiness")
MsgBox " will use the Business image"
' ActiveDocument.InlineShapes.AddPicture _
' FileName:="C:\Users\josie1\Desktop\thirdview\bg_bs_title_sel.png", _
' LinkToFile:=False, SaveWithDocument:=True, Range:=Selection.Range
Case fraUnit.Controls("optFinancial")
MsgBox "will use the Financial image"
' ActiveDocument.InlineShapes.AddPicture _
' FileName:="C:\Users\josie1\Desktop\thirdview\bg_fp_title_sel.png", _
' LinkToFile:=False, SaveWithDocument:=True, Range:=Selection.Range
End Select
Unload Me
End Sub


Note: I have the userform show on document open. This could obviously be whatever you want.

Note: for newbies, note that ALL controls (including the userform itself) have explicit names. No default names are used. True, giving explicit names can be a little tedious. However, I strongly recommend you get into the habit of NOT keeping the default names of anything. When you move on to more complicated and complex coding structures it can save you a lot of debugging time if you see (for example):

Sub optFinancial_Click()

rather than

Sub OptionButton6_Click()

OptionButton6 tells you NOTHING.