Я создаю python сценарии для запуска файла макроса Excel. Он отправляет подтверждение по электронной почте после завершения работы. Но при отправке электронной почты он отправляет двойную электронную почту. Это мой вывод электронной почты.
Это мой python скрипт для командного файла VBA Excel.:
import time
import threading
import zmq
import win32com.client
import os, sys
import pyautogui as py
context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind('tcp://*:5555')
print("Manager Server port 5555 Activate!")
while True:
message = socket.recv()
print('Received request: {}'.format(message))
path = "R:/veerachai/Invoice_form/Invoice_generate/"+str(message)+"/"+str(message)+".xlsm"
isExist = os.path.exists(path)
print(isExist)
if os.path.exists(path):
time.sleep(5)
py.click(770,445)
xl=win32com.client.Dispatch("Excel.Application")
xl.Workbooks.Open(os.path.abspath(path))
xl.Application.Run("Module6.manager()")
#xl.Application.Quit()
del xl
time.sleep(2)
py.click(599,392)
time.sleep(2)
socket.send_json({ 'status': 'ok' })
print('Reply')
Это мой код VBA:
Sub manager()
Dim rng As Range
Dim OutApp As Object
Dim OutMail_User As Object
Dim OutMail_Approver As Object
Dim reply_mail As Object
Dim botton As String
Dim fso As Object
Dim logFile As Object
Dim logFilePath As String
Dim logFileName As String
Dim objReplyAllMail As Outlook.MailItem
Dim objRecipients As Outlook.Recipients
Dim objRecipient As Outlook.Recipient
Dim objSeparateReply As Outlook.MailItem
Set fso = CreateObject("Scripting.FileSystemObject")
Set rng = Nothing
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail_User = OutApp.CreateItem(0)
Set OutMail_Approver = OutApp.CreateItem(0)
Set rng = Selection.SpecialCells(xlCellTypeVisible)
Set rng = Sheets("Petty Cash Log").Range("A1:L40").SpecialCells(xlCellTypeVisible)
On Error Resume Next
On Error GoTo 0
If rng Is Nothing Then
MsgBox "The selection is not a range or the sheet is protected" & _
vbNewLine & "please correct and try again.", vbOKOnly
Exit Sub
End If
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
user = Range("G37").Value
approver = Range("G38").Value
approver_name = Range("E38").Value
Range("I38").Value = Date & " " & Time
Dim oLook As Object, oMail As Object, strEmail As String
Dim FD As FileDialog, vrtSelectedItem As Variant
Set FD = Application.FileDialog(msoFileDialogFilePicker)
Set oLook = CreateObject("Outlook.Application")
Set oMail = oLook.CreateItem(0)
On Error GoTo 0
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
ActiveWorkbook.CheckCompatibility = False
ActiveWorkbook.Save
''End With
On Error Resume Next
strHtml = "<html>" & "<body><br>" & "</br>" & "</body>" & "</html>"
xx = xWs.Range("E37") & " (NTL)_" & xWs.Range("K2") & "/"
inv = Range("K2").Value
'mail_Approval()
With OutMail_Approver
.To = "CFGFIN006@someone.com"
.cc = user
.Subject = inv
.HTMLBody = "Dear AP Team Please Review Invoice Number " & inv & " and attach file in this Email.Information is in below : " & strHtml & RangetoHTML(rng)
.Send
On Error GoTo 0
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
ActiveWorkbook.CheckCompatibility = False
ActiveWorkbook.Save
ActiveWorkbook.Close
End With
End Sub
Я использую шаг Into (F8) в скрипте VBA. Он отправляется один раз по электронной почте. В скрипте Python я убираю скобки в коде. Он может отправлять только электронную почту, но с ошибкой. Как это:
Скажите, пожалуйста, как решить эту проблему.