Este fórum está sendo desativado

Depois de 9 anos, este fórum será desativado. Mas calma.... estamos migrando para uma comunidade no DISCORD. Junte-se a nós.

ENTRAR PARA DISCORD

Tópicos relacionados a códigos VBA, gravação de macros, etc.
Por luizlaljr
#32846
Bom dia Senhores e Senhoras, está é minha primeira pergunta. Nunca fiz, pois sempre encontro a resposta em algum fórum, mas desta vez, eu perdi! Então vamos a minha dúvida.

Utilizei uma mescla de códigos na internet para criar essa rotina que entra no gmail via Internet Explorer, cria uma nova mensagem, TENTA ANEXAR um arquivo e o envia.

(Não utilizei envio via smtp, pois a única porta que o proxy do meu trabalho libera, é a :8080)

Para o código funcionar você deve configurar o seu email com o HTML de visualização padrão do e-mail.

Só que na hora de anexar, ele abre o FileDialog e eu não sei qual código escrevo para escrever o arquivo que eu quero selecionar.

Fiz uma pesquisa sobre FileDialog, porém só ensinam a trabalhar com o dialog que você criou e não um aberto pelo sistema.

Estou há três dias neste sofrimento!

Código que abre o IE e acessa o gmail, e realiza o login se necessário

Vale ressaltar que como a internet aqui é lenta existe um loop para esperar a página está pronta e uma função com um delay.

Para a rotina funcionar necessita adicionar, na aba referencias, as duas bibliotecas listadas abaixo :

Microsoft Internet Controls;

Microsoft HTML Object Library;

Public Sub EnviarEmail()

Dim ie As New SHDocVw.InternetExplorer
Dim HTMLDoc As New MSHTML.HTMLDocument
Dim HTMLElement As MSHTML.IHTMLElement
Dim HTMLInput As MSHTML.HTMLInputElement
Dim HTMLAnch As MSHTML.HTMLAnchorElement
Abre IE e acessa o gmail;

With ie
.Visible = True
.Silent = True
.navigate "https://accounts.google.com/signin/v2/i ... rviceLogin"
Do While .Busy Or .readyState <> READYSTATE_COMPLETE
DoEvents
Loop
End With

Call WaitAFewSeconds(2)

Set HTMLDoc = ie.Document
Realiza o login se necessário;

For Each HTMLInput In HTMLDoc.all
If HTMLInput.getAttribute("name") = "identifier" Then
HTMLDoc.all.identifier.Value = "Meu Login"
HTMLDoc.all.identifierNext.Click

With ie
Do While .Busy Or .readyState <> READYSTATE_COMPLETE
DoEvents
Loop
End With

Call WaitAFewSeconds(2)

For Each HTMLElement In HTMLDoc.getElementsByName("password")
If HTMLElement.getAttribute("type") = "password" Then
HTMLElement.Value = "Minha Senha"
Exit For
End If
Next HTMLElement

HTMLDoc.all.passwordNext.Click

With ie
Do While .Busy Or .readyState <> READYSTATE_COMPLETE
DoEvents
Loop
End With

Call WaitAFewSeconds(4)
Exit For
End If
Next
Procura o link para Escrever Email e clica;

For Each HTMLAnch In HTMLDoc.all
If Len(HTMLAnch.href) > 16 Then
If Right(HTMLAnch.href, 16) = "?&cs=b&pv=tl&v=b" Then
HTMLAnch.Click
Exit For
End If
End If
Next

With ie
Do While .Busy Or .readyState <> READYSTATE_COMPLETE
DoEvents
Loop
End With

Call WaitAFewSeconds(6)
Preenche os campos do email;

HTMLDoc.all("to").innerText = "destinatario@email.com"

HTMLDoc.all("subject").innerText = "Assunto"

HTMLDoc.all("body").innerText = "Corpo do email"
Procura o botão para anexar o arquivo e clica;

For Each HTMLInput In HTMLDoc.all
If HTMLInput.getAttribute("name") = "file0" Then
HTMLInput.Click
Exit For
End If
Next
Exatamente neste ponto, é aberta a caixa de FileDialog para ser selecionado o arquivo. E eu não estou sabendo como escrever o nome do arquivo a ser selecionado e anexado ao email.

Qual o código insiro aqui?
Procura o botão de enviar o email e clica para o envio;

For Each HTMLInput In HTMLDoc.all
If HTMLInput.getAttribute("name") = "nvp_bu_send" Then
HTMLInput.Click
Exit For
End If
Next

With ie
Do While .Busy Or .readyState <> READYSTATE_COMPLETE
DoEvents
Loop
End With
Finaliza o IE e termina a rotina;

ie.Quit

Set ie = Nothing
Set HTMLDoc = Nothing
Set HTMLElement = Nothing
Set HTMLAnch = Nothing

End Sub
Aqui segue a rotina de espera.

Public Sub WaitAFewSeconds(ByVal tempo As Integer)

Dim sngStart As Single

Dim PAUSE_TIME As Integer

PAUSE_TIME = tempo 'seconds

sngStart = Timer
Do Until Timer - sngStart > PAUSE_TIME
DoEvents
Loop

End Sub
Desculpem-me se fui muito prolixo!

Desde já, obrigado pela atenção.
long long title how many chars? lets see 123 ok more? yes 60

We have created lots of YouTube videos just so you can achieve [...]

Another post test yes yes yes or no, maybe ni? :-/

The best flat phpBB theme around. Period. Fine craftmanship and [...]

Do you need a super MOD? Well here it is. chew on this

All you need is right here. Content tag, SEO, listing, Pizza and spaghetti [...]

Lasagna on me this time ok? I got plenty of cash

this should be fantastic. but what about links,images, bbcodes etc etc? [...]

Estamos migrando para uma comunidade no Discord