#1
|
|||
|
|||
Convert MS-Word document to PDF and email
Below is the code that I am working with. The code works fine. The macro will save the file and prepare an email to be send with an attachment in MS-Word format.
I would like to revise the code so that after the user clicks a button called 'convert', the document will be saved as a pdf and an email will be prepared similar to the manner of the current code except the attachment will be a pdf format. I do not need to view the pdf prior to it being sent. The only change to the code is for the attachment to be saved as a pdf prior to the email being prepared. I am using word 365 pro plus. I do not have any sort of MS-Word to PDF converter. To convert the document, I choose 'pdf' as file type when saving the MS-Word file. Thanks. Here is the code: Code:
Private Sub CommandButton1_Click() Dim olkApp As Object Dim strTo As String Dim strBody As String Dim strAtt As String Dim strSubject As String Dim Opara As Range Dim LngPara As Long For LngPara = 1 To ActiveDocument.Paragraphs.Count Set Opara = ActiveDocument.Paragraphs(LngPara).Range If Left(LCase(Opara.Text), 3) = "re:" Then Opara.End = Opara.End - 1 Opara.MoveStartUntil Chr(32) Opara.Start = Opara.Start + 1 strSubject = Opara.Text Exit For End If Next LngPara strBody = "Please see attached file. If you have any questions, please do not hesitate to contact me." strTo = "any@any.com" If ActiveDocument.FullName = "" Then MsgBox "activedocument not saved, exiting" Exit Sub Else If MsgBox("Save Document?", vbYesNo, "Error") <> vbYes Then Exit Sub End If strAtt = ActiveDocument.FullName Set olkApp = CreateObject("outlook.application") With olkApp.createitem(0) .To = strTo .Subject = strSubject .body = strBody .attachments.Add strAtt '.send .Display End With Set olkApp = Nothing End Sub Last edited by macropod; 04-03-2018 at 06:41 PM. Reason: Added code tags |
#2
|
||||
|
||||
Simply change:
Code:
If ActiveDocument.FullName = "" Then MsgBox "activedocument not saved, exiting" Exit Sub Else If MsgBox("Save Document?", vbYesNo, "Error") <> vbYes Then Exit Sub End If strAtt = ActiveDocument.FullName Code:
With ActiveDocument If .FullName = "" Then MsgBox "activedocument not saved, exiting" Exit Sub ElseIf .Saved = False Then If MsgBox("Save Document?", vbYesNo, "Error") <> vbYes Then Exit Sub .Save End If strAtt = Split(.FullName, ".doc")(0) & ".pdf" .SaveAs FileName:=strAtt, FileFormat:=wdFormatPDF, AddToRecentFiles:=False End With PS: When posting code, please use the code tags, indicated by the # button on the posting menu. Without them, your code loses much of whatever structure it had.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Convert MS-Word document to PDF and email
Thanks for the quick response. My issue is resolved. I have an additional question relating to the conversion.
I created a legacy control box that the user will click to start the MS-Word to Pdf conversion. Is it possible to remove the control box from appearing on the pdf document or will I need the user need to utilize a keyboard shortcut to run the macro? |
#4
|
||||
|
||||
I would opt for a keyboard shortcut. Apart from anything else, ActiveX controls don't work on Macs - I don't know whether that might be an issue for your environment.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
Tags |
email, pdf |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Why is a Word document coming up as this? How do I convert? | zooeyhall | Word | 2 | 10-01-2015 06:22 AM |
Convert word document to pdf | Catty | Word VBA | 1 | 12-11-2013 03:57 PM |
Convert a word document in an empty one with backgrounds | Esgrimidor | Word VBA | 11 | 12-03-2012 03:44 PM |
PDF to Word(.doc) document not convert correctly | gonner | Word | 1 | 09-10-2011 03:25 AM |
Convert word document to JPEG. The word document may contain headerfooters | vijayaram | Word | 1 | 12-30-2009 08:25 AM |