Notifications
Clear all

Macro separar arquivos e enviar por Notes

2 Posts
2 Usuários
0 Reactions
698 Visualizações
(@piupa)
Posts: 1
New Member
Topic starter
 

Galera, bom dia!
tenho um pequeno problema aqui.
Estou tentando juntar 2 macros que eu utilizo mas não estou conseguindo fazer o link direito.

Tenho uma tabela em que tenho que separar as informações por aba e depois criar um arquivo por cada aba.
o que queria agora era já realizar este processo e enviar o resultado (os arquivos) pelo notes.

no entanto eu só tenho a macro em que se cria uma lista de receptores e o arquivo a ser enviado já tem que estar descrito.

como faço para que a macro já envie para um destinatário (que poderá ser uma das colunas na tabela por exemplo) somente o arquivo referente a ele?

segue o que tenho até o momento como exemplo: (em vermelho o que tem que ser modificado e que não estou encontrando a solução)

Option Explicit

    Sub FiltraEmAbas()
        Dim ws1 As Worksheet
        Dim wsNew As Worksheet
        Dim rng As Range
        Dim r As Integer
        Dim c As Range
        
        Set ws1 = Sheets("Geral")
        
        'Calcula e Monta o range Nomeado
        Call AddNameRange
        
        Set rng = Range("Database")

        'extract a list of Sales Reps
        ws1.Columns("A:A").AdvancedFilter _
          Action:=xlFilterCopy, _
          CopyToRange:=Range("P1"), Unique:=True
        r = Cells(Rows.Count, "P").End(xlUp).Row
        
        'set up Criteria Area
        Range("R1").Value = Range("A2").Value
        
            For Each c In Range("P3:P" & r)
              'add the rep name to the criteria area
              ws1.Range("R2").Value = c.Value
              'add new sheet and run advanced filter
              Set wsNew = Sheets.Add
              wsNew.Move After:=Worksheets(Worksheets.Count)
              wsNew.Name = c.Value
              rng.AdvancedFilter Action:=xlFilterCopy, _
                  CriteriaRange:=Sheets("Geral").Range("R1:R2"), _
                  CopyToRange:=wsNew.Range("A1"), _
                  Unique:=False
            Next
        ws1.Select
        ws1.Columns("P:R").Delete
        
        '######## Executa a rotina de separação...
        Separa
    End Sub

    
    'Deleta se já existir, Calcula e Monta e Nomeia o Range
    Sub AddNameRange()
        Dim LastRow As Long
        Dim LastCol As Long
        
        'Deleta se já existir, Calcula e Monta o Range Nomeado
        On Error Resume Next
        ActiveWorkbook.Names("Database").Delete
        
            LastRow = Cells(65536, 1).End(xlUp).Row - 1 'Define Última Linha
            LastCol = Cells(2, 255).End(xlToLeft).Column 'Define Última coluna
            
            'Monta o novo range Nomeado
            Cells(2, 1).Resize(LastRow, LastCol).Name = "Database"
    
    End Sub

