PDA

View Full Version : how to insert the picture in vba



batosai17
05-14-2007, 01:52 AM
Private Sub myCleverRemarks_Click()
Dim detail
Dim companyName
Dim companyNameRange As Range

Select Case Me.ComboBox1.Value
Case "test1"
companyName = "test1 :"
detail = _
companyName + vbCrLf + _
" Chefarzt: hmm" + vbCrLf + _
" Sekretariat: yupz " + vbCrLf + _
" Telefon 0 111" + vbCrLf + _
" Telefax 0 22 "
Case "test2"
companyName = "test2:"
detail = _
companyName + vbCrLf + _
" Chefarzt: yupz" + vbCrLf + _
" Sekretariat: test " + vbCrLf + _
" Telefon 0 11 " + vbCrLf + _
" Telefax 0 22 " + vbCrLf + _
" eMail yahoo "
Case Else
companyName = "Unknown"
End Select

Selection.Text = detail

Set companyNameRange = Selection.Range
companyNameRange.Find.Execute FindText:=companyName, Forward:=True
If companyNameRange.Find.Found = True Then
companyNameRange.Bold = True
companyNameRange.Font.Size = 10
companyNameRange.Font.Name = "Arial"
End If

Unload Me
End Sub

anyone know, if i want to insert the picture in vba?
and how to make the font of company name is 10 and the other to 8? anyone can help me? thanx..

shasur
05-14-2007, 07:34 AM
You can use the following to insert shape


Sub InsertShapes()

ActiveDocument.Shapes.AddPicture "c:\Documents and Settings\Temp\SampleFile.jpg"
End Sub



The best option for the font would be


Selection.Font.Size = 10
Selection.Text companyName

' do not include the company in detail
detail = _
vbCrLf + _
" Chefarzt: yupz" + vbCrLf + _
" Sekretariat: test " + vbCrLf + _
" Telefon 0 11 " + vbCrLf + _
" Telefax 0 22 " + vbCrLf + _
" eMail yahoo "

Selection.Font.Size = 8
Selection.Text detail

batosai17
05-22-2007, 03:18 AM
i already try, but the picture always in the top of my value..

like this :

( the picture always in here )
test1
Chefarzt: hmm
Sekretariat: yupz Telefon 0 111
Telefax 0 22
( i want the picture in this place )

my code is :

Private Sub myCleverRemarks_Click()
Dim detail
Dim companyName
Dim companyNameRange As Range
Dim picturePath

Select Case Me.ComboBox1.Value
Case "test1"
companyName = "test1 :"
detail = _
companyName + vbCrLf + _
" Chefarzt: hmm" + vbCrLf + _
" Sekretariat: yupz " + vbCrLf + _
" Telefon 0 111" + vbCrLf + _
" Telefax 0 22 "
picturePath = "C:\Documents and Settings\Administrator\Desktop\test1\New Folder\gefaess1.jpg"

Case "test2"
companyName = "test2:"
detail = _
companyName + vbCrLf + _
" Chefarzt: yupz" + vbCrLf + _
" Sekretariat: test " + vbCrLf + _
" Telefon 0 11 " + vbCrLf + _
" Telefax 0 22 " + vbCrLf + _
" eMail yahoo "

Case Else
companyName = "Unknown"
End Select

If Not IsNull(picturePath) And picturePath <> "" Then
Selection.InlineShapes.AddPicture (picturePath)
End If

Selection.Text = vbCrLf + detail

Set companyNameRange = Selection.Range
companyNameRange.Find.Execute FindText:=companyName, Forward:=True
If companyNameRange.Find.Found = True Then
companyNameRange.Bold = True
companyNameRange.Font.Size = 10
companyNameRange.Font.Name = "Arial"
End If

Unload Me
End Sub

anyone know? thanx..

fumei
05-22-2007, 11:21 AM
batosai17 - please use the VBA code tags to put your code in a code window.

Thanks.

but the picture always in the top of my value..Because that is where you put it. You put in the picture THEN the text. So of course the picture will come first.

fumei
05-22-2007, 11:26 AM
Also, you should probaly declare your variables better.
Dim detail
Dim companyName
Dim picturePathare all being declared (by default) as Variant.

fumei
05-22-2007, 12:11 PM
With some difference - because I did not want to bother making a userform...

So, your Me.ComboBox1.Value = myInput (from an InputBox).

Your details of the address etc. I put into an array. BTW: you did not respond to shasur's suggestion NOT to put the company name into details.

I also used more With...End With; and used "r" as the range object - I see no point in using a long range name.

The code makes the WHOLE detail text 8 pts Times Roman, THEN makes the company name (if found) Arial 10 pts.

You need to understand how to use logical order of things.Sub DoDah() ' myCleverRemarks_Click()
Dim strDetail As String
Dim strCompanyName As String
Dim r As Range
Dim strPicturePath As String
Dim myInput As String
Dim DetailsArray()

DetailsArray = Array(vbCrLf + _
" Chefarzt: hmm" + vbCrLf + _
" Sekretariat: yupz " + vbCrLf + _
" Telefon 0 111" + vbCrLf + _
" Telefax 0 22 ", _
vbCrLf + _
" Chefarzt: yupz" + vbCrLf + _
" Sekretariat: test " + vbCrLf + _
" Telefon 0 11 " + vbCrLf + _
" Telefax 0 22 " + vbCrLf + _
" eMail yahoo ")

myInput = InputBox("whatever")
Select Case myInput
Case "test1"
strCompanyName = "test1:"
strDetail = strCompanyName & vbCrLf & DetailsArray(0)
strPicturePath = "C:\Test\myTest.jpg"
Case "test2"
strCompanyName = "test2:"
strDetail = strCompanyName & vbCrLf & DetailsArray(1)
Case Else
strDetail = "Unknown"
End Select

Selection.Text = vbCrLf + strDetail

Set r = Selection.Range
With r.Font
.Name = "Times Roman"
.Size = 8
End With
With r.Find
.Execute FindText:=strCompanyName, Forward:=True
If .Found = True Then
r.Bold = True
r.Font.Size = 10
r.Font.Name = "Arial"
End If
End With


With Selection
.Collapse direction:=wdCollapseEnd
.TypeParagraph
End With

If Not IsNull(strPicturePath) And strPicturePath <> "" Then
Selection.InlineShapes.AddPicture (strPicturePath)
End If

End Sub