Display Images from a File into Pictureboxes - image

I have a folder called
App.Path & "\Images"
Inside of it I have 5 Images and in my Form I have also 5 Pictureboxes. Now my question is, How can I display them all one PictureBox per Image at a time? heres is my code so far
Picture1.Picture = LoadPicture("Path")
Heres what I've done so far
Dim c As Control
Dim ImageLink As String
With vs1
For Each c In Form1
For i = 1 To .Rows - 1
If Len(ImageLink) > 0 Then ImageLink = ImageLink
Debug.Print c.Picture
Debug.Print .TextMatrix(i, .ColIndex("Image"))
MsgBox .TextMatrix(i, .ColIndex("Image"))
c.Picture = LoadPicture("C:\Users\paul\Desktop\Gondola Monitoring System\Image\" & .TextMatrix(i, .ColIndex("Image")))
Next
Next
End With
Form1.Show
I try to write the filename in flexgrid and call it in each control.
TYSM for help

Picture1.Picture = LoadPicture(App.Path & "\Images\file1.jpg")
Picture2.Picture = LoadPicture(App.Path & "\Images\file2.jpg")
Picture3.Picture = LoadPicture(App.Path & "\Images\file3.jpg")
Picture4.Picture = LoadPicture(App.Path & "\Images\file4.jpg")
Picture5.Picture = LoadPicture(App.Path & "\Images\file5.jpg")
If this doesn't provide you with what you need, please provide more details on your requirements.
EDIT
Your code isn't going to work. You are looping over all the controls, which will include any buttons, labels, etc, in addition to your picture controls, and for each control you are looping through all the rows of your grid. One better approach would be to define the picture controls as an array (starting index from 1 to match your grid for loop), then the for index is used so that the grid rows and picture indexes match. Something like this:
With vs1
For i = 1 To .Rows - 1
Picture1(i).Picture = LoadPicture("C:\Users\paul\Desktop\Gondola Monitoring System\Image\" & .TextMatrix(i, .ColIndex("Image")))
Next
End With

Related

2 different color text in one cell

We are putting together a new standard signature using vbs for Outlook.
Everything looks great but design would like the phone numbers to look like the attached image. The "O" for office # in Orange and then the number in blue, the "C" for cell # in Orange and then the number in blue.
I can get the entire cell to be one color, but I don't see how to do 2 colors.
The signature is in a table with the logo in one cell that has 5 rows merged and then the other side has 5 rows.
Here is some of my code:
strName = objUser.FullName
strTitle = objUser.Title
strPhone = objUser.telephoneNumber
strMobile = objUser.mobile
strOffice = "O "
strCell = "C "
objTable.Cell(3,2).Range.Font.Name = "Lato"
objTable.Cell(3,2).Range.Font.Size = "12"
objTable.Cell(3,2).Range.Text = strOffice & strPhone & " " & strCell & strMobile
Start recording a macro do it manually by editing the in the cell or the formula bar. Stop the macro and step into it to get all the colors. I stuck to the main colors on the bottom of the pallet. You'll may have to track ThemeColor, TintAndShade and ThemeFont depending on the colors you choose.
This should get you started
Public Sub AddLogo(r As Range)
Dim i As Integer
Dim ColorArray
ColorArray = Array(-16777024, -16776961, -16727809, -16711681, -11480942, -11489280, -1003520, -4165632, -10477568, -6279056)
r = "Excel Magic"
For i = 0 To UBound(ColorArray)
With r.Characters(Start:=(i + 1), Length:=1).Font
.Color = ColorArray(i)
End With
Next
End Sub
Usage:
AddLogo objTable.Cell(3,2)

VBA code to call a picture as a result of an IF Formula

I have a form on Sheet 1 with multiple cells, (B7:L7, B11:L11, and B13:L13) and 3-4 different pictures on a separate sheet (Sheet 2). I'm trying to have a different picture called for each of those cells depending on their value (i.e., if the cells =2, they should show one of the pictures from the other sheet, if the cells =3, they should show a different picture, etc.).
I've tried this 100 different ways and keep striking out so any input would be greatly appreciated!!
Edit:Including Code-
Like I said, I've done this about a 100 different ways. This is my most recent attempt. While probably way more clunky than necessary, it works great for one cell, I'm just not sure how to edit it to make it work for the entire range (other than copy and pasting for each cell individually). I'd also like to add in something to center it in the cell, but haven't had luck with that either.
Sub InsertPicture()
Dim PicCell As Range
Set PicCell = Range("B7")
If PicCell = 2 Then
Worksheets("Sheet2").Activate
ActiveSheet.Shapes.Range(Array("Picture2")).Select
Selection.Copy
Worksheets("Sheet1").Activate
Range("B7").Select
Sheets("Sheet1").Pictures.Paste
ElseIf PicCell = 3 Then
Worksheets("Sheet2").Activate
ActiveSheet.Shapes.Range(Array("Picture3")).Select
Selection.Copy
Worksheets("Sheet1").Activate
Range("B7").Select
Sheets("Sheet1").Pictures.Paste
ElseIf PicCell = 4 Then
Worksheets("Sheet2").Activate
ActiveSheet.Shapes.Range(Array("Picture4")).Select
Selection.Copy
Worksheets("Sheet1").Activate
Range("B7").Select
Sheets("Sheet1").Pictures.Paste
ElseIf PicCell = 5 Then
Worksheets("Sheet2").Activate
ActiveSheet.Shapes.Range(Array("Picture5")).Select
Selection.Copy
Worksheets("Sheet1").Activate
Range("B7").Select
Sheets("Sheet1").Pictures.Paste
ElseIf PicCell = 6 Then
Worksheets("Sheet2").Activate
ActiveSheet.Shapes.Range(Array("Picture6")).Select
Selection.Copy
Worksheets("Sheet1").Activate
Range("B7").Select
Sheets("Sheet1").Pictures.Paste
Else: MsgBox ("No picture at this time")
End If
Let's start with this and see if we're getting close to what you're looking for.
My Sheet1 looks like this:
Sheet2 just has the images like this:
Code: (simplified and improved but no error checking)
Sub InsertPicture()
Dim PicSht As Worksheet
Set PicSht = Worksheets("Sheet2")
Dim mySheet As Worksheet
Set mySheet = Worksheets("Sheet1")
Dim cell As Range
For Each cell In mySheet.Range("B7:L7")
Select Case cell
Case 2 To 6
PicSht.Shapes("Picture" & cell.Value).Copy
cell.Select
mySheet.Pictures.Paste
Case Else
MsgBox ("No picture at this time")
End Select
Next cell
End Sub
Results on Sheet1:

Export Pictures Excel VBA

I'm having trouble trying to select and export all pictures from a workbook. I only want the pictures. I need to select and save all of them as:"Photo 1", "Photo 2", "photo 3", and so on, in the same folder of the workbook.
I have already tried this code:
Sub ExportPictures()
Dim n As Long, shCount As Long
shCount = ActiveSheet.Shapes.Count
If Not shCount > 1 Then Exit Sub
For n = 1 To shCount - 1
With ActiveSheet.Shapes(n)
If InStr(.Name, "Picture") > 0 Then
Call ActiveSheet.Shapes(n).CopyPicture(xlScreen, xlPicture)
Call SavePicture(ActiveSheet.Shapes(n), "C:\Users\DYNASTEST-01\Desktop\TEST.jpg")
End If
End With
Next
End Sub
This code is based on what I found here. It has been heavily modified and somewhat streamlined. This code will save all the pictures in a Workbook from all Worksheets to the same folder as the Workbook, in JPG format.
It uses the Export() Method of the Chart object to accomplish this.
Sub ExportAllPictures()
Dim MyChart As Chart
Dim n As Long, shCount As Long
Dim Sht As Worksheet
Dim pictureNumber As Integer
Application.ScreenUpdating = False
pictureNumber = 1
For Each Sht In ActiveWorkbook.Sheets
shCount = Sht.Shapes.Count
If Not shCount > 0 Then Exit Sub
For n = 1 To shCount
If InStr(Sht.Shapes(n).Name, "Picture") > 0 Then
'create chart as a canvas for saving this picture
Set MyChart = Charts.Add
MyChart.Name = "TemporaryPictureChart"
'move chart to the sheet where the picture is
Set MyChart = MyChart.Location(Where:=xlLocationAsObject, Name:=Sht.Name)
'resize chart to picture size
MyChart.ChartArea.Width = Sht.Shapes(n).Width
MyChart.ChartArea.Height = Sht.Shapes(n).Height
MyChart.Parent.Border.LineStyle = 0 'remove shape container border
'copy picture
Sht.Shapes(n).Copy
'paste picture into chart
MyChart.ChartArea.Select
MyChart.Paste
'save chart as jpg
MyChart.Export Filename:=Sht.Parent.Path & "\Picture-" & pictureNumber & ".jpg", FilterName:="jpg"
pictureNumber = pictureNumber + 1
'delete chart
Sht.Cells(1, 1).Activate
Sht.ChartObjects(Sht.ChartObjects.Count).Delete
End If
Next
Next Sht
Application.ScreenUpdating = True
End Sub
One easy approach if your excel file is an Open XML format:
add a ZIP extension to your filename
explore the resulting ZIP package, and look for the \xl\media subfolder
all your embedded pictures should be located there as independent image files
Ross's method works well but using the add method with Chart forces to leave the currently activated worksheet... which you may not want to do.
In order to avoid that you could use ChartObject
Public Sub AddChartObjects()
Dim chtObj As ChartObject
With ThisWorkbook.Worksheets("A")
.Activate
Set chtObj = .ChartObjects.Add(100, 30, 400, 250)
chtObj.Name = "TemporaryPictureChart"
'resize chart to picture size
chtObj.Width = .Shapes("TestPicture").Width
chtObj.Height = .Shapes("TestPicture").Height
ActiveSheet.Shapes.Range(Array("TestPicture")).Select
Selection.Copy
ActiveSheet.ChartObjects("TemporaryPictureChart").Activate
ActiveChart.Paste
ActiveChart.Export Filename:="C:\TestPicture.jpg", FilterName:="jpg"
chtObj.Delete
End With
End Sub

Is it possible to display multiple images on 1 picturebox in running mode

When I try to google it for 3 days, I found that there is only 1 picture/image available in 1 picbox. My goal is to display multiple images, and they cannot overlap. If they overlap, there a red colour should be shown.
I'm using VB6. I'm using 1 combobox1, for select image n 1 commandbutoon. but when I select 2nd image in click button, the image on picbox will auto overwrite it. Is it caused by .cls ??
Private Sub Combo1_Click()
Dim pin As String
Dim intx As Integer
If UCase$(Combo1.List(intx)) = UCase$(pin) Then
Combo1.ListIndex = intx
End If
End Sub
Private Sub Command1_Click()
If Combo1.ListIndex = 0 Then
Set mPic = pin8.Image
ElseIf Combo1.ListIndex = 1 Then
Set mPic = pin12.Image
Else
Set mPic = pin16.Image
End If
mPicWidth = Me.ScaleX(mPic.Width, vbHimetric, Picture1.ScaleMode)
mPicHeight = Me.ScaleY(mPic.Height, vbHimetric, Picture1.ScaleMode)
ShowPictureAtPosition mLeft, mTop
End Sub
Thank you.
Best regard,
chan
Look, The only way to do that is to add 2 images as resources in the project then make one is the default picture or leave it blank as you wish.
The process now is when you click on the command button you switch between the two pictures.
Button Code:
if
PictureBox1.Image = My.Resources.<Name_of_res_file>.<Name_of_image1111_resource>
Then
PictureBox1.Image = My.Resources.<Name_of_res_file>.<Name_of_image2222_resource>
Else
PictureBox1.Image = My.Resources.<Name_of_res_file>.<Name_of_image1111_resource>
End If
This will switch between the 2 pictures. Hope this helps you.

Taking information from ListBox and putting it into a label

Basically I've got a ListBox in this Visual Basic (VB6) application I'm writing. The ListBox contains "Movies Watched."
I want to be able to grab all the items in this ListBox, and put those items into a label. But I can't quite figure out how to get the information from my ListBox to my label.
Any help would be greatly appreciated, thank you!!
Dim ListText As String
Dim Counter As Integer
ListText = ""
For Counter = 0 To List1.ListCount - 1
If List1.Selected(Counter) = True Then
ListText = ListText & List1.List(Counter) & vbCrLf
End If
Next
MsgBox ListText
This will take everything from the ListBox and add it to a Label as you asked in your question. If you want to just put the Selected Items into your label use SpectralGhost's answer.
Dim x As Integer
List1.AddItem ("Hello")
List1.AddItem ("World")
For x = 0 To List1.ListCount - 1
Label1.Caption = Label1.Caption + List1.List(x) + vbCrLf
Next
I'm not familiar with VB, so I can't give you any code.
Just run throught all of the items and do myLabel.Content = myLabel.Content + myListBox.Items[i].Text.

Resources