Sub Separa()
    Dim Novo As Workbook
    Dim Aba As Worksheet
    Dim Onde As String
    Dim Nome As String
    Dim notesSession As Object
    Dim notesMailFile As Object
    Dim notesDocument As Object
    Dim notesField As Object
   [color=#FF0000] Dim receptores(2) As Variant[/color]
    For Each Aba In ThisWorkbook.Worksheets
        If Aba.Name <> "Geral" Then
            Nome = Aba.Name
            
            'Aba.Copy
            Aba.Move
    
            Set Novo = Workbooks(Workbooks.Count)
    
            Onde = ThisWorkbook.Path
            If Right(Onde, 1) <> "/" Then
                Onde = Onde & ""
            End If
    
            Novo.SaveAs Onde & Nome, xlWorkbookDefault
            Novo.Close
        End If
    Next
    
 [color=#BF0000]   'Cria Uma lista de destinatários
    receptores(0) = "xxxx@xxxxx.com"[/color]        
    'Abre uma sessão do notes, abre a base de dados e cria um documento.
    Set notesSession = CreateObject("Notes.NotesSession")
    Set notesMailFile = notesSession.GETDATABASE("", "names.nsf")  '- *.nsf = arq. com lista de contatos
    Set notesDocument = notesMailFile.CreateDocument
    
    'Configura Subject, SendTo e Abre um nomo corpo de e-mail
    Set notesField = notesDocument.AppendItemValue("Subject", "Resumo de Dados")
    Set notesField = notesDocument.AppendItemValue("SendTo", receptores)
    Set notesField = notesDocument.CreateRichTextItem("Body")
        
    'Escreve o texto padrão no e-mail.
    With notesField
        .AppendText "Bom Dia"
        .AddNewLine (2)
        .AppendText "Segue o resumo"
        .AddNewLine (2)
        .AppendText "Dúvidas me contate"
        .AddNewLine (2)
        .AppendText "Abraços,"
        .AddNewLine (1)
        .AppendText "Equipe de Resumos"
        .AddNewLine (3)
    End With
    
[color=#BF0000]    notesField = notesField.EmbedObject(1454, "", "C:DesktopTabela.xlsx")[/color]        
    'Envia o e-mail
    notesDocument.Send False
    
    'Limpa as variáveis
    Set notesSession = Nothing
    Set notesMailFile = Nothing
    Set notesDocument = Nothing
    Set notesField = Nothing
    
End Sub

Obrigado galera!!!
Abs!

 
Postado : 14/12/2012 5:47 am
(@fernandofernandes)
Posts: 43750
Illustrious Member
 

Bom dia!!

Leia em:
http://www.fabalou.com/VBandVBA/lotusnotesmail.asp
Tente adaptar

Sub Enviar_Email_Lotus()
    Dim noSession As Object, noDatabase As Object, noDocument As Object
    Dim obAttachment As Object, EmbedObject As Object
    Dim stSubject As Variant, stAttachment As String
    Dim vaRecipient As Variant, vaMsg As Variant
     
    Const EMBED_ATTACHMENT As Long = 1454
    Const stTitle As String = "Status de pasta de trabalho ativa"
    Const stMsg As String = "A pasta de trabalho ativa deve primeiro ser salva " & vbCrLf _
    & "before it can be sent as an attachment."
    If Len(ActiveWorkbook.Path) = 0 Then
        MsgBox stMsg, vbInformation, stTitle
        Exit Sub
    End If
     
    If ActiveWorkbook.Saved = False Then
        If MsgBox("Do you want to save the changes before sending?", _
        vbYesNo + vbInformation, stTitle) = vbYes Then _
        ActiveWorkbook.Save
    End If
    
    Do
        vaRecipient = Application.InputBox( _
        Prompt:="Please add name of the recipient such as:" & vbCrLf _
        & "will@yahoo.co.uk or just the name if internal mail within Unity.", _
        Title:="Beneficiário", Type:=2)
    Loop While vaRecipient = ""
    
    If vaRecipient = False Then Exit Sub
   
    Do
        vaMsg = Application.InputBox( _
        Prompt:="Por favor insira a mensagem como:" & vbCrLf _
        & "Enclosed please find the weekly report.", _
        Title:="Mensagem", Type:=2)
    Loop While vaMsg = ""
     
   
    If vaMsg = False Then Exit Sub
     
    Do
        stSubject = Application.InputBox( _
        Prompt:="Por favor, adicione um assunto como:" & vbCrLf _
        & "Relatório Semanal.", _
        Title:="Assunto", Type:=2)
    Loop While stSubject = ""
     
    stAttachment = ActiveWorkbook.FullName
     
    Set noSession = CreateObject("Notes.NotesSession")
    Set noDatabase = noSession.GETDATABASE("", "")
     
    If noDatabase.IsOpen = False Then noDatabase.OPENMAIL
     
    Set noDocument = noDatabase.CreateDocument
    Set obAttachment = noDocument.CreateRichTextItem("stAttachment")
    Set EmbedObject = obAttachment.EmbedObject(EMBED_ATTACHMENT, "", stAttachment)
     
    With noDocument
        .Form = "Memo"
        .SendTo = vaRecipient
        .Subject = stSubject
        .Body = vaMsg
        .SaveMessageOnSend = True
    End With
     
    With noDocument
        .PostedDate = Now()
        .Send 0, vaRecipient
    End With
     
     
    Set EmbedObject = Nothing
    Set obAttachment = Nothing
    Set noDocument = Nothing
    Set noDatabase = Nothing
    Set noSession = Nothing
     
     
    AppActivate "Microsoft Excel"
    MsgBox "e-mail, criado e distribuído com sucesso.", vbInformation
End Sub
 
Postado : 14/12/2012 7:08 am