Trying to use existing code to create pdf of userform and attach to an Outlook email

sspatriots

Well-known Member
Joined
Nov 22, 2011
Messages
585
Office Version
  1. 365
Platform
  1. Windows
I have this code below that sizes up my userform nicely and temporarily places it in a network folder so it can be grabbed and displayed in an e-mail. Now, I'm being asked to do the same thing, except attach the a .pdf of that .jpg file to an email as an attachment. I was hoping it would be as simple as changing the extension in the code below, but no luck there. Any help would be greatly appreciated. Thanks, SS


VBA Code:
Sub EmailWithPicture()

    Dim ol As Outlook.Application
    Dim mi As Outlook.MailItem
    Dim doc As Word.Document
    Dim shp As Word.InlineShape
   
    Set ol = New Outlook.Application
    Set mi = ol.CreateItem(olMailItem)
   
    mi.Display
    'mi.To = "someone@somewhere.com"
    mi.Subject = "Job Status: " & Sheets("Quick Search Job Status").Range("B3").Value 'txtJobName.Text'"Pictures"
   
    Set doc = mi.GetInspector.WordEditor
   
    Set shp = doc.Range(0, 0).InlineShapes.AddPicture("\\Documents\PROJECTS\Job List\Test.jpg")
   
    shp.LockAspectRatio = msoTrue
    shp.Width = 600
   
    shp.Glow.Color.RGB = RGB(255, 0, 0)
    shp.Glow.Radius = 10
    shp.Glow.Transparency = 0.5
    'shp.Reflection.Type = msoReflectionType3
    shp.Borders.OutsideLineStyle = wdLineStyleDashDot
    shp.Borders.OutsideLineWidth = wdLineWidth225pt
   
    'doc.Range(0, 0).InsertBefore _
    '    "Please find the current Job Status below:" & vbNewLine & vbNewLine & "" & vbNewLine & vbNewLine


End Sub



I also have the code below that will print the same userform to a printer and size it correctly. Wasn't sure if it would be better to try and modify it to make it print to a pdf and then have another macro that grabs that file off of the network and attaches it to an e-mail.

VBA Code:
Sub PrintJobStatus()
Dim sShape As Picture
Set sShape = Worksheets("Temp").Pictures.Insert("\\Documents\PROJECTS\Job List\Test.jpg")
    
    With sShape
        .ShapeRange.LockAspectRatio = msoTrue    '<---- Lock the original width/height ratio
        .Left = 0   '<---- Very left of sheet
        .Top = 0    '<---- Very top of sheet
        .Width = Columns(24).Left    '<---- 9 Columns wide
        .Name = "Picture 1"
    End With
    
    With ActiveSheet.PageSetup
        .PrintTitleRows = ""
        .PrintTitleColumns = ""
        .LeftMargin = Application.CentimetersToPoints(2#)
        .RightMargin = Application.CentimetersToPoints(0.5)
        .TopMargin = Application.CentimetersToPoints(1.5)
        .BottomMargin = Application.CentimetersToPoints(0.5)
        .HeaderMargin = Application.CentimetersToPoints(0.2)
        .FooterMargin = Application.CentimetersToPoints(0.2)
        .PaperSize = xlPaperLetter
        .Orientation = xlPortrait 'xlLandscape
        .Zoom = False
        .FitToPagesWide = 1
        .FitToPagesTall = 1
    End With
    
    ActiveSheet.Range("A1:W72").PrintOut
    'ActiveSheet.Range("A1:W45").PrintPreview
    
    'or    'ActiveSheet.Cells(1, 1).Resize(ActiveSheet.Shapes("Picture 1").BottomRightCell.Row, ActiveSheet.Shapes("Picture 1").BottomRightCell.Column).PrintOut

    'or    'ActiveSheet.Cells(1, 1).Resize(ActiveSheet.Shapes("Picture 1").BottomRightCell.Row, ActiveSheet.Shapes("Picture 1").BottomRightCell.Column).PrintPreview

End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
I've decided that it may be easier to break this up into two parts, and work from the "Test.jpg" file that is created by the first code I posted above. So trying to take that "Test.jpg" file and convert it to a pdf file saved as "Test.pdf". That code is below, however the output file is going to my "Documents" folder on my C drive. Hoping someone can tell me why it is going there and how to correct it to go to the same folder the originating "Test.jpg" file is in.

VBA Code:
Sub JPG_PDF()
'
' JPG_PDF Macro
'
Application.ScreenUpdating = False


'Declare variables
Dim file As String    'added "As String", SPS, 06/30/22
Dim path As String


path = "\\abc.local\drives\Manufacturing\PROJECTS\-- NEW - Commercial Group 2 Job List\"
file = Dir(path & "Test.jpg")


Worksheets("Quick Search Job Status").Activate


'Insert picture into Excel
Worksheets("Quick Search Job Status").Pictures.Insert (path & file)
ActiveSheet.Pictures(ActiveSheet.Pictures.Count).Name = "A Picture"

ChDir "\\abc.local\drives\Manufacturing\PROJECTS\-- NEW - Commercial Group 2 Job List\"


Application.PrintCommunication = False

With ActiveSheet.PageSetup
    .Orientation = xlPortrait
    '.PrintArea = "$A$1:$N$54"
    .PrintArea = Sheet5.UsedRange
'    .PrintTitleRows = ActiveSheet.Rows(5).Address
    .Zoom = 100   'False
    .FitToPagesTall = 1               'False
    .FitToPagesWide = 1
    .LeftMargin = Application.InchesToPoints(0.8)
    .RightMargin = Application.InchesToPoints(0.2)
End With

Application.PrintCommunication = True

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=file, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False

ActiveSheet.Shapes.Range(Array("A Picture")).Delete


file = Dir()


Application.ScreenUpdating = True


End Sub


When I'm finished with this part, I'll have another code that just grabs the "Test.pdf" file from that folder and attaches it to an email.
 
Upvote 0
Disregard the last post. I figured it out. I moved the file name and path up to the part of the code that says ", Filename:=\\abc.local..."
 
Upvote 0
I have a solution to my original post and it seems to be working. Probably not idea, but works. When I get it cleaned up, I'll post here for others to benefit. Happy 4th everyone.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top