PDA

View Full Version : Saving word doc as pdf file and send it as attachement



buhay
09-22-2010, 12:54 PM
I was wondering if there's a macro that convert word doc to pdf file and send it as attachement through e-mail.

I am using the following code to send a word doc but how do I make it save the word doc as pdf file and send it as pdf?

Sub SendDocumentAsAttachment()

Dim bStarted As Boolean

Dim oOutlookApp As Outlook.Application

'You'll need to add the Outlook Object Library to VBA Tools References

Dim oItem As Outlook.MailItem

On Error Resume Next

If Len(ActiveDocument.Path) = 0 Then 'Document has not been saved

ActiveDocument.Save 'so save it

End If

'see if Outlook is running and if so turn your attention there

Set oOutlookApp = GetObject(, "Outlook.Application")

If Err <> 0 Then 'Outlook isn't running

'So fire it up

Set oOutlookApp = CreateObject("Outlook.Application")

bStarted = True

End If

'Open a new e-mail message

Set oItem = oOutlookApp.CreateItem(olMailItem)

With oItem 'and add the detail to it

.To = "" 'send to this address
.CC = ""
'.BCC = ""

.Subject = " per " & Format(Now, "dd-mmm-yyyy-hh-mm-ss") 'This is the message subject

.Body = Mailbody


.Attachments.Add Source:=ActiveDocument.FullName, Type:=olByValue

.Display


End With

If bStarted Then 'If the macro started Outlook, stop it again.

oOutlookApp.Quit

End If

'Clean up

Set oItem = Nothing

Set oOutlookApp = Nothing

End Sub

Sub SaveAsDate()

ChangeFileOpenDirectory ""

ActiveDocument.SaveAs FileName:="x" & "-" & Format(Now, "dd-mmm-yyyy-hh-mm-ss") & ".doc"

End Sub

Tinbendr
09-22-2010, 02:27 PM
I can't remember which version of Word you have?

buhay
09-22-2010, 03:03 PM
I can't remember which version of Word you have?

I am using word 2003.

buhay
09-23-2010, 01:15 PM
There must be a way how to convert a doc to pdf file and send it as attachement.

The following procedure does it with and excel workbook:


Sub SendAsPDF()
' Uses early binding
' Requires a reference to the Outlook Object Library
Dim OutlookApp As Outlook.Application
Dim MItem As Object
Dim Recipient As String, Subj As String
Dim Msg As String, Fname As String

' Message details
Recipient = "myboss@xrediyh.com"
Subj = "Sales figures"
Msg = "Hey boss, here's the PDF file you wanted."
Msg = Msg & vbNewLine & vbNewLine & "-Frank"
Fname = Application.DefaultFilePath & "\" & _
ActiveWorkbook.Name & ".pdf"

' Create the attachment
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=Fname

' Create Outlook object
Set OutlookApp = New Outlook.Application

' Create Mail Item and send it
Set MItem = OutlookApp.CreateItem(olMailItem)
With MItem
.To = Recipient
.Subject = Subj
.Body = Msg
.Attachments.Add Fname
.Save 'to Drafts folder
'.Send
End With
Set OutlookApp = Nothing

' Delete the file
Kill Fname
End Sub


Maybe somebody can change it so that it works with a work doc.

Thanks in advance

Tinbendr
09-23-2010, 02:13 PM
This (http://www.vbaexpress.com/forum/archive/index.php/t-24167.html) was what I found here on the board.

If you have the MS add-in, then there might be something on MS site.

fhimage
07-18-2013, 02:30 AM
of course, you can convert word to pdf vb.net (http://www.rasteredge.com/how-to/vb-net-imaging/word-convert-pdf/), here are some codes you can refer to.
Imports System.IO
Imports System.Drawing.Printing
Imports RasterEdge.Imaging
Imports RasterEdge.Imaging.Processing
Imports RasterEdge.Imaging.Conversion

Dim PDF As New RasterEdgeImaging()

Public Sub WordConvertToPdf()
If True Then
WordInputFile = ("C:/1.docx")
ImageOutputFile = ImageFormat.pdf
End If
End Sub
doc.Save(@"C:/1.docx", 1, @"C:/1.pdf")

.net image sdk (http://www.rasteredge.com/dotnet-imaging/)is your best choice.

fumei
07-21-2013, 05:59 PM
Well for your Excel code you have: ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF,

This is what saves it as PDF. I do not know if 2003 has the same option, but if it does, then do the same thing.