kguerrero
05-16-2012, 04:15 PM
Hi,
I have been struggling for a couple of days to figure out how to add multiple images to a single slide with a VBA Macro. Other key points is that this task will repeat over multiple slides.
I have successfully created a VBA Macro that will allow me add slides and 2 images (.PNG and .JPG) to multiple slides. This Macro pulls images from a folder specified by the user and creates slides based off of information from a .csv This macro works nicely and is very reliable.
However, now I would like to add and resize multiple photos to a single slide based on a field in the csv. In essence the first line of the csv may indicate that there are 2 photos for a specific slide and the the second line of the csv may indicate 4 photos for the next slide. The images have specific naming conventions that make this part a non issue.
Here is the code that I have written - there are probably easier ways to do much of what I did, so I welcome additional comments. But my main goal is to figure out how to get the "loop" to work.
I've included the entire code: but I think the issue is my integration of the For - Next loop, or the Calls with in the loop.
There are two lines specifically that give me an issue. One is commented out. Both result in different errors.
This one results in an object reference issue:
osld.Shapes.AddPicture strPath + file.Name, False, True, imgLeftPos(i), imgTopPos(i), imgHorSize, imgVertSize
This one results in a "With" issue
Set oPic0 = osld.Shapes.AddPicture(FileName:=strPath & strFileJPG, LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, Left:=imgLeftPos(0), Top:=imgTopPos(0), Width:=imgHorSize, Height:=imgVertSize)
Sub addSlides()
Dim Input_file As Variant
Dim dlgOpen As FileDialog
Dim InfoFile As String
Dim FileNum As Integer
Dim Buffer As String
Dim osld As Slide
Dim oshp As Shape
Dim x As Integer
Dim y As Integer
Dim xPos As Integer
Dim yPos As Integer
Dim strInput As String
Dim JPG_found As Boolean
Dim oText1 As Shape
Dim oText2 As Shape
Dim oText3 As Shape
Dim columCount As Integer
Dim PedNum As String
Dim WID As String
Dim Use As Integer
Dim PedType As String
Dim Results As String
Dim Lamina As String
Dim oPic0 As Shape
Dim oPic1 As Shape
Dim oPic2 As Shape
Dim strPath As String
Dim strFilePNG As String
Dim strFileJPG As String
Dim checkforFile As String
Dim TotalSlids As Integer
Dim imgLeftPos(10) As Integer
Dim imgTopPos(10) As Integer
Dim imgHorSize As Integer
Dim imgVertSize As Integer
Dim i As Integer, thisUse As String
MsgBox ("The first thing you'll" & Chr(13) & "need to do is navigate to" & Chr(13) & "the csv with WID, Ped info, etc." & Chr(13) & "The next window is an 'open'" & Chr(13) & "window where you navigate and" & Chr(13) & "select the .csv")
Set dlgOpen = Application.FileDialog(Type:=msoFileDialogOpen)
dlgOpen.AllowMultiSelect = False
If dlgOpen.Show = -1 Then
Input_file = dlgOpen.SelectedItems.Item(1)
End If
EnterPath.Show
strPath = EnterPath.EnterImagePathTextBox
MsgBox ("Image Path = " + strPath)
FileNum = FreeFile()
Open Input_file For Input As FreeFile
columCount = 0
TotalSlides = 0
While Not EOF(FileNum)
columCount = columCount + 1
Input #FileNum, Buffer
If columCount = 1 Then
WID = Buffer
End If
If columCount = 2 Then
Use = Buffer
If Use = 1 Then
imgHorSize = 340
imgVertSize = 340
imgLeftPos(0) = 18
imgTopPos(0) = 60
imgLeftPos(1) = 360
imgTopPos(1) = 60
End If
If Use = 2 Then
imgHorSize = 234
imgVertSize = 234
imgLeftPos(0) = 6
imgTopPos(0) = 102
imgLeftPos(1) = 240
imgTopPos(1) = 102
imgLeftPos(2) = 474
imgTopPos(2) = 102
End If
End If
If columCount = 3 Then
PedNum = Buffer
End If
If columCount = 4 Then
PedType = Buffer
End If
If columCount = 5 Then
Results = Buffer
End If
If columCount = 6 Then
Lamina = Buffer
Set osld = ActivePresentation.Slides.Add(ActivePresentation.Slides.Count + 1, ppLayoutBlank)
Set oText1 = osld.Shapes.AddTextbox(msoTextOrientationHorizontal, Left:=260, Top:=10, Width:=648, Height:=42)
With oText1.TextFrame
.WordWrap = msoFalse
.AutoSize = ppAutoSizeShapeToFitText
.HorizontalAnchor = msoAnchorCenter
With .TextRange
.Text = WID + ", " + Use
With .Font
.Name = "Calibri"
.Size = 40
.Bold = msoFalse
.Color.RGB = RGB(0, 0, 0)
End With
End With
End With
Set oText2 = osld.Shapes.AddTextbox(msoTextOrientationHorizontal, Left:=30, Top:=360, Width:=276, Height:=51)
With oText2.TextFrame
.WordWrap = msoTrue
.AutoSize = ppAutoSizeShapeToFitText
.HorizontalAnchor = msoAnchorNone
With .TextRange
.Text = "Ped " + PedNum + " - " + PedType & Chr(13) & "Results: " + Results & Chr(13) & "Lamina :" + Lamina
With .Font
.Name = "Calibri"
.Size = 12
.Bold = msoFalse
.Color.RGB = RGB(0, 0, 0)
End With
End With
End With
Set oText3 = osld.Shapes.AddTextbox(msoTextOrientationHorizontal, Left:=402, Top:=438, Width:=210, Height:=24)
With oText3.TextFrame
.WordWrap = msoFalse
.AutoSize = ppAutoSizeShapeToFitText
.HorizontalAnchor = msoAnchorNone
With .TextRange
.Text = "IR Image of Process Wafer"
With .Font
.Name = "Calibri"
.Size = 14
.Bold = msoFalse
.Color.RGB = RGB(0, 0, 0)
End With
End With
End With
For i = 0 To Use
thisUse = CStr(i)
strFileJPG = "orig." + WID + "gu" + thisUse + "sPSQ-donorr1200EXFO.jpg"
checkforFile = strPath & strFileJPG
If Dir(checkforFile) <> "" Then
osld.Shapes.AddPicture strPath + file.Name, False, True, imgLeftPos(i), imgTopPos(i), imgHorSize, imgVertSize
' Set oPic0 = osld.Shapes.AddPicture(FileName:=strPath & strFileJPG, LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, Left:=imgLeftPos(0), Top:=imgTopPos(0), Width:=imgHorSize, Height:=imgVertSize)
Else
MsgBox "Donor Image for " + WID + " Use " + thisUse + " was not found."
End If
Next
TotalSlides = TotalSlides + 1
columCount = 0
End If
Wend
MsgBox ("Total Slides Added = " & TotalSlides)
End Sub
Thanks, I look forward to any help that can be offered
I have been struggling for a couple of days to figure out how to add multiple images to a single slide with a VBA Macro. Other key points is that this task will repeat over multiple slides.
I have successfully created a VBA Macro that will allow me add slides and 2 images (.PNG and .JPG) to multiple slides. This Macro pulls images from a folder specified by the user and creates slides based off of information from a .csv This macro works nicely and is very reliable.
However, now I would like to add and resize multiple photos to a single slide based on a field in the csv. In essence the first line of the csv may indicate that there are 2 photos for a specific slide and the the second line of the csv may indicate 4 photos for the next slide. The images have specific naming conventions that make this part a non issue.
Here is the code that I have written - there are probably easier ways to do much of what I did, so I welcome additional comments. But my main goal is to figure out how to get the "loop" to work.
I've included the entire code: but I think the issue is my integration of the For - Next loop, or the Calls with in the loop.
There are two lines specifically that give me an issue. One is commented out. Both result in different errors.
This one results in an object reference issue:
osld.Shapes.AddPicture strPath + file.Name, False, True, imgLeftPos(i), imgTopPos(i), imgHorSize, imgVertSize
This one results in a "With" issue
Set oPic0 = osld.Shapes.AddPicture(FileName:=strPath & strFileJPG, LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, Left:=imgLeftPos(0), Top:=imgTopPos(0), Width:=imgHorSize, Height:=imgVertSize)
Sub addSlides()
Dim Input_file As Variant
Dim dlgOpen As FileDialog
Dim InfoFile As String
Dim FileNum As Integer
Dim Buffer As String
Dim osld As Slide
Dim oshp As Shape
Dim x As Integer
Dim y As Integer
Dim xPos As Integer
Dim yPos As Integer
Dim strInput As String
Dim JPG_found As Boolean
Dim oText1 As Shape
Dim oText2 As Shape
Dim oText3 As Shape
Dim columCount As Integer
Dim PedNum As String
Dim WID As String
Dim Use As Integer
Dim PedType As String
Dim Results As String
Dim Lamina As String
Dim oPic0 As Shape
Dim oPic1 As Shape
Dim oPic2 As Shape
Dim strPath As String
Dim strFilePNG As String
Dim strFileJPG As String
Dim checkforFile As String
Dim TotalSlids As Integer
Dim imgLeftPos(10) As Integer
Dim imgTopPos(10) As Integer
Dim imgHorSize As Integer
Dim imgVertSize As Integer
Dim i As Integer, thisUse As String
MsgBox ("The first thing you'll" & Chr(13) & "need to do is navigate to" & Chr(13) & "the csv with WID, Ped info, etc." & Chr(13) & "The next window is an 'open'" & Chr(13) & "window where you navigate and" & Chr(13) & "select the .csv")
Set dlgOpen = Application.FileDialog(Type:=msoFileDialogOpen)
dlgOpen.AllowMultiSelect = False
If dlgOpen.Show = -1 Then
Input_file = dlgOpen.SelectedItems.Item(1)
End If
EnterPath.Show
strPath = EnterPath.EnterImagePathTextBox
MsgBox ("Image Path = " + strPath)
FileNum = FreeFile()
Open Input_file For Input As FreeFile
columCount = 0
TotalSlides = 0
While Not EOF(FileNum)
columCount = columCount + 1
Input #FileNum, Buffer
If columCount = 1 Then
WID = Buffer
End If
If columCount = 2 Then
Use = Buffer
If Use = 1 Then
imgHorSize = 340
imgVertSize = 340
imgLeftPos(0) = 18
imgTopPos(0) = 60
imgLeftPos(1) = 360
imgTopPos(1) = 60
End If
If Use = 2 Then
imgHorSize = 234
imgVertSize = 234
imgLeftPos(0) = 6
imgTopPos(0) = 102
imgLeftPos(1) = 240
imgTopPos(1) = 102
imgLeftPos(2) = 474
imgTopPos(2) = 102
End If
End If
If columCount = 3 Then
PedNum = Buffer
End If
If columCount = 4 Then
PedType = Buffer
End If
If columCount = 5 Then
Results = Buffer
End If
If columCount = 6 Then
Lamina = Buffer
Set osld = ActivePresentation.Slides.Add(ActivePresentation.Slides.Count + 1, ppLayoutBlank)
Set oText1 = osld.Shapes.AddTextbox(msoTextOrientationHorizontal, Left:=260, Top:=10, Width:=648, Height:=42)
With oText1.TextFrame
.WordWrap = msoFalse
.AutoSize = ppAutoSizeShapeToFitText
.HorizontalAnchor = msoAnchorCenter
With .TextRange
.Text = WID + ", " + Use
With .Font
.Name = "Calibri"
.Size = 40
.Bold = msoFalse
.Color.RGB = RGB(0, 0, 0)
End With
End With
End With
Set oText2 = osld.Shapes.AddTextbox(msoTextOrientationHorizontal, Left:=30, Top:=360, Width:=276, Height:=51)
With oText2.TextFrame
.WordWrap = msoTrue
.AutoSize = ppAutoSizeShapeToFitText
.HorizontalAnchor = msoAnchorNone
With .TextRange
.Text = "Ped " + PedNum + " - " + PedType & Chr(13) & "Results: " + Results & Chr(13) & "Lamina :" + Lamina
With .Font
.Name = "Calibri"
.Size = 12
.Bold = msoFalse
.Color.RGB = RGB(0, 0, 0)
End With
End With
End With
Set oText3 = osld.Shapes.AddTextbox(msoTextOrientationHorizontal, Left:=402, Top:=438, Width:=210, Height:=24)
With oText3.TextFrame
.WordWrap = msoFalse
.AutoSize = ppAutoSizeShapeToFitText
.HorizontalAnchor = msoAnchorNone
With .TextRange
.Text = "IR Image of Process Wafer"
With .Font
.Name = "Calibri"
.Size = 14
.Bold = msoFalse
.Color.RGB = RGB(0, 0, 0)
End With
End With
End With
For i = 0 To Use
thisUse = CStr(i)
strFileJPG = "orig." + WID + "gu" + thisUse + "sPSQ-donorr1200EXFO.jpg"
checkforFile = strPath & strFileJPG
If Dir(checkforFile) <> "" Then
osld.Shapes.AddPicture strPath + file.Name, False, True, imgLeftPos(i), imgTopPos(i), imgHorSize, imgVertSize
' Set oPic0 = osld.Shapes.AddPicture(FileName:=strPath & strFileJPG, LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, Left:=imgLeftPos(0), Top:=imgTopPos(0), Width:=imgHorSize, Height:=imgVertSize)
Else
MsgBox "Donor Image for " + WID + " Use " + thisUse + " was not found."
End If
Next
TotalSlides = TotalSlides + 1
columCount = 0
End If
Wend
MsgBox ("Total Slides Added = " & TotalSlides)
End Sub
Thanks, I look forward to any help that can be offered