ppossa
17-08-2011 12:50:29
Olá pessoal,
estou com uma duvida se da pra fazer ou não. Tenho o código abaixo em cada planilha para salvar um arquivo em excel e ja envi-lo por e-mail.
o código esta funcionando perfeitamente, mas tenho que te-lo em cada planilha, ou seja, essas planilha são para usar em orçamento e faço em média de 4 a 5 orçamentos por dia. Imaginem querer fazer uma melhoria no código?
A minha intenção é fazer uma macro em uma planilha unica, onde eu pego a planilha desejada e executo a marco.
tentei fazer, mas não sou muito bom nisso e não me dei bem.
aguardo resposta e segue o código.
vlw pessoal
--------------------------------------------------------
Option Explicit
Dim Nome As String
Dim Path As String
Sub Criar_PDF()
Dim PDF As PDFCreator.clsPDFCreator
Nome = Range("b2").Value & ".pdf" 'ThisWorkbook.Name & ".pdf"
Path = "\\server\Custo\Orçamentos\Pdf's\2011\"
Set PDF = New PDFCreator.clsPDFCreator
With PDF
If .cStart("/NoProcessingAtStartup") = False Then
MsgBox "Não é Possível iniciar o PDFCreator", vbCritical + vbOKOnly, "PDF"
Exit Sub
End If
.cOption("UseAutosave") = 1
.cOption("UseAutosaveDirectory") = 1
.cOption("AutosaveDirectory") = Path
.cOption("AutosaveFilename") = Nome
.cOption("AutosaveFormat") = 0 ' 0 = PDF
.cClearCache
End With
ActiveSheet.PrintOut copies:=1, ActivePrinter:="PDFCreator"
Do Until PDF.cCountOfPrintjobs = 1
DoEvents
Loop
PDF.cPrinterStop = False
Do Until PDF.cCountOfPrintjobs = 0
DoEvents
Loop
PDF.cClose
Set PDF = Nothing
End Sub
Sub Enviar_email()
Dim anexo As String
Dim r As Integer
Dim fim
Dim enviar
Dim objOlAppApp As Outlook.Application
Dim objOlAppMsg As Outlook.MailItem
Dim objOlAppRecip As Outlook.Recipient
Dim objOlAppAnexo As Outlook.Attachment
Set objOlAppApp = CreateObject("Outlook.Application")
Set objOlAppMsg = objOlAppApp.CreateItem(olMailItem)
With objOlAppMsg
'Processar endereços para o envio
Set objOlAppRecip = .Recipients.Add("Orçamento")
objOlAppRecip.Type = olTo
'verificar se existe destinatário
If .Recipients.Count = 0 Then GoTo fim
'Anexar ficheiro, com o nome e caminho escrito na celula C13
anexo = Path & "\" & Nome
'verificar se o caminho para o anexo é válido
If Dir(anexo) = "" Then
r = MsgBox("Anexo inexistente ou caminho invalido, " & _
"pretende enviar assim mesmo ? ", _
vbYesNo, _
"Erro de anexo")
If r = vbYes Then GoTo enviar Else GoTo fim
End If
Set objOlAppAnexo = .Attachments.Add(anexo)
enviar:
'O assunto
.Subject = Nome
'O conteudo do Mail
.Body = "Segue em anexo orçamento " & Nome & vbCrLf & vbCrLf & Range("B3") & vbCrLf
'enviar mensagem
.Send
End With
fim:
'Libertar as variaveis
Set objOlAppApp = Nothing
Set objOlAppMsg = Nothing
Set objOlAppAnexo = Nothing
Set objOlAppRecip = Nothing
End Sub
Sub Executar()
Call Criar_PDF
Call Enviar_email
ActiveWorkbook.Save
End Sub
estou com uma duvida se da pra fazer ou não. Tenho o código abaixo em cada planilha para salvar um arquivo em excel e ja envi-lo por e-mail.
o código esta funcionando perfeitamente, mas tenho que te-lo em cada planilha, ou seja, essas planilha são para usar em orçamento e faço em média de 4 a 5 orçamentos por dia. Imaginem querer fazer uma melhoria no código?
A minha intenção é fazer uma macro em uma planilha unica, onde eu pego a planilha desejada e executo a marco.
tentei fazer, mas não sou muito bom nisso e não me dei bem.
aguardo resposta e segue o código.
vlw pessoal
--------------------------------------------------------
Option Explicit
Dim Nome As String
Dim Path As String
Sub Criar_PDF()
Dim PDF As PDFCreator.clsPDFCreator
Nome = Range("b2").Value & ".pdf" 'ThisWorkbook.Name & ".pdf"
Path = "\\server\Custo\Orçamentos\Pdf's\2011\"
Set PDF = New PDFCreator.clsPDFCreator
With PDF
If .cStart("/NoProcessingAtStartup") = False Then
MsgBox "Não é Possível iniciar o PDFCreator", vbCritical + vbOKOnly, "PDF"
Exit Sub
End If
.cOption("UseAutosave") = 1
.cOption("UseAutosaveDirectory") = 1
.cOption("AutosaveDirectory") = Path
.cOption("AutosaveFilename") = Nome
.cOption("AutosaveFormat") = 0 ' 0 = PDF
.cClearCache
End With
ActiveSheet.PrintOut copies:=1, ActivePrinter:="PDFCreator"
Do Until PDF.cCountOfPrintjobs = 1
DoEvents
Loop
PDF.cPrinterStop = False
Do Until PDF.cCountOfPrintjobs = 0
DoEvents
Loop
PDF.cClose
Set PDF = Nothing
End Sub
Sub Enviar_email()
Dim anexo As String
Dim r As Integer
Dim fim
Dim enviar
Dim objOlAppApp As Outlook.Application
Dim objOlAppMsg As Outlook.MailItem
Dim objOlAppRecip As Outlook.Recipient
Dim objOlAppAnexo As Outlook.Attachment
Set objOlAppApp = CreateObject("Outlook.Application")
Set objOlAppMsg = objOlAppApp.CreateItem(olMailItem)
With objOlAppMsg
'Processar endereços para o envio
Set objOlAppRecip = .Recipients.Add("Orçamento")
objOlAppRecip.Type = olTo
'verificar se existe destinatário
If .Recipients.Count = 0 Then GoTo fim
'Anexar ficheiro, com o nome e caminho escrito na celula C13
anexo = Path & "\" & Nome
'verificar se o caminho para o anexo é válido
If Dir(anexo) = "" Then
r = MsgBox("Anexo inexistente ou caminho invalido, " & _
"pretende enviar assim mesmo ? ", _
vbYesNo, _
"Erro de anexo")
If r = vbYes Then GoTo enviar Else GoTo fim
End If
Set objOlAppAnexo = .Attachments.Add(anexo)
enviar:
'O assunto
.Subject = Nome
'O conteudo do Mail
.Body = "Segue em anexo orçamento " & Nome & vbCrLf & vbCrLf & Range("B3") & vbCrLf
'enviar mensagem
.Send
End With
fim:
'Libertar as variaveis
Set objOlAppApp = Nothing
Set objOlAppMsg = Nothing
Set objOlAppAnexo = Nothing
Set objOlAppRecip = Nothing
End Sub
Sub Executar()
Call Criar_PDF
Call Enviar_email
ActiveWorkbook.Save
End Sub