Print a selected range of cells as pdf in a folder on the desktop Office 2011 - MAC

DTest

New Member
Joined
Sep 11, 2014
Messages
6
Hello everyone.


I need a vba command that when activated it does the following:


1) Saves a selected range of cells, in this case it is on a sheet call Reports in cells A1:I93, as a pdf on the desktop in a folder titled Test.


2) The name of the PDF is in cell J86 also on the Reports worksheet


3) All of the options for creating the pdf should be their defaults. So all I have to do is click the button and the pdf is created.


The trick is that it has to work on a MAC with Office 2011


Thanks again.
 
.
.

This is how you would do it in Windows. (A mac use will need to show you how to adapt it.)

Code:
Private Sub CommandButton1_Click()
  'Requires reference to Windows
  'Script Host Object Model
  
  Dim objWshShell As WshShell
  Dim strDesktopPath As String
  Dim rngExportRange As Range
  Dim strExpFileName As String
  
  'Get desktop path
  Set objWshShell = New WshShell
  strDesktopPath = objWshShell.SpecialFolders("Desktop")
  Set objWshShell = Nothing
  
  'Make test folder
  On Error Resume Next
  MkDir strDesktopPath & Application.PathSeparator & "Test"
  On Error GoTo 0
  
  'Set export range
  Set rngExportRange = Me.Range("A1:I93")
  
  'Set export filename
  strExpFileName = strDesktopPath & Application.PathSeparator & "Test" & _
    Application.PathSeparator & Me.Range("J86").Value & ".pdf"
  
  'Perform export
  rngExportRange.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=strExpFileName, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=False, _
    IgnorePrintAreas:=True, _
    OpenAfterPublish:=False
End Sub
 
Upvote 0
Or similarly,

Code:
Private Sub CommandButton1_Click()
  Dim sSep          As String
  Dim sPath         As String
  Dim sFile         As String

  sSep = Application.PathSeparator

  sPath = CreateObject("Wscript.Shell").SpecialFolders("Desktop") & sSep & _
          "Test" & sSep
  If Len(Dir(sPath, vbDirectory)) = 0 Then MkDir sPath

  sFile = Range("J86").Value & ".pdf"
  Range("A1:I93").ExportAsFixedFormat Type:=xlTypePDF, _
                                      Filename:=sPath & sFile, _
                                      Quality:=xlQualityStandard, _
                                      IncludeDocProperties:=False, _
                                      IgnorePrintAreas:=True, _
                                      OpenAfterPublish:=False
End Sub
 
Upvote 0
This worked on my Mac. You'll have to adjust the path to the folder to match your situation.
Code:
Sub test()
    With ThisWorkbook.Sheets("Reports")
        .Range("a1:I93").ExportAsFixedFormat xlTypePDF, "Macintosh HD:Users:michaelerickson:Desktop:Test:" & .Range("J86").Value
    End With
End Sub
 
Upvote 0

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