按“新邮件”使用VBA的Microsoft 365电子邮件中的按钮 [英] Press "New mail" button in Microsoft 365 email using VBA

查看:150
本文介绍了按“新邮件”使用VBA的Microsoft 365电子邮件中的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 < button autoid =_ n_jtype =buttonclass =_ n_j2 o365buttonaria-labelledby =_ ariaId_31regionbehavior =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- circlerole =presentation>< / span>
< span class =_ n_k2 ms-font-weight-regular ms-font-color-themePrimaryid =_ ariaId_31>新邮件< / span>
< / button>

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

  Dim objButton As Object 
设置objButton = IE.Document.getElementById(_ n_j2 o365button)
objButton.onfocus
objButton。 onclick

但是我收到运行时错误。我认为这是因为按钮是使用javascript(页面不按重新加载,只是更新)。任何想法如何做?

解决方案

尝试 IE.Document.GetElementsByClassName(_ n_j2 o365button) (0)。点击



因为没有为按钮分配ID,所以不能使用.GetElementByID()。 >

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



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



(再次,我'假设你没有像选项基础1 声明的东西



你可能需要迭代通过收集,并以某种方式测试每个项目以查看您需要的按钮,并相应地更改(0)


I am trying to send an email over Microsoft 365 using IE as the browser. I am currently having difficulty getting VBA to create a new email and am trying to do so by simply telling it to click the "New Mail" button which has the following HTML code:

<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>

I am trying to press this using the following VBA code:

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

But am getting a run time error. I think this is because the button is using javascript (page doesnt reload when pressed, just updates). Any ideas how to do this?

解决方案

Try IE.Document.GetElementsByClassName("_n_j2 o365button")(0).Click

You can't use .GetElementByID() because the button hasn't been assigned an ID.

I've used .GetElementsByClassName() because we know that the button has a class assigned, however a class is not unique. This method returns a collection of all elements with this class - without seeing the whole HTML, I'm going to make an assumption that all buttons on the page share this class.

The (0) tells the code to click the first item stored in the collection.

(Again, I'm going to assume you don't have something like Option Base 1 declared)

You might have to iterate through the collection and somehow test each item to see which button you need and change the (0) accordingly.

这篇关于按“新邮件”使用VBA的Microsoft 365电子邮件中的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