Hi all,
I've been doing some writing in VBA over the last few months and i have managed to get some pretty cool things to work. I have been working on a way to get a macro to;
Read through a list of product codes
Look to a central server to see if the image exists
If it does, Fetch the image
insert the image into a second sheet
put the product code and price and title info below it
loop to the next code
insert the image alongside the previous one until there are 5 images in a line
increase row count by 3 and start again until all codes have been processed.
insert a title (user defined) and company logo at the top
export to PDF and save in a folder on users desktop using the title as filename
tell you haw many images were not found
then cleanup so any changes to the original spreadsheet are reversed
'make you a cup of tea
'take the bins out ....
and, it works!
Mostly i found code on here and using Google and filled in the gaps, but there are a last couple of step that i just cant work out.
so, heres the code ... i know its messy and inefficient but i'm scaling the learning curve here and i'm just impressed it works!
So, the things i need help with are;
When it exports to PDF obviously it uses the print area markers to set the page size, sometimes this will split the image and description onto different pages. can i define and force it to export a certain size to a page ie Xrows and Xcolumns = a page ... ALWAYS!
I cannot for the life of me work out how to include a footer on each page, can this not be done exporting to PDF? if not how can i define the last row on each page to insert the text? (company info, always the same)
sometimes the image's aspect ratio means when it fits the width of the cell the hight is too large, 90% of the time thats not really an issue, and doing it by height means that 90% of the images would be an issue can i do
i might have just worked out that one there .... ill go test it ... yep that worked
and lastly, when i enter the price as part of
it will show with the minimum amount of decimal places possible, ive tried numerous ways to force it to "0.00" but if it is possible i have not stumbled over the correct syntax for it yet.
I hope there is someone who has made it this far, and can help, but at least the code above might prove useful for someone trying to do similar things.
Cheers
ImaBus
I've been doing some writing in VBA over the last few months and i have managed to get some pretty cool things to work. I have been working on a way to get a macro to;
Read through a list of product codes
Look to a central server to see if the image exists
If it does, Fetch the image
insert the image into a second sheet
put the product code and price and title info below it
loop to the next code
insert the image alongside the previous one until there are 5 images in a line
increase row count by 3 and start again until all codes have been processed.
insert a title (user defined) and company logo at the top
export to PDF and save in a folder on users desktop using the title as filename
tell you haw many images were not found
then cleanup so any changes to the original spreadsheet are reversed
'make you a cup of tea
'take the bins out ....
and, it works!
Mostly i found code on here and using Google and filled in the gaps, but there are a last couple of step that i just cant work out.
so, heres the code ... i know its messy and inefficient but i'm scaling the learning curve here and i'm just impressed it works!
Code:
Sub Sales_sheet_1111111111111111()
Dim PictureFileName As String, TargetCells As Range
Dim p As Object, t As Double, l As Double, w As Double, h As Double
Dim myDir As String
Dim myCell As String
Dim myCount As Single
Dim mycol As String
Dim myrow As String
Dim filename As String
Dim strSpecialFolderPath
Dim objWSHShell As Object
Dim TargetCell As Range
Set objWSHShell = CreateObject("WScript.Shell")
SpecialFolderPath = objWSHShell.SpecialFolders("Desktop") & "\Sales Sheets\" 'Change this to the location the files are to be copied to
Dim mycode As String
Dim mytitle As String
Dim fail As String
Dim code As String
Dim rrp As String
Dim entercode As String
Dim entertitle As String
Dim pic As Shape
Dim wb As Workbook, wbName As String
Application.ScreenUpdating = False
mycode = InputBox(prompt:="What column contains the B Code?", Default:="A")
Myrrp = InputBox(prompt:="What column contains the RRP?", Default:="c")
mytitle = InputBox(prompt:="What column contains the Title?", Default:="b")
filename = InputBox(prompt:="Please enter Title.", Title:="Sales sheet title", Default:="Bookspeed sales sheet")
myCount = 2 'mycount is the starting row that the file names start at
mycol = "b" 'column where first image will be inserted
myrow = 2
myDir = "\\10.0.0.1\Bookspeed Images\large\"
fail = 0
ActiveSheet.Copy
ActiveSheet.Name = "Sheet"
Sheets.Add.Name = "Sales Sheet"
If Len(Dir(SpecialFolderPath, vbDirectory)) = 0 Then MkDir SpecialFolderPath
Sheets("Sales Sheet").Range("A:A,C:C,E:E,G:G,I:I,K:K").ColumnWidth = 2.6
Do
myCell = Sheets("sheet").Range(mycode & myCount)
Title = Sheets("sheet").Range(mytitle & myCount)
rrp = Sheets("sheet").Range(Myrrp & myCount)
If Len(Dir(myDir & myCell & ".jpg")) > 0 Then
Sheets("Sales Sheet").Select
Sheets("Sales Sheet").Range(mycol + myrow).Select
Set p = ActiveSheet.Pictures.Insert(myDir & myCell & ".jpg")
ActiveCell.ColumnWidth = 12
ActiveCell.RowHeight = 100
With p
.ShapeRange.LockAspectRatio = msoTrue
.width = ActiveCell.width
.Top = ActiveCell.Top + (ActiveCell.height - p.height)
'.height = Range(mycol + myrow).height
'.Top = Range(mycol + myrow).Top
'.Left = Range(mycol + myrow).Left
'.Placement = xlMoveAndSize
'.HorizontalAlignment = xlBottom
'.ShapeRange.VerticalAlignment = xlCenter
End With
entertitle = myrow + 2
entercode = myrow + 1
Sheets("Sales Sheet").Range(mycol + entercode) = myCell & " " & "£" & rrp
Sheets("Sales Sheet").Range(mycol + entercode).Font.Size = 8
Sheets("Sales Sheet").Range(mycol + entercode).Font.Bold = True
Sheets("Sales Sheet").Range(mycol + entercode).HorizontalAlignment = xlCenter
Sheets("Sales Sheet").Range(mycol + entercode).WrapText = True
Sheets("Sales Sheet").Range(mycol + entertitle) = Title
Sheets("Sales Sheet").Range(mycol + entertitle).Font.Size = 6
Sheets("Sales Sheet").Range(mycol + entertitle).WrapText = True
Sheets("Sales Sheet").Range(mycol + entertitle).HorizontalAlignment = xlCenter
Sheets("Sales Sheet").Range(mycol + entertitle).VerticalAlignment = xlTop
'Sheets("Sales Sheet").Range(mycol + entertitle).Font.Bold = True
Select Case mycol
Case "b"
mycol = "d"
Case "d"
mycol = "f"
Case "f"
mycol = "h"
Case "h"
mycol = "j"
Case "j"
mycol = "b"
myrow = myrow + 3
End Select
Set p = Nothing
Else: fail = fail + 1
End If
myCount = myCount + 1
Loop Until IsEmpty(Sheets("sheet").Range(mycode & myCount))
Sheets("Sales Sheet").Range("a1") = filename
With Sheets("Sales Sheet").Range("a1")
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlTop
.MergeCells = True
End With
With Sheets("Sales Sheet").Range("a1")
.Name = "Arial"
.Font.Bold = True
.Font.Size = 30
End With
'insert logo
If Len(Dir("C:\Users\deanrougvie\Desktop\logo_bookspeed.gif")) > 0 Then _
Range("j1:k1").Select
Set p = ActiveSheet.Pictures.Insert("C:\Users\deanrougvie\Desktop\logo_bookspeed.gif")
With p
.Top = ActiveCell.Top
.width = ActiveCell.width
.Top = ActiveCell.Top + (ActiveCell.height - p.height)
'ActiveCell.Offset(1).EntireRow.Insert
End With
Set p = Nothing
Application.DisplayAlerts = False
Sheets("Sheet").Select
ActiveWindow.SelectedSheets.Delete
Sheets("Sales Sheet").Select
'export to PDF
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, filename:=SpecialFolderPath & filename & ".pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=True
'cleanup
Set wb = ActiveWorkbook
wbName = ThisWorkbook.Path & filename & ".xls"
wb.SaveAs wbName
wb.Close
Kill wbName
MsgBox ("PDF created, " & fail & "images not found")
Application.ScreenUpdating = True
End Sub
So, the things i need help with are;
When it exports to PDF obviously it uses the print area markers to set the page size, sometimes this will split the image and description onto different pages. can i define and force it to export a certain size to a page ie Xrows and Xcolumns = a page ... ALWAYS!
I cannot for the life of me work out how to include a footer on each page, can this not be done exporting to PDF? if not how can i define the last row on each page to insert the text? (company info, always the same)
sometimes the image's aspect ratio means when it fits the width of the cell the hight is too large, 90% of the time thats not really an issue, and doing it by height means that 90% of the images would be an issue can i do
Code:
.width = ActiveCell.width
if pic height > than activecell height
then .height = ActiveCell.height
and lastly, when i enter the price as part of
Code:
Range(mycol + entercode) = myCell & " " & "£" & rrp
it will show with the minimum amount of decimal places possible, ive tried numerous ways to force it to "0.00" but if it is possible i have not stumbled over the correct syntax for it yet.
I hope there is someone who has made it this far, and can help, but at least the code above might prove useful for someone trying to do similar things.
Cheers
ImaBus