Hello Experts!
I am totally stuck when it comes to inserting images with Access VBA/Macro. Is there any way to do so?
Below is my code so far. It is to export query data to Excel Template. I looking for a way to insert the ProductPicture from every record to Excel. The product pictures are stored in the Access database as Path at the moment.
Code:
Option Explicit
'Enter Location of your Template Here
Const ExcelTemplate = "\\nas\Database\Product Development\MasterList\QuoteSheetTemplate.xlsx"
'Enter the Folder Directory to save results to
Const SaveResutsFldr = "\\nas\Database\Product Development\MasterList"
Sub CreateWorkbook()
Dim dbs As DAO.Database
Dim qdf As DAO.QueryDef
Dim strSQL As String
strSQL = "SELECT * FROM QuoteItems WHERE Lognumber='" & Forms![QuoteLog]![LogNumber] & "';"
Set dbs = CurrentDb
With dbs
.QueryDefs.Delete ("myQuery")
Set qdf = dbs.CreateQueryDef("myQuery", strSQL)
.Close
End With
Application.RefreshDatabaseWindow
Dim SaveAsStr As String
Dim ExcelApp, WB As Object
Dim qry As QueryDef
Dim i As Integer
Dim c As Integer
c = DCount("*", "myQuery") + 9
'Create Reference to Run Excel
Set qry = CurrentDb.QueryDefs("myQuery")
Set ExcelApp = CreateObject("Excel.Application")
'Create Reference to your Table
Dim T As Recordset
Set T = qry.OpenRecordset
'Loop through all Record on Table
If Not (T.BOF And T.EOF) Then
T.MoveFirst
End If
Do While Not T.EOF
'Open Your Excel Template
Set WB = ExcelApp.Workbooks.Open(ExcelTemplate)
For i = 10 To c
'Enter your data from your table here to the required cells
WB.Worksheets("sheet1").Cells(3, 41).Value = Forms![QuoteLog]![LogNumber]
WB.Worksheets("sheet1").Cells(3, 2).Value = Forms![QuoteLog]![CustomerName]
WB.Worksheets("sheet1").Cells(4, 41).Value = Forms![QuoteLog]![SentDate]
WB.Worksheets("sheet1").Range("B" & i) = T("LogNumber")
WB.Worksheets("sheet1").Range("C" & i) = T("ProductSpec")
Dim strExcel As String
Dim n As Integer
strExcel = "=IF(A" & i & " = """"," & """EMPTY""" & "," & """FILLED""" & ") "
WB.Worksheets("sheet1").Range("D" & i).Formula = strExcel
'Repeat this line for each piece of data you need entered
'Changing the Sheet name, cell range, a field name as per your requirements
'WB.Wor...
'WB.Wor...
T.MoveNext
Next i
i = i + 1
Loop
'Save and Close the Workbook
SaveAsStr = SaveResutsFldr & "\" & [Forms]![QuoteLog].LogNumber & "_" & [Forms]![QuoteLog].CustomerName & "_" & Format(Now(), "yymmdd") & ".xlsx"
WB.SaveAs SaveAsStr
WB.Close
Set WB = Nothing
'Move to the Next Record
'Close down the Excel Application
ExcelApp.Quit
Set ExcelApp = Nothing
Shell "EXCEL.EXE """ & SaveAsStr & "", vbNormalFocus
End Sub
And all I could find was a code for the excel, which I have no idea how to put into Access.
Code:
shapes.AddPicture(Filename, LinkToFile, SaveWithDocument, Left, Top, Width, Height)
Any help would be much appreciated!!
Thank you in advance,
Jo