WildBurrow
New Member
- Joined
- Apr 5, 2021
- Messages
- 41
- Office Version
- 365
- Platform
- Windows
Good afternoon,
Hitting a wall... I've created a photo log template that allows users to insert and describe pictures; the file is saved to .pdf and then closed. The purpose of which is to move a number of .jpg files into a single document, thus decreasing file storage requirements (original image files will be deleted afterwards). The template allows the user to add sheets as needed that will format images as portrait or landscape. The sheet options include one picture per sheet or multiple pictures per sheet. No problems with this portion of the project.
However, we would also like to keep unique photos in a training folder to be used as needed. I had this bright idea that it would be convenient to copy the pictures to the training folder before closing the template. If a photo is exported and saved for training purposes, I need;
Also, my current code looks to export a single picture at a time simply because I thought I could manage writing the code sneaky. However, I am now wondering if there is was a way for the user to select more than one picture and export them all at once. Any suggestions?
Any help will be greatly appreciated.
Hitting a wall... I've created a photo log template that allows users to insert and describe pictures; the file is saved to .pdf and then closed. The purpose of which is to move a number of .jpg files into a single document, thus decreasing file storage requirements (original image files will be deleted afterwards). The template allows the user to add sheets as needed that will format images as portrait or landscape. The sheet options include one picture per sheet or multiple pictures per sheet. No problems with this portion of the project.
However, we would also like to keep unique photos in a training folder to be used as needed. I had this bright idea that it would be convenient to copy the pictures to the training folder before closing the template. If a photo is exported and saved for training purposes, I need;
- Define root directory that will allow the user to pick a subfolder
- Define a default name that utilized a cell value and a suffix like "Trng"
- Add unique number if the file exists
- There is more than one picture(chart) on the sheet - also seems like the current code only likes landscape orientation
- The sheet name is not "Sheet1" - given that the user can add multiple sheets, I can't provide a sheet name
- Passing the exported file to the Function to obtain a unique number
Also, my current code looks to export a single picture at a time simply because I thought I could manage writing the code sneaky. However, I am now wondering if there is was a way for the user to select more than one picture and export them all at once. Any suggestions?
Any help will be greatly appreciated.
VBA Code:
Sub ExportDesktop()
Dim outFldr As String, SourceFldr As String
Dim co As ChartObject
SourceFldr = "C:\Users\10030798\Desktop\2016\"
If SourceFldr = "" Then
Msgbox "Export Cancelled"
Else
Dim myChart As String, MyPicture As String, DefaultNm As String, fileExt As String, saveAsExportFilename As String
Dim PicWidth As Long, PicHeight As Long
DefaultNm = Worksheets("Cover").range("O12").Value & ".Trng"
fileExt = ".jpg"
saveAsExportFilename = GetSaveAsExportFilename(SourceFldr, DefaultNm, fileExt)
Application.ScreenUpdating = False
On Error GoTo Finish
'Define picture as selection
MyPicture = Selection.Name
'Sets picture parameters
With Selection
PicHeight = .ShapeRange.Height
PicWidth = .ShapeRange.Width
End With
'Initiates add chart and set parameters
Charts.Add
ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1"
Selection.Border.LineStyle = 0
myChart = Selection.Name & " " & Split(ActiveChart.Name, " ")(2)
'Sets chart width/height same as picture width/height
With ActiveSheet
With .Shapes(myChart)
.Width = PicWidth
.Height = PicHeight
End With
'Copies back to picture that was selected and defined
.Shapes(MyPicture).Copy
'Pastes pic to new chart
With ActiveChart
.ChartArea.Select
.Paste
End With
'Exports chart then deletes it from the sheet
.ChartObjects(1).Chart.Export SourceFldr & "\" & DefaultNm & fileExt
.Shapes(myChart).Cut
End With
Application.ScreenUpdating = True
Exit Sub
Finish:
Msgbox "You must select a picture"
End If
End Sub
Public Function GetSaveAsExportFilename(ByVal SourceFldr As String, ByVal DefaultNm As String, Optional ByVal fileExt As String = ".jpg") As String
Dim tempFilename As String
Dim fileCount As Long
tempFilename = tempFilename = SourceFldr & DefaultNm & fileExt
If Len(Dir(tempFilename, vbNormal)) > 0 Then
fileCount = fileCount + 1
tempFilename = SourceFldr & DefaultNm & " #" & fileCount & fileExt
If Len(Dir(tempFilename, vbNormal)) = 0 Then
End If
End If
GetSaveAsExportFilename = tempFilename
End Function
'Code blended from:
'Source:https://www.mrexcel.com/board/threads/vba-to-export-all-charts-in-workbook-to-selected-folder.784004/ (BrianMH) &
'Source:https://www.tapatalk.com/groups/xlvbafr/export-pictures-from-excel-t207.html#.UguN7ZKyAqY