Export excel table (and graph) as image (linux, php, python, anything...) - excel

maybe I will have luck here.
I have crazy idea. In order to automatize things at work, I would like to know if there is any possibility to create screenshot or image from excel file using script and then send it somewhere. I dont know whether it is possible using Php, or python, or whatever running on Linux.
If you have working solution on Windows - feel free to post it.
And no, I could not found any possible solution on the internet.

I don't know about taking a screen shot but you can always simply duplicate the excel spreadsheet file and sent it as an attachment to an email address you choose (ie change the '.To' file below). If you have Outlook installed on your machine the following should work. It will append the date to the spreadsheet filename before sending it. You'll of course have to go into the macro and change the settings - but you can always use VBA to create a form prompting you for the necessary information:
Sub send_worksheet()
Dim xFile As String
Dim xFormat As Long
Dim Wb As Workbook
Dim Wb2 As Workbook
Dim FilePath As String
Dim FileName As String
Dim OutlookApp As Object
Dim OutlookMail As Object
On Error Resume Next
Application.ScreenUpdating = False
Set Wb = Application.ActiveWorkbook
ActiveSheet.Copy
Set Wb2 = Application.ActiveWorkbook
Select Case Wb.FileFormat
Case xlOpenXMLWorkbook:
xFile = ".xlsx"
xFormat = xlOpenXMLWorkbook
Case xlOpenXMLWorkbookMacroEnabled:
If Wb2.HasVBProject Then
xFile = ".xlsm"
xFormat = xlOpenXMLWorkbookMacroEnabled
Else
xFile = ".xlsx"
xFormat = xlOpenXMLWorkbook
End If
Case Excel8:
xFile = ".xls"
xFormat = Excel8
Case xlExcel12:
xFile = ".xlsb"
xFormat = xlExcel12
End Select
FilePath = Environ$("temp") & "\"
FileName = Wb.Name & Format(Now, "dd-mmm-yy h-mm-ss")
Set OutlookApp = CreateObject("Outlook.Application")
Set OutlookMail = OutlookApp.CreateItem(0)
Wb2.SaveAs FilePath & FileName & xFile, FileFormat:=xFormat
With OutlookMail
.To = "email.account#gmail.com"
.CC = ""
.BCC = ""
.Subject = "Example of Spreadsheet sent via email"
.Body = "See the attached spreadsheet."
.Attachments.Add Wb2.FullName
.Send
End With
Wb2.Close
Kill FilePath & FileName & xFile
Application.ScreenUpdating = True
End Sub

Related

VBA FilePicker no longer opening from ThisWork.Path

I have a workbook (located on our network) with a macro enabled so I can email exported PDF worksheets via Outlook every Friday. The exported worksheet PDF gets saved to the same location as the Workbook. Outlook then opens the FilePicker and lets me select the file that I want to attach to the email. For some reason the Filepicker is now not opening to ThisWork.path anymore; it opens to my default MyDocuments located on my native computer. The exported file still saves in the proper spot (located on the network) but the FilePicker just wont open to that location. Now, if I move this Workbook to my direct computer, the FilePicker works as it should. I have not changed anything in the VBA so I don't know why this is all the sudden not working. It worked just last week. Below is the code I believe to be relevant to the issue.
'Creates workpath string
Dim mypath As String, fname As String
mypath = ThisWorkbook.Path
fname = CreateObject("Scripting.FileSystemObject").GetBaseName(ThisWorkbook.Name)
'********************************************************
'Saves PDF with Template Text and Date based on Order Date
Sheets(1).ExportAsFixedFormat 0, mypath & "\" & "TEXT Order Sheet " & Format(Range("D7").Value, "mm-dd-yy")
ActiveSheet.Name = Format(Range("D7").Value, "mm-dd-yy")
'********************************************************
Dim xStrFile As String
Dim xFilePath As String
Dim xFileDlg As FileDialog
Dim xFileDlgItem As Variant
Dim xOutApp As Outlook.Application
Dim xMailOut As Outlook.MailItem
Application.ScreenUpdating = False
Set xOutApp = CreateObject("Outlook.Application")
Set xMailOut = xOutApp.CreateItem(olMailItem)
Set xFileDlg = Application.FileDialog(msoFileDialogFilePicker)
xFileDlg.Filters.Clear
xFileDlg.Filters.Add "pdf files", "*.pdf"
xFileDlg.AllowMultiSelect = True
xFileDlg.InitialFileName = ThisWorkbook.Path
If xFileDlg.Show = -1 Then
'********************************************************
With xMailOut
.Display
.To = "fake#email.com"
.Subject = "TEXT" & Range("D7").Value
.HTMLBody = "<p style='font-family:calibri;font-size:12.0pt'>" & "Here is our TEXT order for the week of " & Range("D7").Value & "." & " Please respond to this email to confirm that you have received the order." & .HTMLBody
For Each xFileDlgItem In xFileDlg.SelectedItems
.Attachments.Add xFileDlgItem
Next xFileDlgItem
.Display
End With
End If
Set xMailOut = Nothing
Set xOutApp = Nothing
Application.ScreenUpdating = True
'********************************************************

