Importar Automaticamente XMLs

mathgalvan

15-06-2021 16:46:21

Olá Boa tarde Senhores Programadores. Sou novo por aqui e trabalho numa Transportadora.
Elaborei uma Planilha de Importação de XML e está rodando perfeitamente, porém, gostaria de programá-la para importar os XMLs automaticamente ao abrir a planilha e, ao importar, excluisse os XMLs da pasta que foram importados.
Estou usando essa progrmação para importar:

Sub importarNFe()

'Variaveis de manipulação de xml
Dim NFe As New MSXML2.DOMDocument

'Variaveis sem tipo difinido
Dim XMLS As Variant, XML As Variant

'Variaveis de Dicionário
Dim Notas As New Scripting.Dictionary

XMLS = AbrirXMLS

For Each XML In XMLS

NFe.Load (XML)

i = i + 1

With DadosNFe

.Data = Left(NFe.SelectSingleNode("//dhEmi").Text, 10)
.Hora = Mid(NFe.SelectSingleNode("//dhEmi").Text, 12, 8)
.NºNFe = NFe.SelectSingleNode("//nNF").Text
.Série = NFe.SelectSingleNode("//serie").Text
.Posto = NFe.SelectSingleNode("//emit//xNome").Text
On Error Resume Next
.Fantasia = NFe.SelectSingleNode("//emit//xFant").Text

If xFant = "" Then
.Fantasia = ""
End If

.Produto = NFe.SelectSingleNode("//xProd").Text
.ValorUni = NFe.SelectSingleNode("//vUnCom").Text
.Litros = NFe.SelectSingleNode("//qCom").Text
.ValorTotal = NFe.SelectSingleNode("//vProd").Text
.CFOP = NFe.SelectSingleNode("//CFOP").Text
.NCM = NFe.SelectSingleNode("//NCM").Text
.Informações = NFe.SelectSingleNode("//infCpl").Text


Notas(i) = Array(i, .Data, .Hora, .NºNFe, .Série, .Posto, .Fantasia, .Produto, .ValorUni, .Litros, .ValorTotal, .CFOP, .NCM, .Informações)

End With


Call LimparDadosNFe

Next XML

With Relatório

.Range("A" & .Range("A" & Rows.Count).End(xlUp).Row + 1).Resize(Notas.Count, UBound(Notas(i)) + 1).Value = Application.Transpose(Application.Transpose(Notas.Items))


End With
End Sub

Se podurem me ajudar, ficarei mto agradecido!!