Function AlreadyOpen(sFname As String) As Boolean
Dim wkb As Workbook
On Error Resume Next
Set wkb = Workbooks(sFname)
If Err.Number = 0 Then
AlreadyOpen = True
Else
AlreadyOpen = False
End If
End Function
----------------------------------------------------------------------------------------------------------------------------------------
Sub NWM_DILAA_ToOutlook()
Dim oLookApp As Outlook.Application
Dim oLookFdr As Outlook.Folder
Dim oLookNsp As Outlook.Namespace
Dim oLookItm As Outlook.MailItem
Dim NVPath As String
Dim newdate As String
Dim mailSubj As String
Dim rng As Range
Dim wdrange As Object
Dim wdoc As Object
Dim wbk As Workbook
Dim ccypath As String
Dim mailTo_List As String
Dim mailCC_List As String
Dim arrSh(2) As String
Dim namesheet As String
arrSh(0) = "New NWM Summary"
arrSh(1) = "Grid"
arrSh(2) = "Summary"
'---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ThisWorkbook.Activate
NWMSolosPath = ThisWorkbook.Sheets("Reference").Range("NWMSolospath")
newdate = Format(ActiveSheet.Range("COB_DATE"), "yyyymmdd")
ccypath = NWMSolosPath & "\" & newdate & " NW Markets Solo DILAA" & "\" & "CCy Profile"
mailTo_List = Sheets("Reference").Range("NWMToList").Value
mailCC_List = Sheets("Reference").Range("NWMCCList").Value
'---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
If AlreadyOpen(newdate & " " & " NW Markets Solo DILAA.xlsm") = False Then
Application.AutomationSecurity = msoAutomationSecurityLow
Set wbk = Workbooks.Open(Filename:=NWMSolosPath & "\" & newdate & " NW Markets Solo DILAA" & "\" & newdate & " NW Markets Solo DILAA.xlsm", UpdateLinks:=0, ReadOnly:=True)
Else
Set wbk = Workbooks(newdate & " " & " NW Markets Solo DILAA.xlsm")
Application.AutomationSecurity = msoAutomationSecurityByUI
End If
'---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
If AlreadyOpen("CCy Profile.xlsx") = False Then
wbk.Sheets(Array("CCY Profile")).Copy
ActiveSheet.Cells.Copy
Cells.PasteSpecial paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Format (Range("B4:E76").Select), ("#,##0")
Range("A1").Select
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:=ccypath & ".xlsx"
Application.DisplayAlerts = True
Set wb2 = ActiveWorkbook
Else
Set wb2 = Workbooks("CCy Profile.xlsx")
End If
'---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
With ThisWorkbook.Sheets("Reference")
For i = 1 To 4
Filerange = .Cells(6, i + 3).Value
If i = 4 Then j = 2 Else j = i - 1
Call createJpg(arrSh(j), Range(Filerange).Address, "img" & i, wbk)
Next i
End With
'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = mailTo_List
.CC = mailCC_List
.Subject = "SOC Metrics for Daily Call"
.Attachments.Add wb2.FullName 'attach currency file
.Display
'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
For i = 1 To 4 'Step -1 'reverse order'With OutMail
.Attachments.Add Environ("temp") & "\img" & i & ".jpg" ', olByValue, 0
.HTMLBody = .HTMLBody & "<br><img src='cid:img" & i & ".jpg'><br><br>"
Next i 'End With
.HTMLBody = .HTMLBody & "<br><br>Regards" & "<br><br>"
End With 'Outmail End with
wbk.Close False
wb2.Close False
End Sub
Private Sub createJpg(namesheet As String, nameRange As String, nameFile As String, wbk As Workbook)
wbk.Worksheets(namesheet).Activate
Set Plage = wbk.Worksheets(namesheet).Range(nameRange)
Plage.CopyPicture
With wbk.Worksheets(namesheet).ChartObjects.Add(Plage.Left, Plage.Top, Plage.Width, Plage.Height)
.Activate
.Chart.paste
.Chart.Export Environ("temp") & "\" & nameFile & ".jpg", "JPG"
End With
wbk.Worksheets(namesheet).ChartObjects(Worksheets(namesheet).ChartObjects.Count).Delete
Set Plage = Nothing
End Sub