+ Reply to Thread
Results 1 to 2 of 2

Office Script code to print to PDF

Hybrid View

  1. #1
    Registered User
    Join Date
    08-31-2023
    Location
    andorra
    MS-Off Ver
    365
    Posts
    1

    Office Script code to print to PDF

    Hi, I'm getting crazy trying to find a solution. I'm working on a share excel on micro 365 for team work, and on this excel we sant to print by clicking a button, just to print to pdf and save in the computer.

    As I know this version doesn't work with VB, just basic java script and I cant manage it to find the code to just print a selection of cells.

    Thank you very much and aprreciate for any help.
    Last edited by 6StringJazzer; 08-31-2023 at 08:21 AM. Reason: better titles please

  2. #2
    Valued Forum Contributor ranman256's Avatar
    Join Date
    07-29-2012
    Location
    Kentucky
    MS-Off Ver
    Excel 2003
    Posts
    1,177

    Re: Office Script code to print to PDF

    paste this code into a module ,
    then select the cells to save,
    then run: SaveBlock2Pdf() (put it on the QuickAccess toolbar)

    Option Explicit
    
    Sub SaveBlock2Pdf()
    Dim vFile
          
          'set print area of data to save
     ActiveSheet.PageSetup.PrintArea = Selection.Address
     'ActiveSheet.PageSetup.PrintArea = "$B$22:$D$29"
        
    vFile = UserPick1File()
    If vFile = "" Then Exit Sub
    
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=vFile, _
            Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
            :=False, OpenAfterPublish:=True
       ' ActiveWindow.SmallScroll Down:=3
    End Sub
    
    
    
    
    Public Function UserPick1File(Optional pvPath)
    Dim strTable As String
    Dim strFilePath As String
    Dim sDialogMsg As String, sDecr  As String, sExt As String
    Const msoFileDialogViewList = 1
    Const msoFileDialogSaveAs = 2
    Const msoFileDialogFilePicker = 3
    
    'getFilterTxt pvFilter, sDecr, sExt, sDialog
    If IsMissing(pvPath) Then pvPath = getMyDocs()
    
    ''SetFileFilter pvFilter, sDecr, sExt, sDialogMsg
    
    'Application.FileDialog(msoFileDialogSaveAs) =2     'SAVE AS
    'Application.FileDialog(msoFileDialogFilePicker) =3  'file OPEN
    
    With Application.FileDialog(msoFileDialogSaveAs)   'REFERENCE not needed now : Microsoft Office XX.0 Object Library
        .AllowMultiSelect = True
        .Title = sDialogMsg   ' "Locate a file to Import"
        .ButtonName = "Save As"
        
         Dim lFilterIndex As Long
    
          For lFilterIndex = 1 To .Filters.Count
            'Debug.Print lFilterIndex, .Filters(lFilterIndex).Description
            
                  'get pdf format from type filter
               If InStr(.Filters(lFilterIndex).Description, "PDF") > 0 Then
                   .FilterIndex = lFilterIndex
                   Exit For
               End If
           Next
            
        .InitialFileName = pvPath
        .InitialView = msoFileDialogViewList    'msoFileDialogViewThumbnail
    
            If .Show = 0 Then
               'There is a problem
               Exit Function
            End If
    
        'Save the first file selected
        UserPick1File = Trim(.SelectedItems(1))
    End With
    End Function
    
    
    
    
    Public Function getMyDocs()
    Dim vDir, vUsr
    
    On Error GoTo errDocs
    vUsr = Environ("UserProfile")
    vDir = vUsr & "\Documents\"
    If Not DirExists(vDir) Then
        vDir = vUsr & "\My Documents\"
        'If Not DirExists(vDir) Then
        '   vDir = "c:\temp"
        '   MakeDir vDir
        'End If
    End If
    getMyDocs = vDir
    Exit Function
    errDocs:
    MsgBox "Cannot find temp folder", vbInformation, "getMyDocs():" & Err
    End Function
    
    
    
    Public Function DirExists(ByVal pvDir) As Boolean
    Dim fso
    On Error Resume Next
    Set fso = CreateObject("Scripting.FileSystemObject")
    DirExists = fso.FolderExists(pvDir)
    Set fso = Nothing
    End Function

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to use script to print the specific sheet of Excel?
    By woshichuanqilz in forum Excel General
    Replies: 1
    Last Post: 07-08-2017, 02:25 PM
  2. VB script to print value in field
    By krappleby1976 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-23-2015, 07:23 AM
  3. [SOLVED] script to set print area
    By MATT.B in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-05-2014, 07:49 PM
  4. Excel script to print URLs
    By hudwink in forum Excel General
    Replies: 1
    Last Post: 10-20-2011, 12:45 PM
  5. macro script for IF & print functions
    By rhua5436 in forum Excel General
    Replies: 6
    Last Post: 04-14-2011, 11:11 AM
  6. Create VBA script to short, dedicated button in excel for script?
    By realized in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-01-2009, 11:54 PM
  7. can't print excel sheet using wsh script
    By onedollar in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-10-2006, 11:10 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1