【发布时间】:2014-09-15 18:10:25
【问题描述】:
所以我正在工作的项目,我有一个 VBS 脚本,我每天使用 Windows 任务计划程序自动运行。 VBS 脚本调用此宏。但是,我在 Outlook 中的电子邮件总是弹出一个我无法摆脱的弹出窗口(在我的组织中不是一个选项。)。我也无法在弹出窗口中调用“发送”键对象,因为这些对象位于访问受限目录中。所以我尝试使用发送键来简单地发送回车键来点击弹出窗口。
我的问题是我的 sendkeys 代码只能部分工作。它发送回车键,但它只在我在 MS Office 应用程序中时发送回车键,但不会发送到弹出窗口。有什么想法吗?
Sub Mail_ActiveSheet()
' Refreshes webquery
Application.Worksheets("Sheet1").Range("A1").QueryTable.Refresh BackgroundQuery:=False
' Enters Title Comments in Cell M2
Range("$M$2").Value = "Comments"
' Enters formula in column M
Range("$M$3").Formula = Range("G3") & (",") & Range("L3")
' Draws formula to the end of the workbook
Dim Lastrow As Long
Application.ScreenUpdating = False
Lastrow = Range("L" & Rows.Count).End(xlUp).Row
Range("M3:M" & Lastrow).Formula = "=G3&"",""&L3"
ActiveSheet.AutoFilterMode = False
Application.ScreenUpdating = True
' Works in Excel 2000, Excel 2002, Excel 2003, Excel 2007, Excel 2010, Outlook 2000, Outlook 2002, Outlook 2003, Outlook 2007, Outlook 2010
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim Sourcewb As Workbook
Dim Destwb As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim OutApp As Object
Dim OutMail As Object
Dim WshShell As Object
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set Sourcewb = ActiveWorkbook
' Next, copy the sheet to a new workbook.
' You can also use the following line, instead of using the ActiveSheet object,
' if you know the name of the sheet you want to mail :
' Sheets("Sheet5").Copy
ActiveSheet.Copy
Set Destwb = ActiveWorkbook
' Determine the Excel version, and file extension and format.
With Destwb
Select Case Sourcewb.FileFormat
Case 51: FileExtStr = ".csv": FileFormatNum = 6
Case 52:
If .HasVBProject Then
FileExtStr = ".csv": FileFormatNum = 6
Else
FileExtStr = ".csv": FileFormatNum = 6
End If
Case 56: FileExtStr = ".csv": FileFormatNum = 6
Case Else: FileExtStr = ".csv": FileFormatNum = 6
End Select
End With
' You can use the following statements to change all cells in the
' worksheet to values.
' With Destwb.Sheets(1).UsedRange
' .Cells.Copy
' .Cells.PasteSpecial xlPasteValues
' .Cells(1).Select
' End With
' Application.CutCopyMode = False
' Save the new workbook, mail, and then delete it.
TempFilePath = Environ$("temp") & "\"
TempFileName = "Part of " & Sourcewb.Name & " " _
& Format(Now, "dd-mmm-yy h-mm-ss")
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With Destwb
.SaveAs TempFilePath & TempFileName & FileExtStr, _
FileFormat:=FileFormatNum
On Error Resume Next
' Change the mail address and subject in the macro before
' running the procedure.
With OutMail
.To = "myemail@gmail.com"
.CC = ""
.BCC = ""
.Subject = "Daily File"
.Body = "Daily File"
.Attachments.Add Destwb.FullName
' You can add other files by uncommenting the following statement.
'.Attachments.Add ("C:\test.txt")
' In place of the following statement, you can use ".Display" to
' display the mail.
.Send
End With
On Error GoTo 0
.Close SaveChanges:=False
End With
' Delete the file after sending.
Kill TempFilePath & TempFileName & FileExtStr
Set OutMail = Nothing
Set OutApp = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
Application.Wait (Now + TimeValue("0:00:04"))
ActiveWindow.Activate
Set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys "~", True
WshShell.SendKeys ("{NUMLOCK}")
End Sub
【问题讨论】:
-
使用
FINDWINDOW和SENDMESSAGEAPI 代替SendKeys -
您可能需要在发送键之后执行
DoEvents命令,以便接收窗口可以处理消息。 产生执行以便操作系统可以处理其他事件。 语法`DoEvents()` -
如果您想将密钥发送到 Outlook 应用程序,请不要在 Excel 应用程序中激活窗口 - 删除
ActiveWindow.Activate -
我能够通过将发送键命令放入 VBScript 中,然后从单个批处理 (.bat) 文件中运行两个宏/脚本来让我的脚本工作
标签: vba excel outlook sendkeys