Reputation: 31
I've been working on PCOMM-VBA macros for a couple of months now. This issue is mostly happens with every macros I write. Although not every cases when I try to run the macro.
I am quite suspicious that the source of the problem is PCOMM cannot keep up with excel hence some parts of the code will be skipped. However reading a couple of posts make me feel maybe excel have some problems with the loops and activesheet in my code. I am not sure what causes the issue but I hope you can direct me to the answer.
The macro's purpose is to update suppliers' email address. To select the suppliers I've created a template which contains the supplier code and couple additional information along with the email address needs to be uploaded.(4 column with a simple .xls)
So be brief: When I run the code in F8 the code runs through without any errors. When I try to run it in F5 the compiler will skip some lines and make an infinite loop (as the requested data will never appear in the field) or worse case actually update it with the wrong data.
Here is the actual code:
Sub email_upload()
Dim appExcel As Excel.Application
Dim appWb As Excel.Workbook
Dim appWs As Excel.Worksheet
'This part creates a connection between an existing PCOMM session and Excel
Set autECLConnMgrObj = CreateObject("PCOMM.autECLConnMgr")
Set autECLOIAObj = CreateObject("PCOMM.autECLOIA")
Set autECLSessionObj = CreateObject("PCOMM.autECLSession")
Set autECLPSObj = CreateObject("PCOMM.autECLPS")
Set autECLConnList = CreateObject("PCOMM.autECLConnList")
autECLOIAObj.SetConnectionByHandle (autECLConnList(1).Handle)
autECLPSObj.SetConnectionByHandle (autECLConnList(1).Handle)
autECLConnList.Refresh
'Opens the template where the supplier codes are in (normal .xls)
With Application.FileDialog(1)
.Title = "Please select a file to import!"
.AllowMultiSelect = False
If .Show <> -1 Then
Exit Sub
End If
FileSelected = .SelectedItems(1)
End With
fileName = FileSelected
Set appExcel = Excel.Application
Set appWb = appExcel.Workbooks.Open(fileName:=fileName)
Set appWs = appWb.Worksheets("input")
appExcel.Visible = True
appWb.Worksheets("input").Activate
With appWs
autECLOIAObj.WaitForAppAvailable
'Loop thru every row in the template worksheet
For i = 2 To Cells(Rows.Count, "A").End(xlUp).Row Step 1
'Start system navigation in AS/400
autECLPSObj.SetCursorPos 6, 23
autECLPSObj.SendKeys Cells(i, "A")
autECLPSObj.WaitForAttrib 6, 23, "00", "3c", 3, 10000
autECLOIAObj.WaitForInputReady
autECLOIAObj.WaitForAppAvailable
autECLPSObj.SendKeys "[enter]"
autECLOIAObj.WaitForInputReady
autECLOIAObj.WaitForAppAvailable
autECLPSObj.SendKeys "[pf13]"
autECLOIAObj.WaitForInputReady
autECLOIAObj.WaitForAppAvailable
autECLPSObj.SendKeys "[pf10]"
autECLOIAObj.WaitForInputReady
autECLOIAObj.WaitForAppAvailable
autECLPSObj.SetCursorPos 13, 24
autECLPSObj.SendKeys Cells(i, "B")
autECLPSObj.WaitForAttrib 13, 24, "00", "3c", 3, 10000
autECLOIAObj.WaitForInputReady
autECLOIAObj.WaitForAppAvailable
autECLPSObj.SendKeys "[enter]"
autECLOIAObj.WaitForInputReady
autECLOIAObj.WaitForAppAvailable
autECLPSObj.SendKeys "[pf8]"
autECLOIAObj.WaitForInputReady
autECLOIAObj.WaitForAppAvailable
If Cells(i, "C") = "" Then
autECLPSObj.SetCursorPos 13, 21
autECLPSObj.SendKeys "1"
autECLPSObj.WaitForAttrib 13, 21, "00", "3c", 3, 10000
autECLOIAObj.WaitForInputReady
autECLOIAObj.WaitForAppAvailable
Else
autECLPSObj.SetCursorPos 13, 21
autECLPSObj.SendKeys Cells(i, "C")
autECLPSObj.WaitForAttrib 13, 21, "00", "3c", 3, 10000
autECLOIAObj.WaitForInputReady
autECLOIAObj.WaitForAppAvailable
End If
autECLPSObj.SetCursorPos 21, 1
autECLPSObj.SendKeys Cells(i, "D")
autECLPSObj.WaitForAttrib 21, 1, "00", "3c", 3, 10000
autECLOIAObj.WaitForInputReady
autECLOIAObj.WaitForAppAvailable
autECLPSObj.SendKeys "[pf8]"
autECLOIAObj.WaitForInputReady
autECLOIAObj.WaitForAppAvailable
autECLPSObj.SendKeys "[pf12]"
autECLOIAObj.WaitForInputReady
autECLOIAObj.WaitForAppAvailable
autECLPSObj.SendKeys "[pf12]"
autECLOIAObj.WaitForInputReady
autECLOIAObj.WaitForAppAvailable
Next i
End With
appWb.Close
MsgBox ("Input done!")
End Sub
Upvotes: 0
Views: 1164