Convert an activeworkbook on xlsx to put in an email

I have a mail service who is good and i want to put my active workbook on my mail but not on xlsm, i want on xlsx.
I tried a solution but the convertion don't work (error).
My code :
Private Sub CommandButton4_Click()
Dim TheMail As Variant
Dim Path_name As String
Dim File_name As String
Dim Complete_File_name As String
Set LeMail = CreateObject("Outlook.Application")
File_name = ActiveWorkbook.Name
File_name , FileFormat:=xlOpenXMLWorkbook
Path_name = ThisWorkbook.Path
Complete_File_name = Path_name & "\" & File_name
With TheMail.CreateItem(olMailItem)
.Subject = "Prerequisite"
.To = ""
.Body = ""
.Attachments.Add Complete_File_name & ".xlsx"
.Display
End With
End Sub
Thanks you for all of your response,
Have a nice day !
You cannot simple add a xlsx extension! They are two different workbooks. You should firstly save it as xlsx, to make the conversion you are talking about. Anyhow, a file having the name you try attaching does not even exist...
Please, use the next way:
Private Sub CommandButton4_Click()
Dim TheMail As Variant, Path_name As String, File_name As String
Dim Complete_File_name As String
Set TheMail = CreateObject("Outlook.Application")
File_name = ActiveWorkbook.name
Path_name = ThisWorkbook.path
Complete_File_name = Path_name & "\" & left(File_name, InStrRev(File_name, ".") - 1) & ".xlsx"
ActiveWorkbook.saveas Complete_File_name, xlWorkbookDefault
With TheMail.CreateItem(olMailItem)
.Subject = "Prerequisite"
.To = ""
.body = ""
.Attachments.Add Complete_File_name
.Display
End With
End Sub
If you do not need the 'converted' xlsx file, you can delete it. After closing, or use SaveCopyAs instead of saveAs, to not be necessary to close it.

Browse folders to attach files in Outlook mail using VBA Excel [duplicate]

This question already has answers here:
How to add an attachment to an email using VBA in Excel
(2 answers)
Closed 1 year ago.
I am working on an userform in VBA Excel that allows a user to submit their request. The users complete the form then click on the Send button. An Outlook mail will be opened and the completed form is automatically attached.
The users usually have data or/and documents related to the request. I want to add functionality to my userform, which allows them to browse their PC and import the files. When they click on the Send button, these files will be attached to the same mail as the original Excel form.
Below are the codes for my Send button.
Function CreationMail(criticité As String)
Dim xFile As String
Dim xFormat As Long
Dim Wb As Workbook
Dim Wb2 As Workbook
Dim FilePath As String
Dim FileName As String
Dim OutlookApp As Object
Dim OutlookMail As Object
Dim rng As Range
Set Sheet1 = ThisWorkbook.Sheets("Formulaire")
Set rng = Sheets("Formulaire").Range("C6:D11").SpecialCells(xlCellTypeVisible)
Application.ScreenUpdating = False
Set Wb = Application.ActiveWorkbook
ActiveSheet.Copy
Set Wb2 = Application.ActiveWorkbook
Select Case Wb.FileFormat
Case xlOpenXMLWorkbook:
xFile = ".xlsx"
xFormat = xlOpenXMLWorkbook
Case xlOpenXMLWorkbookMacroEnabled:
If Wb2.HasVBProject Then
xFile = ".xlsm"
xFormat = xlOpenXMLWorkbookMacroEnabled
Else
xFile = ".xlsx"
xFormat = xlOpenXMLWorkbook
End If
Case Excel8:
xFile = ".xls"
xFormat = Excel8
Case xlExcel12:
xFile = ".xlsb"
xFormat = xlExcel12
End Select
FilePath = Environ$("temp") & "\"
FileName = "STATSAE" & "_" & Format(Now, "yymmdd") & "_" & Format(Now, "hhnnss")
Set OutlookApp = CreateObject("Outlook.Application")
Set OutlookMail = OutlookApp.CreateItem(0)
Wb2.SaveAs FilePath & FileName & xFile, FileFormat:=xFormat
With OutlookMail
.To = ";" & ";"
.CC = ""
If criticité = "Haute" Then
.Importance = olImportanceHigh
End If
If criticité = "" Then
.Importance = olImportanceNormal
End If
If criticité = "Faible" Then
.Importance = olImportanceNormal
End If
.Subject = "Request" & Space(1) & FileName
.Attachments.Add Wb2.FullName
.Body = "Please find the requested information" & vbCrLf & "Best Regards"
.HTMLBody = RangetoHTML(rng)
.Display
End With
Wb2.Close
Kill FilePath & FileName & xFile
Set OutlookMail = Nothing
Set OutlookApp = Nothing
Application.ScreenUpdating = True
End Function
First of all, you need to do customizations to your form to pick up files that requires to be attached to the email. Then in the code you can repeat the Add method of the Attachments class for each entry the user has chosen. The source of the attachment can be a file (represented by the full file system path with a file name) or an Outlook item that constitutes the attachment.
.Attachments.Add Wb2.FullName
.Attachments.Add your_chosen_file

