【问题标题】:Macro stops at `ThisWorkbook.EnvelopeVisible = True`宏在 `ThisWorkbook.EnvelopeVisible = True` 处停止
【发布时间】:2018-01-26 15:40:37
【问题描述】:

我正在尝试通过使用 VB 自动运行宏而不打开 Excel 来自动化该过程。

我已经测试了宏,如果打开工作簿,它可以正常工作。但是,当我通过 VBs 脚本运行宏时,Maro 停在ThisWorkbook.EnvelopeVisible = True。有人知道为什么吗?

我的宏

Sub sendEmail()
MsgBox "sendEmail Start!"    'Appears when run VBs

   ThisWorkbook.EnvelopeVisible = True
   MsgBox "EnvelopeVisible True!" 'did not appear when run VBs

    With ThisWorkbook.Sheets("sheet1").MailEnvelope
    MsgBox "MailEnvelope Prepare!"
      .Introduction = "Message"
      .Item.To = "To who it may concern"
      .Item.Subject = ""
      .Item.Send
      MsgBox "MailEnvelope Send!"
    End With

  ThisWorkbook.EnvelopeVisible = False
  MsgBox "sendEmail Done!"
End Sub

我的 VB

Set objExcel = CreateObject("Excel.Application")
objExcel.Application.Run "'C:\Users\UserName\Desktop\Folder\Ver 6.xlsm'!AutoEmail.sendEmail"
objExcel.DisplayAlerts = False
objExcel.Application.Quit
Set objExcel = Nothing

我正在运行 VB 来激活宏

【问题讨论】:

  • 如果您使用F8 单步执行它,它会运行该行然后退出子吗?
  • @BruceWayne 是的,当工作簿打开时。宏运行良好。就在我使用 VB 运行宏时,它停在MsgBox "EnvelopeVisible True!"MsgBox "sendEmail Start!" 确实出现了
  • "...当我使用 VB 运行宏时..." - 你是什么意思?而不是ThisWorkbook,也许实际上使用文件名/路径分配工作簿?
  • 一开始我也觉得是因为ThisWorkbook。我尝试使用另一个宏With ThisWorkbook.Worksheets("shee1") MsgBox"Checking"Checking 确实出现了。这让我认为这是因为EnvelopeVisible,但我对此并不积极。
  • 在 VBS 中,在 Set objExcel = CreateObject("Excel.Application") 之后立即添加行 objExcel.Visible = True 并尝试。

标签: vba excel


【解决方案1】:

您需要在 VBS 中添加以下行:

objExcel.Visible = True

紧接着

Set objExcel = CreateObject("Excel.Application")

这是基于您的问题描述给出的一个简单线索。

  • 如果 Excel 宏从 Excel 打开并运行,它就可以工作。

  • 如果从 VBS 运行它会停止

当我查看这个属性的 MSDN 解释时

https://msdn.microsoft.com/en-us/vba/excel-vba/articles/workbook-envelopevisible-property-excel

它说

如果电子邮件撰写标题和信封工具栏都可见,则为真。读/写布尔值。

因此,要使其工作,我们需要使父对象可见,即 Excel.Application。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-09
    • 1970-01-01
    • 2017-05-15
    • 2013-11-29
    相关资源
    最近更新 更多