Hi folks,
i'm new to the forum and need some help regarding VBA Lotus automation. All the topics i've seen and read (also on other forums) have helped me generate my emails (without sending) but i seem to have a problem that happens sometimes.
What i want : have my code loop through a file which determines if an email needs to be generated and generate as many emails as required.
Problem : so far i've successfully managed to generate my emails with incorporated signature, text and copypicture from required parts of the excel file. The problem is that sometimes it works flawlessly and sometimes it doesn't (can't figure out why), i.e. all emails are created with the maildb.createdocument lign but the content of all emails is gathered in the first email. So i end up with for instance 7 emails with the first one containing 7 bodies (with the right individual content) and 6 blank emails. I'm guessing it has sometimes to do with the focus but can't figure out what exactly. Is it a variable problem ? Lotus focus problem ?
Thanks in advance,
J.
P.S. I'm french, forgive my english. Also : I have never taken VBA classes and had to learn for myself, therefore what I do works but is very messy. I have just started reading a VBA book for tips & methods.
Code :
i'm new to the forum and need some help regarding VBA Lotus automation. All the topics i've seen and read (also on other forums) have helped me generate my emails (without sending) but i seem to have a problem that happens sometimes.
What i want : have my code loop through a file which determines if an email needs to be generated and generate as many emails as required.
Problem : so far i've successfully managed to generate my emails with incorporated signature, text and copypicture from required parts of the excel file. The problem is that sometimes it works flawlessly and sometimes it doesn't (can't figure out why), i.e. all emails are created with the maildb.createdocument lign but the content of all emails is gathered in the first email. So i end up with for instance 7 emails with the first one containing 7 bodies (with the right individual content) and 6 blank emails. I'm guessing it has sometimes to do with the focus but can't figure out what exactly. Is it a variable problem ? Lotus focus problem ?
Thanks in advance,
J.
P.S. I'm french, forgive my english. Also : I have never taken VBA classes and had to learn for myself, therefore what I do works but is very messy. I have just started reading a VBA book for tips & methods.
Code :
Code:
Sub inquiry()
Dim wbs As Workbook
Dim wbm As Workbook
Dim wbsupp As Workbook
Dim wsssl As Object, wsmasteran As Object, wssupp As Object
Dim ss As String
Dim Maildb As Object
Dim UserName As String
Dim MailDbName As String
Dim MailDoc As Object
Dim Session As Object
Dim Subject1 As String
Dim ccRecipient As String
ss = Workbooks("PC Tool.xlsm").Sheets("Anfragen").Range("B3").Value & Workbooks("PC Tool.xlsm").Sheets("Anfragen").Range("B4").Value
ll = Workbooks("PC Tool.xlsm").Sheets("Anfragen").Range("B7").Value & Workbooks("PC Tool.xlsm").Sheets("Anfragen").Range("B8").Value
Set wbm = ThisWorkbook
'open SSL and Supplier workbooks
On Error Resume Next
testifopen = Workbooks(ss).Sheets("SSL ").Range("A1").Value
If Err > 0 Then
Workbooks.Open Filename:=Workbooks("PC Tool.xlsm").Sheets("Anfragen").Range("B2").Value & "\" & ss, ReadOnly:=True
Err = 0
Else
End If
On Error Resume Next
testifopen = Workbooks(ll).Sheets("Actual HB Suppliers").Range("A1").Value
If Err > 0 Then Workbooks.Open Filename:=Workbooks("PC Tool.xlsm").Sheets("Anfragen").Range("B6").Value & "\" & ll, ReadOnly:=True
Set wbs = Workbooks(ss)
Set wbsupp = Workbooks(ll)
Set wsmasteran = wbm.Sheets("Anfragen")
Set wsssl = wbs.Sheets("SSL ")
Set wssupp = wbsupp.Sheets("Actual HB Suppliers")
Dim inwork As Object
Dim workspace As Object
'sort supplier list to prep
wssupp.Range("table").Sort key1:=Range("A4"), order1:=xlAscending
For i = 1 To Application.WorksheetFunction.CountA(wsmasteran.Range("Q:Q")) - 1
If wsmasteran.Range("O1").Value = "Automatic" Then
If wsmasteran.Cells(1 + i, 17).Value = "Floor" Or wsmasteran.Cells(1 + i, 17).Value = "Staircase" Then
head = wsssl.Range("A:E").Find(wsmasteran.Cells(1 + i, 17).Value, LookAt:=xlWhole).Row
ligne = wsssl.Range("A:E").Find(wsmasteran.Cells(1 + i, 17).Value, LookAt:=xlWhole).Row + 1
Else
head = wsssl.Range("A:E").Find(wsmasteran.Cells(1 + i, 17).Value, LookAt:=xlPart).Row + 1
ligne = wsssl.Range("A:E").Find(wsmasteran.Cells(1 + i, 17).Value, LookAt:=xlPart).Row + 2
End If
Else
If wsmasteran.Cells(1 + i, 17).Value = "Floor" Or wsmasteran.Cells(1 + i, 17).Value = "Staircase" Then
head = wsmasteran.Cells(1 + i, 19).Value
ligne = wsmasteran.Cells(1 + i, 19).Value + 1
Else
head = wsmasteran.Cells(1 + i, 19).Value + 1
ligne = wsmasteran.Cells(1 + i, 19).Value + 2
End If
End If
If Len(wsssl.Cells(ligne, 1).Value) > 0 Then
cremail:
If IsError(Application.WorksheetFunction.VLookup(wsssl.Cells(ligne, 10).Value, wssupp.Range("table"), 4, False)) = True Then
Recipient = wssupp.Cells(Application.WorksheetFunction.Match(wsssl.Cells(ligne, 10).Value, wssupp.Range("A4:A500"), 1) + 4, 4).Value
nom = wssupp.Cells(Application.WorksheetFunction.Match(wsssl.Cells(ligne, 10).Value, wssupp.Range("A4:A500"), 1) + 4, 6).Value
english = wssupp.Cells(Application.WorksheetFunction.Match(wsssl.Cells(ligne, 10).Value, wssupp.Range("A4:A500"), 1) + 4, _
wssupp.Range("A3:AZ3").Find("Vertragszusatz").Column).Value
Else
Recipient = wssupp.Cells(Application.WorksheetFunction.Match(wsssl.Cells(ligne, 10).Value, wssupp.Range("A4:A500"), 0) + 3, 4).Value
nom = wssupp.Cells(Application.WorksheetFunction.Match(wsssl.Cells(ligne, 10).Value, wssupp.Range("A4:A500"), 0) + 3, 6).Value
english = wssupp.Cells(Application.WorksheetFunction.Match(wsssl.Cells(ligne, 10).Value, wssupp.Range("A4:A500"), 0) + 3, _
wssupp.Range("A3:AZ3").Find("Vertragszusatz").Column).Value
End If
proj = wsmasteran.Range("B10").Value
'copy picture to paste later
picligne = ligne
pic:
If Len(wsssl.Cells(picligne + 1, 10).Value) > 0 Then
If wsssl.Cells(picligne + 1, 10).Value = wsssl.Cells(picligne, 10).Value Then
If wsssl.Cells(picligne + 1, 1).Value > 0 Then
picligne = picligne + 1
GoTo pic
End If
End If
End If
wsssl.Range(wsssl.Cells(ligne, 1), wsssl.Cells(picligne, 9)).CopyPicture xlScreen, xlBitmap
'lotus part starts here
Set Session = CreateObject("Notes.NotesSession")
UserName = Session.UserName
MailDbName = Left$(UserName, 1) & Right$(UserName, (Len(UserName) - InStr(1, UserName, " "))) & ".nsf"
Set Maildb = Session.GETDATABASE("", MailDbName)
If Maildb.IsOpen = True Then
Else: Maildb.OPENMAIL
End If
Set MailDoc = Maildb.createdocument
MailDoc.Form = "Memo"
MailDoc.Sendto = Recipient
If InStr(1, english, "AUF ENGLISCH") Then
Subject1 = proj & "-Inquiry " & wsmasteran.Cells(1 + i, 17).Value
body_text = "Dear " & nom & "," & Chr(10) & Chr(10) & "please submit me an offer for the above mentioned project as follows :" & Chr(10) & Chr(10)
Else
Subject1 = proj & "-Anfrage " & wsmasteran.Cells(1 + i, 18).Value
body_text = "Hallo " & nom & "," & Chr(10) & Chr(10) & "bitte erstellen Sie mir für das o.g. Projekt ein Angebot wie folgt :" & Chr(10) & Chr(10)
End If
MailDoc.Subject = Subject1
Set workspace = CreateObject("Notes.NotesUIWorkspace")
' Call workspace.EDITDocument(True, MailDoc).fieldsettext("body", body_text & Signature)
'paste picture
' Call workspace.EDITDocument(True, MailDoc)
Set inwork = workspace.EDITDocument(True, MailDoc)
inwork.GOTOFIELD ("Body")
inwork.Paste
'paste headers
wsssl.Range(wsssl.Cells(head, 1), wsssl.Cells(head, 9)).CopyPicture xlScreen, xlBitmap
inwork.GOTOFIELD ("Body")
inwork.Paste
'add text
inwork.GOTOFIELD ("Body")
inwork.inserttext (body_text)
Set workspace = Nothing
Set inwork = Nothing
Set MailDoc = Nothing
Set Session = Nothing
Set Maildb = Nothing
'create another mail if multiple suppliers
recheck:
If Len(wsssl.Cells(ligne + 1, 10).Value) > 0 Then
If wsssl.Cells(ligne + 1, 1).Value > 0 Then
If wsssl.Cells(ligne + 1, 10).Value = wsssl.Cells(ligne, 10).Value Then
ligne = ligne + 1
GoTo recheck
Else
ligne = ligne + 1
GoTo cremail
End If
End If
End If
Else
End If
Next i
'close workbooks
Application.DisplayAlerts = False
wbsupp.Close
wbs.Close
Application.DisplayAlerts = True
'get focus on lotus (problem if brower is open with lotus name in tab name)
Dim wd As Object
Dim tsk As Object
Set wd = CreateObject("word.application")
For Each tsk In wd.Tasks
If InStr(tsk.Name, "Lotus") Then
tsk.Activate
tsk.WindowState = wdWindowStateMaximize
End
End If
Next
wd.Quit
Set wd = Nothing
End Sub