Send sheet via email

I have a workbook called Status report which contains several sheets. I need to send sheet8 (Called tables) via email.
I generated code but it is giving me errors.
Location of file is on desktop.
Option Explicit
Sub EmailWithOutlook()
Dim oApp As Object
Dim oMail As Object
Dim WB As Workbook
Dim FileName As String
Dim wSht As Worksheet
Dim shtName As String
Application.ScreenUpdating = False
ActiveSheet.Copy
Set WB = ActiveWorkbook
FileName = WB.Worksheets(1).Name
Kill "C:\Users\Default\Desktop" & "Status report.xlsm"
On Error GoTo 0
WB.SaveAs FileName:="C:\Users\Default\Desktop" & "Status report.xlsm"
Set oApp = CreateObject("Outlook.Application")
Set oMail = oApp.CreateItem(0)
With oMail
.To = "sleepyyx#gmail.com"
.Subject = "Test workbook"
.body = "Hello, could you please check workbook" & vbCrLf & vbCrLf & _
"I attached you file"
.Attachments.Add WB.FullName
.Display
End With
WB.ChangeFileAccess Mode:=xlReadOnly
Kill WB.FullName
WB.Close SaveChanges:=False
Application.ScreenUpdating = True
Set oMail = Nothing
Set oApp = Nothing
End Sub
Before you kill a File in Kill "C:\Users\Default\Desktop\" & "Status report.xlsm", you need to check if the file exists :
~~> Check if file exists
If Dir("C:\Users\Default\Desktop\" & "Status report.xlsm") <> "" Then
Kill "C:\Users\Default\Desktop\" & "Status report.xlsm"
End If
And you forget the backslash
You probably are trying to use Filename from the cell.
Consider using a variable for consistency and in case it changes, and make sure you include the backslash between the path and filename.
FileName = WB.Worksheets(1).Name
Dim sFullFile As String
sFullFile = "C:\Users\Default\Desktop\" & FileName
If Dir(sFullFile) <> "" Then Kill sFullFile
WB.SaveAs FileName:=sFullFile
Another thing to note is that if you are saving it as an XLSM then the original format also has to be XLSM, or the SaveAs will error if you don't specify the File Format.
XlFileFormat Enumeration Documentation

Need to send attachment in XLSX format using CDO mail

Hi Iam using follwing code as a sample to send a mail with attachment via SMTP, but the attachment what it send is in XLSM format i need that to be in XLSX (non macro) format. Kindly help me to fo this.
Option Explicit
'This procedure will mail the whole workbook
'You can 't send a Workbook that is open with CDO.
'That's why it use SaveCopyAs to save it with another name and send that file.
Sub CDO_Mail_Workbook()
'Working in 2000-2007
Dim wb As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim FileExtStr As String
Dim iMsg As Object
Dim iConf As Object
' Dim Flds As Variant
Set wb = ActiveWorkbook
If Val(Application.Version) >= 12 Then
If wb.FileFormat = 51 And wb.HasVBProject = True Then
MsgBox "There is VBA code in this xlsx file, there will be no VBA code in the file you send." & vbNewLine & _
"Save the file first as xlsm and then try the macro again.", vbInformation
Exit Sub
End If
End If
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'Make a copy of the file/Mail it/Delete it
'If you want to change the file name then change only TempFileName
TempFilePath = Environ$("temp") & "\"
TempFileName = "Copy of " & wb.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")
FileExtStr = "." & LCase(Right(wb.Name, Len(wb.Name) - InStrRev(wb.Name, ".", , 1)))
wb.SaveCopyAs TempFilePath & TempFileName & FileExtStr
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
' iConf.Load -1 ' CDO Source Defaults
' Set Flds = iConf.Fields
' With Flds
' .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
' .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "Fill in your SMTP server here"
' .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
' .Update
' End With
With iMsg
Set .Configuration = iConf
.To = "ron#debruin.nl"
.CC = ""
.BCC = ""
.From = """Ron"" <ron#something.nl>"
.Subject = "This is a test"
.TextBody = "This is the body text"
.AddAttachment TempFilePath & TempFileName & FileExtStr
.Send
End With
'If you not want to delete the file you send delete this line
Kill TempFilePath & TempFileName & FileExtStr
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
I think what you'll need to do, is have this code reside in an add-in. That way you aren't trying to send the existing file with code via email.
You are sending workbook which you are running code from so it must be .xlsm and so you send it.
You must either create a copy of your workbook without macros and then send this copy or move your macro to PERSONAL (assuming macro you posted is the only code contained in your workbook)

Resources