I am basically trying to create a report for my larger scale macro. The report will show each mail processsed and what attachments are found within the email.
I have the following code working, but does not supply me with the correct results for the .csv files only. Can anyone see any problems that I cannot?
If .Attachments.Count = 0 Then
csv_report = "NO"
pdf_report = "NO"
xls_report = "NO"
End If
If .Attachments.Count > 0 Then
For i2 = 1 To .Attachments.Count
If LCase(Right(.Attachments(i2).Filename, 4)) = ".csv" Then
csv_report = "YES"
Else
csv_report = "NO"
End If
If LCase(Right(.Attachments(i2).Filename, 4)) = ".pdf" Then
pdf_report = "YES"
Else
pdf_report = "NO"
End If
If LCase(Right(.Attachments(i2).Filename, 4)) = ".xls" Or LCase(Right(.Attachments(i2).Filename, 5)) = ".xlsx" Then
xls_report = "YES"
Else
xls_report = "NO"
End If
Next
End If
Sheets("Mail Report").Activate
Range("C65000").End(xlUp).Offset(1).Value = csv_report
Range("D65000").End(xlUp).Offset(1).Value = pdf_report
Range("E65000").End(xlUp).Offset(1).Value = xls_report
subject_line = mail.Subject
Range("A65000").End(xlUp).Offset(1).Value = subject_line
So by just adding 'GoTo' function I was able to answer my own query. Thank you all for leaving comments
If .Attachments.Count = 0 Then
csv_report = "NO"
pdf_report = "NO"
xls_report = "NO"
End If
If .Attachments.Count > 0 Then
For i2 = 1 To .Attachments.Count
If LCase(Right(.Attachments(i2).Filename, 4)) = ".csv" Then
csv_report = "YES"
GoTo CSVyes 'if a .csv file is found, it skips to the PDF attachment checker
Else
csv_report = "NO"
End If
Next
CSVyes:
For i2 = 1 To .Attachments.Count
If LCase(Right(.Attachments(i2).Filename, 4)) = ".pdf" Then
pdf_report = "YES"
GoTo PDFyes 'if a .pdf file is found, it skips to the XLS attachment checker
Else
pdf_report = "NO"
End If
Next
PDFyes:
For i2 = 1 To .Attachments.Count
If LCase(Right(.Attachments(i2).Filename, 4)) = ".xls" Or LCase(Right(.Attachments(i2).Filename, 5)) = ".xlsx" Or UCase(Right(.Attachments(i2).Filename, 4)) = ".XLS" Then
xls_report = "YES"
GoTo XLSyes 'if a .xls file is found, it skips to the end of the checks
Else
xls_report = "NO"
End If
Next
XLSyes:
End If
Sheets("Mail Report").Activate
Range("C65000").End(xlUp).Offset(1).Value = csv_report
Range("D65000").End(xlUp).Offset(1).Value = pdf_report
Range("E65000").End(xlUp).Offset(1).Value = xls_report
subject_line = mail.Subject
Range("A65000").End(xlUp).Offset(1).Value = subject_line
Related
I've got a problem with a VBA SAP script. I have data in excel and I want to execute the transaction code IW41 by copying and pasting data from Excel. I have all of the data like dates, number of orders, who did it etc in Excel and I want to automate it. I did a loop for variable i but I get an error and I cannot fix it.
Error :
Run-time error '619': Application-defined or object-defined error
Code provided below.
Can you give me some pro tips or help me fix it?
Screen in IW41 where the error occurs:
Code :
Sub ConfirmPM_Nots()
SystemName = "CCP" 'change as needed or use a variable
Transaction = "SESSION_MANAGER" 'change as needed or use a variable
On Error GoTo ErrorHandler:
If Not IsObject(Sap_Applic) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set Sap_Applic = SapGuiAuto.GetScriptingEngine
End If
On Error GoTo 0
koniec:
qConnections = Sap_Applic.Connections.Count
If qConnections = 0 Then
MsgBox "No connection to SAP"
End
End If
bSession = False
For iConnectionCounter = 0 To qConnections - 1
Set Connection = Sap_Applic.Children(Int(iConnectionCounter))
If Not Connection.Description = "" Then
qSessions = Connection.Children.Count
For iSessionCounter = 0 To qSessions - 1
Set session = Connection.Children(Int(iSessionCounter))
If session.info.SystemName <> SystemName Then Exit For
If session.info.Transaction = Transaction Then
bSession = True
Exit For
End If
Next
End If
If bSession Then Exit For
Next
If Not bSession Then
MsgBox SystemName & " not available or free session not available"
End
End If
Do
i = 1
session.findById("wnd[0]").resizeWorkingPane 128, 37, False
session.findById("wnd[0]/tbar[0]/okcd").Text = "iw41"
session.findById("wnd[0]").sendVKey 0
Order = Cells(i, 1)
b = Cells(i, 2)
c = Cells(i, 3)
d = Cells(i, 4)
session.findById("wnd[0]/usr/ctxtCORUF-AUFNR").Text = Order
session.findById("wnd[0]/usr/ctxtCORUF-AUFNR").caretPosition = 7
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/tblSAPLCORUTC_3100/txtAFVGD-VORNR[1,0]").SetFocus
session.findById("wnd[0]/usr/tblSAPLCORUTC_3100/txtAFVGD-VORNR[1,0]").caretPosition = 2
session.findById("wnd[0]").sendVKey 2
session.findById("wnd[0]/usr/chkAFRUD-AUERU").Selected = True
session.findById("wnd[0]/usr/chkAFRUD-LEKNW").Selected = True
session.findById("wnd[0]/usr/ctxtAFRUD-ISDD").Text = c
session.findById("wnd[0]/usr/txtAFRUD-IDAUR").Text = b
session.findById("wnd[0]/usr/ctxtAFRUD-IEDD").Text = c
session.findById("wnd[0]/usr/txtAFRUD-LTXA1").Text = d
session.findById("wnd[0]/usr/txtAFRUD-LTXA1").SetFocus
session.findById("wnd[0]/usr/txtAFRUD-LTXA1").caretPosition = 10
session.findById("wnd[0]/tbar[0]/btn[11]").press
i = i + 1
Loop
Exit Sub
ErrorHandler:
MsgBox "No connection to SAP"
End
End Sub
"iw41" (from session.findById("wnd[0]/tbar[0]/okcd").Text = "iw41") only works if the current screen is the start menu. OK-Code "/niw41" will always work! So this code might work (untested):
Do
i = 1
' session.findById("wnd[0]").resizeWorkingPane 128, 37, False
' iw41 only works in the start menu. OK-Code /niw41 will always work!
session.findById("wnd[0]/tbar[0]/okcd").Text = "/niw41"
session.findById("wnd[0]").sendVKey 0
Order = Cells(i, 1)
b = Cells(i, 2).value
c = Cells(i, 3).value
d = Cells(i, 4).value
session.findById("wnd[0]/usr/ctxtCORUF-AUFNR").Text = Order
' session.findById("wnd[0]/usr/ctxtCORUF-AUFNR").caretPosition = 7
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/tblSAPLCORUTC_3100/txtAFVGD-VORNR[1,0]").SetFocus
' session.findById("wnd[0]/usr/tblSAPLCORUTC_3100/txtAFVGD-VORNR[1,0]").caretPosition = 2
session.findById("wnd[0]").sendVKey 2
session.findById("wnd[0]/usr/chkAFRUD-AUERU").Selected = True
session.findById("wnd[0]/usr/chkAFRUD-LEKNW").Selected = True
session.findById("wnd[0]/usr/ctxtAFRUD-ISDD").Text = c
session.findById("wnd[0]/usr/txtAFRUD-IDAUR").Text = b
session.findById("wnd[0]/usr/ctxtAFRUD-IEDD").Text = c
session.findById("wnd[0]/usr/txtAFRUD-LTXA1").Text = d
' session.findById("wnd[0]/usr/txtAFRUD-LTXA1").SetFocus
' session.findById("wnd[0]/usr/txtAFRUD-LTXA1").caretPosition = 10
session.findById("wnd[0]/tbar[0]/btn[11]").press
i = i + 1
Loop
I also commented the lines with resizeWorkingPane, caretPosition and SetFocus because this is usually not needed. resizeWorkingPane will resize the SAPGUI screen and caretPosition is the position of a cursor within a textbox. Sometimes needed if you want to replace text for example. But in this case certainly not.
NOTE: Your need to exit our Do Loop and place the record pointer i out of the loop. Otherwise i = 1. To exit the loop I often use first blank cell value.
This way you can set the first record to start with, and in most cases i=2 as most sheets have used first row as headers.
i = 1
Do Until Cells(i, 1) = ""
' code
i = i + 1
Loop
When I debugged the code to use it in my own update of Equipment in SAP, I found that our SAP does not have Connection.Description so I just pick up the first session like this.
This worked just fine for me:
Sub SetEQLocations()
' Script written by Svein Aren Hylland 02.12.2022
' Use of VBScript recording from SAP to work with SAP transaction IE02 - Change Equipment.
' The sub will transfere new Location data found in sheet on all visible rows with filter and headers in first row.
' Code will show progress in first column while it updates each EQ in SAP.
'
Sv = MsgBox("This routine work towards SAP IE02 from row 2 - and will update all EQ locations as shown in this sheet.", vbOKCancel)
If Sv = vbCancel Then Exit Sub
SystemName = "KO3" 'change as needed or use a variable
Transaction = "SESSION_MANAGER" 'change as needed or use a variable
On Error GoTo ErrorHandler:
If Not IsObject(Sap_Applic) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set Sap_Applic = SapGuiAuto.GetScriptingEngine
End If
On Error GoTo 0
koniec:
qConnections = Sap_Applic.Connections.Count
If qConnections = 0 Then
MsgBox "No connection to SAP"
End
End If
'MsgBox Sap_Applic.Children(0).info.SystemName
bSession = False
For iConnectionCounter = 0 To qConnections - 1
Set Connection = Sap_Applic.Children(Int(iConnectionCounter))
'MsgBox Connection.Description
'If Not Connection.Description = "" Then
qSessions = Connection.Children.Count
For iSessionCounter = 0 To qSessions - 1
Set Session = Connection.Children(Int(iSessionCounter))
'MsgBox Session.info.SystemName
If Session.info.SystemName <> SystemName Then Exit For
If Session.info.Transaction = Transaction Then
bSession = True
Exit For
End If
Next
'End If
If bSession Then Exit For
Next
If Not bSession Then
MsgBox SystemName & " not available or free session not available"
End
End If
'Stop
Session.findById("wnd[0]").resizeWorkingPane 154, 24, False
Session.findById("wnd[0]/tbar[0]/okcd").Text = "ie02"
Session.findById("wnd[0]").sendVKey 0
i = 2
Do Until Cells(i, 1) = ""
If Cells(i, 1).Rows.Hidden = False Then
EQ = Cells(i, 1)
' Display progress
Cells(i, 1).Select
Cells(i, 1).Interior.Color = vbYellow
' Get data from sheet to be poulated in SAP fields
CostCenter = Cells(i, 14) ' Organization/Cost Center
MainWC = Cells(i, 15) ' Organization/Main Work Center
LocWorkCenter = Cells(i, 16) ' Location/Work Center
LocRoom = Cells(i, 17) ' Location/Room
Session.findById("wnd[0]/usr/ctxtRM63E-EQUNR").Text = EQ
Session.findById("wnd[0]/tbar[0]/btn[0]").press
Session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\02").Select
Session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\02/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102A:SAPLITO0:1050/txtITOB-MSGRP").Text = LocRoom
Session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\02/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102A:SAPLITO0:1050/txtITOB-MSGRP").SetFocus
Session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\02/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102A:SAPLITO0:1050/txtITOB-MSGRP").caretPosition = 6
Session.findById("wnd[0]").sendVKey 0
Session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\02/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102A:SAPLITO0:1050/ctxtITOBATTR-ARBPL").Text = LocWorkCenter
Session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\02/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102A:SAPLITO0:1050/ctxtITOBATTR-ARBPL").SetFocus
Session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\02/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102A:SAPLITO0:1050/ctxtITOBATTR-ARBPL").caretPosition = 6
Session.findById("wnd[0]").sendVKey 0
Session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\03").Select
Session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\03/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102A:SAPLITO0:1052/ctxtITOB-KOSTL").Text = CostCenter
Session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\03/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102B:SAPLITO0:1062/ctxtITOBATTR-GEWRK").Text = MainWC
Session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\03/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102B:SAPLITO0:1062/ctxtITOBATTR-GEWRK").SetFocus
Session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\03/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102B:SAPLITO0:1062/ctxtITOBATTR-GEWRK").caretPosition = 6
Session.findById("wnd[0]").sendVKey 0
Session.findById("wnd[0]/tbar[0]/btn[11]").press
End If
i = i + 1
Loop
Exit Sub
ErrorHandler:
MsgBox "No connection to SAP"
End
End Sub
I've this macro in an UserForm to register accounting operations, after I finish the registration and I click the "x" button in the Userform my excel workbook close without asking me. I know this piece of code is the problem because my Userform do other things and these other things don't cause this problem.
Confirmar = MsgBox("¿Desea registrar la nueva operación contable?", vbYesNo)
If Confirmar = vbYes Then
Next_LibroDiario = WShe_LibroDiario.Cells(Rows.Count, 2).End(xlUp).Row + 1
If APP_RegistroContable.OptionButton_Débito = True Then
WShe_LibroDiario.Cells(Next_LibroDiario, 7) = APP_RegistroContable.Monto + 0
APP_RegistroContable.Monto = ""
ElseIf APP_RegistroContable.OptionButton_Crédito = True Then
WShe_LibroDiario.Cells(Next_LibroDiario, 8) = APP_RegistroContable.Monto + 0
APP_RegistroContable.Monto = ""
ElseIf APP_RegistroContable.OptionButton_Débito = False _
And APP_RegistroContable.OptionButton_Crédito = False _
Then
MsgBox "Please select an accounting item"
Exit Sub
End If
WShe_LibroDiario.Cells(Next_LibroDiario, 2) = APP_RegistroContable.Ctas_Bancarias
APP_RegistroContable.Ctas_Bancarias = ""
WShe_LibroDiario.Cells(Next_LibroDiario, 3) = CDate(APP_RegistroContable.Fecha)
APP_RegistroContable.Fecha = ""
WShe_LibroDiario.Cells(Next_LibroDiario, 4) = APP_RegistroContable.Recibo_CF
APP_RegistroContable.Recibo_CF = ""
WShe_LibroDiario.Cells(Next_LibroDiario, 5) = APP_RegistroContable.Nombre
APP_RegistroContable.Nombre = ""
WShe_LibroDiario.Cells(Next_LibroDiario, 6) = APP_RegistroContable.Auxiliar + 0
WShe_LibroDiario.Cells(Next_LibroDiario, 9) = APP_RegistroContable.Clasificación
APP_RegistroContable.Clasificación = ""
WShe_LibroDiario.Cells(Next_LibroDiario, 10) = APP_RegistroContable.Comentario
APP_RegistroContable.Comentario = ""
' This part creates an ID for the accounting operation using the date registered and the number of
' operations registered in that date
Last_ID = WShe_LibroDiario.Cells(Rows.Count, 2).End(xlUp).Row
Set Rang_Fecha = WShe_LibroDiario.Range("C8:C" & Last_ID)
Set Rang_ID = WShe_LibroDiario.Cells(Last_ID, 3)
Inte_IDGenerator = WorksheetFunction.CountIf(Rang_Fecha, Rang_ID)
WShe_LibroDiario.Cells(Last_ID, 1).Value = WShe_LibroDiario.Cells(Last_ID, 3).Value & "-0" & _
Inte_IDGenerator
MsgBox "The accounting operation is now in the system"
End if
Compiler Error: End With Has No With. I know that this is wrong, and there is something in my code in where I am not calling it correctly that is making it mess up but I cannot find it. I'm just trying to grab information off of my sheet1 so that I can use it later on.
With ThisWorkbook.Sheets("Sheet1")
While (Counter <= 300)
Pcounter = .Cells(ACBoxCounter, 2)
If (Pcounter <> "") Then
ACounter = ACounter + 1
End If
ACBCounter = ACBCounter + 30
Wend
While (OverallACounter < ACounter)
Set objStream = CreateObject("ADODB.Stream")
objStream.Charset = "iso-8859-1"
objStream.Open
ExampleString = .Cells(Row2Counter + 22, 3)
ChooseM = Split(ExampleString, "-")(1)
If (ChooseM = "8")
M = "II"
P = 97
Label = .Cells(Row2Counter, 2)
ElseIf (ChooseM = "13") Then
Model = "A II"
P = 10
Label = "A6_" & .Cells(Row2Counter, 2)
ElseIf (ChooseM = "19") Then
M = "AC1I"
P = 56
Label = "A9_" & .Cells(Row2Counter, 2)
End If
OverallD = 0
Overall= 0
OverallB = 0
ChooseBoxType = Split(ExampleString, "-")(2)
If ((StrComp(ChooseB, "1") = 0) Or (StrComp(ChooseB, "1M") = 0)) Then
BoxInputT= "1 Phase"
ElseIf ((StrComp(ChooseB, "2") = 0) Or (StrComp(ChooseB, "2M") = 0)) Then
BoxInput= "2"
ElseIf ((StrComp(ChooseB ,"3") = 0) Or (StrComp(ChooseBo, "3M") = 0)) Then
BoxInput= "3"
End If
objStream.WriteText (" <" & .Cells(Row2Counter, 2).Text & ">" & vbLf)
Wend
End With
Compiler Error: End With Has No With
I want compute the sum of a column by VBA.
This following my code:
Sub CALRU()
ECP_CA = 0
Radome_CA = 0
For i = 1 To 21726
If Cells(i, "L") = "GET" Then
If Cells(i, "H") = "2014" Then
ECP_CA = ECP_CA + Cells(i, "J")
Else
MsgBox "not found"
End If
Else
MsgBox "not found"
End If
Next i
End Sub
My loop when it find the first result is true it stoped.And do not termine the rest of column.
Someone, can give me a suggestion where's the problem ?
Thank you.
If your code stops, you must click the OK button in the MsgBox for the macro to continue.
EDIT#1:
This may help solve your problem:
Sub CALRU()
ECP_CA = 0
Radome_CA = 0
For i = 1 To 21726
If Cells(i, "L") = "GET" Then
If Cells(i, "H") = "2014" Then
ECP_CA = ECP_CA + ReturnNumber(Cells(i, "J"))
Else
MsgBox "not found"
End If
Else
MsgBox "not found"
End If
Next i
End Sub
Public Function ReturnNumber(v As Variant) As Double
Dim L As Long, temp As String, CH As String
L = Len(v)
If L = 0 Then
ReturnNumber = 0
Exit Function
End If
temp = ""
For i = 1 To L
CH = Mid(v, i, 1)
If CH Like "[0-9]" Or CH = "." Or CH = "-" Then temp = temp & CH
Next i
If temp = "" Then
ReturnNumber = 0
Else
ReturnNumber = CDbl(temp)
End If
End Function
It should work now:
Option Explicit
Sub CALRU()
Dim ecp_ca As Double
Dim Radome_CA As Double
Dim i As Long
ecp_ca = 0
Radome_CA = 0
With ActiveSheet
For i = 1 To 217
If .Cells(i, "L") = "GET" Then
If .Cells(i, "H") = "2014" Then
ecp_ca = ecp_ca + .Cells(i, "J")
Else
Debug.Print "not found"
End If
Else
Debug.Print "not found"
End If
Next i
End With
End Sub
I have tried up to 217, because I didn't want to wait to print for 21K. I have changed the MsgBoxes to Debug.Print and I have added With ActiveSheet, as far as this can be a problem as well. Option Explicit is also added.
I want to fetch all the Outlook inbox emails into an Excel sheet with additional columns having the data like This mail was replied on or This mail was forwarded to
Here is the code that I have done so far
Dim Folder As Outlook.MAPIFolder
Dim iRow As Integer
MailBoxName = 'Mailbox Name Goes Here
Pst_Folder_Name = "Inbox"
Set Folder = Outlook.Session.PickFolder 'Folders(MailBoxName).Folders(Pst_Folder_Name)
If Folder = "" Then
MsgBox "Invalid Data in Input"
GoTo end_lbl1:
End If
Folder.Items.Sort "[ReceivedTime]", False
LimitDateTimeValue = 'Date Limit
CellNo = 2
For iRow = 1 To Folder.Items.Count
On Error Resume Next
If Folder.Items.Item(iRow).ReceivedTime > LimitDateTimeValue Then
'CellNo = 2
On Error Resume Next
ThisWorkbook.Sheets("Inbox").Range("A2").Select
FullSubjectLine = Folder.Items.Item(iRow).Subject
If InStr(1, FullSubjectLine, "FE:", vbTextCompare) > 0 Or InStr(1, FullSubjectLine, "FW:", vbTextCompare) > 0 Or InStr(1, FullSubjectLine, "RE:", vbTextCompare) Then
FilteredSubjectLine = Mid(FullSubjectLine, 5)
ThisWorkbook.Sheets("Inbox").Cells(CellNo, 2) = FilteredSubjectLine
Else
ThisWorkbook.Sheets("Inbox").Cells(CellNo, 2) = Folder.Items.Item(iRow).Subject
End If
ThisWorkbook.Sheets("Inbox").Cells(CellNo, 4) = Left(Folder.Items.Item(iRow).Body, 1024)
If Folder.Items.Item(iRow).UnRead Then
ThisWorkbook.Sheets("Inbox").Cells(CellNo, 6) = "UnRead"
Else
ThisWorkbook.Sheets("Inbox").Cells(CellNo, 6) = "Read"
End If
ThisWorkbook.Sheets("Inbox").Cells(CellNo, 1) = Folder.Items.Item(iRow).SenderName
ThisWorkbook.Sheets("Inbox").Cells(CellNo, 3) = Folder.Items.Item(iRow).ReceivedTime
CellNo = CellNo + 1
End If
Next iRow
The code is extremely inefficient, this is multiple dot notation taken to its extreme. Cache the Items collection before entering the loop and retrieve the item only once on each iteration - otherwise OOM will have to return a brand new COM object for each ".".
On Error Resume Next
set vItems = Folder.Items
For iRow = 1 To vItems.Count
set vItem = vItems.Item(iRow)
FullSubjectLine = vItem.Subject
lastVerbExecuted = vItem.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x10810003")
if Err.Number <> 0 Then
lastVerbExecuted = 0
Err.Clear
End If
...
next