Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-03-2018, 06:26 PM
naeemakhtar naeemakhtar is offline Convert MS-Word document to PDF and email Windows 10 Convert MS-Word document to PDF and email Office 2016
Novice
Convert MS-Word document to PDF and email
 
Join Date: Apr 2016
Posts: 8
naeemakhtar is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 04-03-2018, 06:54 PM
macropod's Avatar
macropod macropod is offline Convert MS-Word document to PDF and email Windows 7 64bit Convert MS-Word document to PDF and email Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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
to:
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
(I've added a couple of minor enhancements)

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]
Reply With Quote
  #3  
Old 04-03-2018, 07:06 PM
naeemakhtar naeemakhtar is offline Convert MS-Word document to PDF and email Windows 10 Convert MS-Word document to PDF and email Office 2016
Novice
Convert MS-Word document to PDF and email
 
Join Date: Apr 2016
Posts: 8
naeemakhtar is on a distinguished road
Default 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?
Reply With Quote
  #4  
Old 04-03-2018, 07:46 PM
macropod's Avatar
macropod macropod is offline Convert MS-Word document to PDF and email Windows 7 64bit Convert MS-Word document to PDF and email Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
Reply

Tags
email, pdf



Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert MS-Word document to PDF and email Why is a Word document coming up as this? How do I convert? zooeyhall Word 2 10-01-2015 06:22 AM
Convert MS-Word document to PDF and email Convert word document to pdf Catty Word VBA 1 12-11-2013 03:57 PM
Convert MS-Word document to PDF and email Convert a word document in an empty one with backgrounds Esgrimidor Word VBA 11 12-03-2012 03:44 PM
Convert MS-Word document to PDF and email PDF to Word(.doc) document not convert correctly gonner Word 1 09-10-2011 03:25 AM
Convert MS-Word document to PDF and email Convert word document to JPEG. The word document may contain headerfooters vijayaram Word 1 12-30-2009 08:25 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:17 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft