Option Explicit On Imports VERAG_PROG_ALLGEMEIN Imports Microsoft.Office.Tools.Ribbon Imports System.Windows.Forms Imports Microsoft.Office.Interop Imports VERAGAddIn.ThisAddIn Imports System.Runtime.InteropServices Imports System.Text.RegularExpressions Imports System.IO Public Class VERAGRibbon Dim LOGIN_OK = False Public Shared Event bla() Private Sub CustomerRibbon_Load(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonUIEventArgs) Handles MyBase.Load End Sub Private Function CreateRibbonDropDownItem() As RibbonDropDownItem Return Me.Factory.CreateRibbonDropDownItem() End Function Private Function CreateRibbonMenu() As RibbonMenu Return Me.Factory.CreateRibbonMenu() End Function 'Private Function CreateRibbonButton() As RibbonButton ' Dim button As RibbonButton = Me.Factory.CreateRibbonButton() ' ' AddHandler (button.Click), AddressOf Button_Click ' Return button 'End Function Private Sub Ribbon1_Load(ByVal sender As System.Object, ByVal e As RibbonUIEventArgs) Handles MyBase.Load 'VERAG_PROG_ALLGEMEIN.cAllgemein.TESTSYSTEM = False VERAG_PROG_ALLGEMEIN.cAllgemein.TESTSYSTEM = True LOGIN_OK = AVISO_Mail_Functions.initFirmaUser() End Sub Private Sub Button2_Click(sender As Object, e As RibbonControlEventArgs) Handles Button2.Click If LOGIN_OK Then AVISO_Mail_Functions.NewAviso_Hauptfenster() End Sub Private Sub Button4_Click(sender As Object, e As RibbonControlEventArgs) Handles Button4.Click Try MsgBox(System.Security.Principal.WindowsIdentity.GetCurrent().Name) Catch ex As Exception End Try End Sub Sub Button1_Click_1(sender As Object, e As RibbonControlEventArgs) Handles Button1.Click If LOGIN_OK Then AVISO_Mail_Functions.newEKOLMAIL() End Sub Private Sub Button1_Click(sender As Object, e As RibbonControlEventArgs) 'MsgBox("HI") ' Dim ThisAddIn As New ThisAddIn Dim s As String = "" 'Dim app = ThisAddIn.getapp() ' ThisAddIn.PrintAPageFromEmail() RaiseEvent bla() End Sub Sub btnPdfPrint_Click(sender As Object, e As RibbonControlEventArgs) Handles btnPdfPrint.Click AVISO_Mail_Functions.printPDFs() End Sub Sub Button3_Click(sender As Object, e As RibbonControlEventArgs) Handles Button3.Click If LOGIN_OK Then AVISO_Mail_Functions.addMailToAviso_Hauptfenster() End Sub Private Sub btnVM_Click(sender As Object, e As RibbonControlEventArgs) Handles btnVM.Click ' ToDo -> Logik für das Einfügen eines PDFs aus einer Mail in eine Vollmacht im Aviso. If LOGIN_OK Then AVISO_Mail_Functions.addMailToAviso_Vollmachten() End Sub End Class Public Class AVISO_Mail_Functions Shared Sub addMailToAviso_Hauptfenster() Try Dim explorer As Outlook.Explorer = Globals.ThisAddIn.Application.ActiveExplorer Dim selection As Outlook.Selection = explorer.Selection If selection.Count > 0 Then Dim selectedItem = selection(1) Dim mailItem As Outlook.MailItem = selectedItem mailItem = DirectCast(mailItem, Outlook.MailItem) addMailToAviso(mailItem) End If Catch ex As Exception MsgBox(ex.Message & ex.StackTrace) End Try End Sub Shared Function getAttachmentStringList(Mail) As List(Of String) Dim att As New List(Of String) For Each attachment As Outlook.Attachment In Mail.Attachments att.Add(attachment.FileName) Next Return att End Function Shared Sub addMailToAviso(mailItem As Outlook.MailItem, Optional isFormular As Boolean = False) Try If mailItem IsNot Nothing Then ' Remove special characters from the file name and make sure it is not longer than 100 characters ' Remove special characters from the file name and make sure it is not longer than 100 characters Dim strFileName As String = "E-Mail" If mailItem.Subject IsNot Nothing AndAlso mailItem.Subject <> "" Then strFileName = Left(Regex.Replace(mailItem.Subject, "[\/\\\:\?!€,&'\*\<\>\|""]", ""), 100).Replace("""", "").Replace(vbTab, " ") & ".msg" End If 'Dim strFileName As String = "Mail.msg" Dim pdf_mail = "" Dim SendungsId = -1 Dim Art = "" Dim ATTACHMENTS_LIST As New List(Of String) loadAttachment(mailItem, ATTACHMENTS_LIST) If isMail_EKOL(mailItem) Then If vbYes = MsgBox("EKOL-Anhänge laden?", vbYesNoCancel) Then getATT_EKOL(mailItem, ATTACHMENTS_LIST) End If End If Dim Bezeichnung = strFileName Dim AvisoId = getAvisoId(Bezeichnung, pdf_mail, SendungsId, Art, ATTACHMENTS_LIST, isFormular) ')getAttachmentStringList(mailItem)) If AvisoId < 0 Then Exit Sub Dim BezAnhang = If(Bezeichnung <> strFileName, Bezeichnung, "") 'Wenn die Bezeichnung geändert wurde --> und wenn nur 1 Anhang, dann wird die Bezeichnung für den Anhang verwendet. If pdf_mail = "PDF" Then saveAttachment(ATTACHMENTS_LIST, AvisoId, SendungsId, Art, BezAnhang) ElseIf pdf_mail = "PDFMAIL" Then saveMail(mailItem, AvisoId, Bezeichnung,, SendungsId, Art) saveAttachment(ATTACHMENTS_LIST, AvisoId, SendungsId, Art, BezAnhang) Else 'ONLYMAIL saveMail(mailItem, AvisoId, Bezeichnung,, SendungsId, Art) End If End If Catch ex As Exception MsgBox(ex.Message & ex.StackTrace) End Try End Sub Shared Sub printPDFs() Dim cntPDFs = countPDFs() If cntPDFs > 0 Then If vbYes = MsgBox("Möchten Sie alle " & cntPDFs & " PDF-Anhänge drucken?", vbYesNoCancel) Then Try Dim explorer As Outlook.Explorer = Globals.ThisAddIn.Application.ActiveExplorer Dim selection As Outlook.Selection = explorer.Selection If selection.Count > 0 Then Dim selectedItem = selection(1) Dim mailItem As Outlook.MailItem = selectedItem If mailItem IsNot Nothing Then Dim attachments As Outlook.Attachments = mailItem.Attachments For Each attachment As Outlook.Attachment In mailItem.Attachments 'Dim io As New IO.FileInfo(attachment.PathName) If attachment.FileName.EndsWith(".pdf") Then Dim TMP_PATH As String = VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.getTMPPath(attachment.FileName, ".pdf", False, False, "OutlookAttach_Print") attachment.SaveAsFile(TMP_PATH) Marshal.ReleaseComObject(attachment) If IO.File.Exists(TMP_PATH) Then VERAG_PROG_ALLGEMEIN.cFormularManager.PrintViaSpirePDF({TMP_PATH}) ', cboPrinter.SelectedItem.ToString) Try : IO.File.Delete(TMP_PATH) : Catch : End Try End If End If Next End If End If Catch ex As Exception MsgBox(ex.Message & ex.StackTrace) End Try End If End If End Sub Shared Sub newEKOLMAIL() Try Dim explorer As Outlook.Explorer = Globals.ThisAddIn.Application.ActiveExplorer Dim selection As Outlook.Selection = explorer.Selection If selection.Count > 0 Then Dim selectedItem = selection(1) Dim mailItem As Outlook.MailItem = selectedItem If mailItem IsNot Nothing Then If AVISO_Mail_Functions.isMail_EKOL(mailItem) Then VERAG_PROG_ALLGEMEIN.cFormularManager.PrintViaSpirePDF(AVISO_Mail_Functions.getATT_EKOL(mailItem)) End If End If End If Catch ex As Exception MsgBox(ex.Message & ex.StackTrace) End Try End Sub Shared Function initFirmaUser() As Boolean Dim LOGIN_OK = False VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA = "VERAG" 'DEFAULT Try Dim ADMIN As New cADMIN Try Dim WI = System.Security.Principal.WindowsIdentity.GetCurrent().Name If WI.Contains("\") Then Dim Split() = WI.Split("\") If Split(0).Contains("VERAG-NCTS") Then : VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA = "ATILLA" ElseIf Split(0).Contains("VERAG") Then : VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA = "VERAG" ElseIf Split(0).Contains("IMEX") Then : VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA = "IMEX" ElseIf Split(0).Contains("UNISU") Then : VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA = "UNISPED" End If Dim UsrId As Integer = ADMIN.getUstId_ByAD(Split(0), Split(1)) If UsrId > 0 Then Dim MA_TMP As New VERAG_PROG_ALLGEMEIN.cMitarbeiter(UsrId) VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA = If(MA_TMP.mit_firma = "ALL", MA_TMP.mit_FirmaHaupt, MA_TMP.mit_firma) LOGIN_OK = ADMIN.checkLogin(MA_TMP.mit_username, MA_TMP.mit_pwd, VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA) End If End If Catch ex As Exception ' MsgBox(ex.Message & ex.StackTrace) End Try If Not LOGIN_OK Then LOGIN_OK = ADMIN.checkLogin("MAIL_USER", "VERAG_MAIL_USER_2017", VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA) End If Catch ex As Exception MsgBox(ex.Message & ex.StackTrace) End Try Return LOGIN_OK End Function Shared Sub NewAviso_Hauptfenster() VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.clearTMPPath("OutlookAttach") Try Dim explorer As Outlook.Explorer = Globals.ThisAddIn.Application.ActiveExplorer Dim selection As Outlook.Selection = explorer.Selection If selection.Count > 0 Then Dim selectedItem = selection(1) Dim mailItem As Outlook.MailItem = selectedItem If mailItem IsNot Nothing Then NewAviso(mailItem) End If End If Catch ex As Exception MsgBox(ex.Message & ex.StackTrace) End Try End Sub Shared Sub MovedMail() Try Dim explorer As Outlook.Explorer = Globals.ThisAddIn.Application.ActiveExplorer Dim selection As Outlook.Selection = explorer.Selection If selection.Count > 0 Then Dim selectedItem = selection(1) Dim mailItem As Outlook.MailItem = selectedItem If mailItem IsNot Nothing Then MsgBox("Du bist hier angelangt!") End If End If Catch ex As Exception MsgBox(ex.Message & ex.StackTrace) End Try End Sub Shared Function getFirmaFromToMail(ToMail As String) If ToMail.Contains("@verag") Then Return "VERAG" If ToMail.Contains("@imex") Then Return "IMEX" If ToMail.Contains("@unisped") Then Return "UNISPED" If ToMail.Contains("@front-office") Then Return "FRONTOFFICE" Return "" End Function Shared Sub NewAviso(mailItem As Outlook.MailItem) Dim ATTACHMENTS_LIST As New List(Of String) Dim KundenNr = -1 Dim EingangsDatum As Date = CDate("01.01.1990") If mailItem IsNot Nothing Then Try 'MsgBox(mailItem.To) 'MsgBox(mailItem.) 'MsgBox(getFirmaFromToMail(mailItem.To)) EingangsDatum = mailItem.ReceivedTime Dim senderMail = "" If mailItem.SenderEmailType = "EX" Then senderMail = (mailItem.Sender.GetExchangeUser.PrimarySmtpAddress) Else senderMail = (mailItem.SenderEmailAddress) End If KundenNr = VERAG_PROG_ALLGEMEIN.cKundenKontakte.getKdNrFromMailAdress(senderMail, False) ' false, wenn mehrere Niederlassungen von Frimen im KdStamm vorhanden... falsche KdNr... If isMail_EKOL(mailItem) Then If vbYes = MsgBox("EKOL-Anhänge laden?", vbYesNoCancel) Then getATT_EKOL(mailItem, ATTACHMENTS_LIST) End If End If 'End If loadAttachment(mailItem, ATTACHMENTS_LIST) VERAG_PROG_ALLGEMEIN.cGlobal.Aktive_ID = 0 Dim mainForm As New AVISO.frmEintragAviso mainForm.ATT = ATTACHMENTS_LIST If KundenNr > 0 Then AddHandler mainForm.Shown, Sub() mainForm.kdAvisierer.KdNr = KundenNr End Sub End If If EingangsDatum > CDate("01.01.1990") Then mainForm.EingangsDatum = EingangsDatum End If mainForm.TopMost = True mainForm.StartFromOUTLOOK = True mainForm.ShowDialog() ATTACHMENTS_LIST = mainForm.ATT If VERAG_PROG_ALLGEMEIN.cGlobal.Aktive_ID > 0 Then saveAttachment(ATTACHMENTS_LIST, VERAG_PROG_ALLGEMEIN.cGlobal.Aktive_ID, -1, "", "") saveMail(mailItem, VERAG_PROG_ALLGEMEIN.cGlobal.Aktive_ID,, False) End If Catch ex As Exception MsgBox(ex.Message & ex.StackTrace) Finally Try 'If attachments IsNot Nothing Then ' Marshal.ReleaseComObject(attachments) 'End If If mailItem IsNot Nothing Then Marshal.ReleaseComObject(mailItem) End If 'If currInspector IsNot Nothing Then ' Marshal.ReleaseComObject(currInspector) 'End If Catch ex As Exception End Try End Try End If End Sub Shared Sub saveAttachment(ByRef ATTACHMENTS_LIST As List(Of String), AvisoId As Integer, SendungsId As Integer, Art As String, BezeichnungAnh As String) Try If AvisoId > 0 Then If ATTACHMENTS_LIST.Count > 1 Then BezeichnungAnh = "" ' nur wenn 1 Anhang wird die Bezeichnung übernommen. For Each ATT In ATTACHMENTS_LIST Dim fi As New IO.FileInfo(ATT) Dim Bezeichnung = BezeichnungAnh If Bezeichnung = "" Then Bezeichnung = fi.Name Dim typ = "" If AVISO.frmSendungAnhangImport.getFileTypeValid(fi.Extension, typ) Then AVISO.frmSendungAnhangImport.saveToDS(AvisoId, Bezeichnung, ATT, Art, typ, ,,, If(SendungsId > 0, SendungsId, Nothing)) End If Next End If Catch ex As Exception MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ex.Message & ex.StackTrace) End Try End Sub Shared Sub loadAttachment(ByRef mail As Outlook.MailItem, ByRef ATTACHMENTS_LIST As List(Of String)) Dim attachments As Outlook.Attachments = Nothing Try attachments = mail.Attachments For Each attachment As Outlook.Attachment In mail.Attachments If Not isEmbedded(attachment) Then 'Next 'For i As Integer = 1 To attachments.Count ' Dim attachment As Outlook.Attachment = attachments.Item(i) ' MsgBox(attachment.FileName) Dim specialFolder = "OutlookAttach\Outlook_" & Now.ToString("ddMMyy_HHmmss.ffff") While IO.Directory.Exists(VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.TMP_PATH & specialFolder) specialFolder = "OutlookAttach\Outlook_" & Now.ToString("ddMMyy_HHmmss.ffff") End While 'Dim iof As New IO.FileInfo(attachment.PathName) Dim TMP_PATH As String = VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.getTMPPath(attachment.FileName, "", False, False, specialFolder) attachment.SaveAsFile(TMP_PATH) Dim iof As New IO.FileInfo(TMP_PATH) ' If iof.Length > 5000 Then ' > 5 KB ATTACHMENTS_LIST.Add(TMP_PATH) ' End If End If Marshal.ReleaseComObject(attachment) Next Catch ex As Exception MsgBox(ex.Message, ex.StackTrace) Finally If attachments IsNot Nothing Then Marshal.ReleaseComObject(attachments) End If If mail IsNot Nothing Then ' Marshal.ReleaseComObject(mail) End If 'If currInspector IsNot Nothing Then ' Marshal.ReleaseComObject(currInspector) 'End If End Try End Sub Shared Function isEmbedded(attachment As Outlook.Attachment) As Boolean If attachment.Type <> Outlook.OlAttachmentType.olByValue Then Return True End If Dim iTmp As Integer = attachment.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x37050003") If iTmp = 6 Then Return True End If iTmp = attachment.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x37140003") If iTmp = 4 Then Return True End If Return False End Function Shared Function countPDFs() As Integer countPDFs = 0 Try Dim explorer As Outlook.Explorer = Globals.ThisAddIn.Application.ActiveExplorer Dim selection As Outlook.Selection = explorer.Selection If selection.Count > 0 Then Dim selectedItem = selection(1) Dim mailItem As Outlook.MailItem = selectedItem If mailItem IsNot Nothing Then Dim attachments As Outlook.Attachments = mailItem.Attachments For Each attachment As Outlook.Attachment In mailItem.Attachments 'Dim io As New IO.FileInfo(attachment.PathName) If attachment.FileName.EndsWith(".pdf") Then countPDFs += 1 End If 'O.File.Delete(TMP_PATH) Next End If End If Catch ex As Exception MsgBox(ex.Message & ex.StackTrace) End Try End Function Public Shared Function GetTempFilePathWithExtension(ByVal extension As String) As String Dim path = IO.Path.GetTempPath() Dim fileName = Guid.NewGuid().ToString() & extension Return IO.Path.Combine(path, fileName) End Function Shared Function isMail_EKOL(mailItem As Outlook.MailItem) As Boolean Try If mailItem IsNot Nothing Then Dim srch As String = " 0 AndAlso l.Contains(""">") Then ' ENDE Dim link = ("https://web01.ekol.com/documentservice/download/" & l.ToString.Substring(0, l.ToString.IndexOf(""">"))) Dim TMP_PATH = GetTempFilePathWithExtension(".pdf") My.Computer.Network.DownloadFile(link, TMP_PATH) If IO.File.Exists(TMP_PATH) Then ATT.Add(TMP_PATH) End If End If cnt += 1 Next End If LKW_Nr = getEKOL_Var(html, "Position :") RefNr = getEKOL_Var(html, "Truck :") INFO = getEKOL_Var(html, "Route :") Catch ex As Exception MsgBox(ex.Message & ex.StackTrace) End Try Return ATT End Function Shared Function getEKOL_Var(html, varName, Optional splitTo = "
") If html.contains(varName) Then Dim split = html.split(varName) If split(1).ToString.Contains(splitTo) Then Dim split2 = html.split(splitTo) Return split2(0).ToString.Trim End If End If Return "" End Function Shared Sub saveMail(mailItem As Outlook.MailItem, AvisoId As Integer, Optional bezeichnung As String = "", Optional saveAttachments As Boolean = True, Optional SendungsId As Integer = -1, Optional Art As String = "") ' Dim mailItem As Outlook.MailItem = mailItemTMP.Copy Try If AvisoId > 0 Then ' The full path will place the email in the user's temporary folder If bezeichnung = "" Then bezeichnung = Left(Regex.Replace(mailItem.Subject, "[\/\\\:\?\*\<\>\|""]", ""), 100).Replace("""", "").Replace(vbTab, " ") & ".msg" Dim strTmpPath As String = System.IO.Path.GetTempPath() & bezeichnung & If(bezeichnung.EndsWith(".msg"), "", ".msg") ' Save the email to the user's temp folder and convert it to a .MSG 'Dim cnt = 0 'If Not saveAttachments Then ' While mailItem.Attachments.Count > 0 And cnt < 100 ' mailItem.Attachments.Remove(1) : cnt += 1 ' Remove(1) stimmt ' End While 'End If mailItem.SaveAs(strTmpPath, Outlook.OlSaveAsType.olMSG) ' UploadDocument("http://testURL/sites/testing", "Documents", strFileName, btSaveFile) If AVISO.frmSendungAnhangImport.saveToDS(AvisoId, bezeichnung, strTmpPath, Art, "MSG",,,, If(SendungsId, SendungsId, Nothing)) Then ' MsgBox("Anhang wurde hinzugefügt!") End If ' Clean up the temporary .MSG file from the user's temporary folder System.IO.File.Delete(strTmpPath) End If Catch ex As Exception MsgBox(ex.Message & ex.StackTrace) Finally If mailItem IsNot Nothing Then Marshal.ReleaseComObject(mailItem) End If End Try End Sub Shared Function getAvisoId(ByRef Bezeichnung, ByRef pdf_mail, ByRef SendungsId, ByRef Art, ByRef Att, ByVal isFormular) As Integer If (isFormular = True) Then Dim frmAvisoFormularAnfuegen As New frmAvisoFormularAnfuegen(Bezeichnung) If frmAvisoFormularAnfuegen.ShowDialog = DialogResult.OK Then SendungsId = frmAvisoFormularAnfuegen.SendungsId Bezeichnung = frmAvisoFormularAnfuegen.Bezeichnung pdf_mail = frmAvisoFormularAnfuegen.PDF_MAIL Att = frmAvisoFormularAnfuegen.ATT Return 1 Exit Function End If End If Dim frmAvisoAnfügen As New frmAvisoAnfuegen(Bezeichnung) frmAvisoAnfügen.ATT = Att If frmAvisoAnfügen.ShowDialog = DialogResult.OK Then Dim AvisoId = frmAvisoAnfügen.AvisoId SendungsId = frmAvisoAnfügen.SendungsId Bezeichnung = frmAvisoAnfügen.Bezeichnung pdf_mail = frmAvisoAnfügen.PDF_MAIL Art = frmAvisoAnfügen.cboArt._value Att = frmAvisoAnfügen.ATT Return AvisoId End If Return -1 End Function Shared Sub addMailToAviso_Vollmachten() Try Dim explorer As Outlook.Explorer = Globals.ThisAddIn.Application.ActiveExplorer Dim selection As Outlook.Selection = explorer.Selection If selection.Count > 0 Then Dim selectedItem = selection(1) Dim mailItem As Outlook.MailItem = selectedItem mailItem = DirectCast(mailItem, Outlook.MailItem) addMailToAviso(mailItem, True) End If Catch ex As Exception MsgBox(ex.Message & ex.StackTrace) End Try End Sub End Class