使用VBA在Microsoft 365电子邮件中按“新邮件”button

我正在尝试通过使用IE作为浏览器的Microsoft 365发送电子邮件。 我目前很难得到VBA创build一个新的电子邮件,并试图通过简单地告诉它点击具有以下HTML代码的“新邮件”button:

<button autoid="_n_j" type="button" class="_n_j2 o365button" aria-labelledby="_ariaId_31" regionbehavior="1"> <span class="_n_l2 owaimg ms-Icon--plus ms-bcl-tp ms-bg-transparent ms-icon-font-circle ms-fcl-tp ms-icon-font-size-20-circle" role="presentation"></span> <span class="_n_k2 ms-font-weight-regular ms-font-color-themePrimary" id="_ariaId_31">New mail</span> </button> 

我正在尝试使用以下VBA代码按此:

 Dim objButton As Object Set objButton = IE.Document.getElementById("_n_j2 o365button") objButton.onfocus objButton.onclick 

但是我得到一个运行时错误。 我认为这是因为button是使用JavaScript(页面没有重新加载时按下,只是更新)。 任何想法如何做到这一点?

尝试IE.Document.GetElementsByClassName("_n_j2 o365button")(0).Click

您不能使用.GetElementByID(),因为该button还没有被分配一个ID。

我已经使用.GetElementsByClassName(),因为我们知道button有一个类分配,但是一个类是不唯一的。 这个方法返回一个包含这个类的所有元素的集合 – 没有看到整个HTML,我将假设页面上的所有button都共享这个类。

(0)告诉代码单击存储在集合中的第一个项目。

(同样,我假设你没有像声明的Option Base 1那样的东西)

您可能需要遍历集合,并以某种方式testing每个项目,以查看您需要哪个button,并相应地更改(0)