Advertisement
Guest User

Untitled

a guest
Feb 12th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub BulkConvertDocToPDF()
  2.     Dim oFileDlg As FileDialog
  3.     Dim strFolder As String
  4.     Dim strFileName As String
  5.     Dim oDoc As Document
  6.     Dim rsp As VbMsgBoxResult
  7.    
  8.     Application.ScreenUpdating = False
  9.    
  10.     ' Tell user what's happening
  11.    rsp = MsgBox( _
  12.         prompt:="Convert all documents in a folder to PDF format? PLEASE BACK UP YOUR WORD DOCUMENTS IN CASE THEY GET CORRUPTED BEFORE PROCEEDING " & _
  13.             vbCr & "If yes, select the folder in the next dialog.", _
  14.         buttons:=vbYesNo + vbExclamation, _
  15.         Title:="Bulk Convert to PDF")
  16.     If rsp = vbYes Then
  17.         ' Prepare and show folder picker dialog
  18.        Set oFileDlg = Application.FileDialog(msoFileDialogFolderPicker)
  19.         With oFileDlg
  20.             .Title = "Bulk Convert to PDF"
  21.             .AllowMultiSelect = False
  22.             ' Start in user's Documents folder
  23.            .InitialFileName = Application.Options.DefaultFilePath(wdDocumentsPath)
  24.             If .Show = -1 Then
  25.                 ' User clicked OK; get selected path
  26.                strFolder = .SelectedItems(1) & "\"
  27.             End If
  28.         End With
  29.         ' Remove dialog object from memory
  30.        Set oFileDlg = Nothing
  31.     End If
  32.    
  33.     If Not strFolder = "" Then
  34.         strFileName = Dir(pathname:=strFolder & "*.doc*")
  35.         WordBasic.DisableAutoMacros 1   'Disables auto macros
  36.        Application.ScreenUpdating = False
  37.        
  38.         While strFileName <> ""
  39.             ' Set an error handler
  40.            On Error Resume Next
  41.            
  42.             ' Attempt to open the document
  43.            Set oDoc = Documents.Open( _
  44.                 FileName:=strFolder & strFileName, _
  45.                 PasswordDocument:="?#nonsense@$")
  46.            
  47.             ' Check for error that indicates password protection
  48.            Select Case Err.Number
  49.                 Case 0
  50.                     ' Document successfully opened
  51.                    ' Do nothing here
  52.                Case 5408
  53.                      ' Document is Password-protected and was NOT Opened
  54.                    Debug.Print strFileName & " is password-protected " & _
  55.                         "and was NOT processed."
  56.                     ' Clear Error Object and Disable Error Handler
  57.                    Err.Clear
  58.                     On Error GoTo 0
  59.                     ' Get Next Document
  60.                    GoTo GetNextDoc
  61.    
  62.                 Case Else
  63.                     ' Another Error Occurred
  64.                    MsgBox "Error " & Err.Number & vbCr & Err.Description
  65.             End Select
  66.            
  67.             ' Change extension from .doc* to .pdf
  68.            strFileName = Replace(LCase(strFileName), ".doc", ".pdf")
  69.             If Right(strFileName, 1) = "x" Or Right(strFileName, 1) = "m" Then
  70.                 strFileName = Left(strFileName, Len(strFileName) - 1)
  71.             End If
  72.            
  73.             ' Save the file in PDF format
  74.            oDoc.SaveAs2 FileName:=strFolder & strFileName, FileFormat:=wdFormatPDF
  75.            
  76.             ' Close the document and clear the object
  77.            oDoc.Close SaveChanges:=wdDoNotSaveChanges
  78.             Set oDoc = Nothing
  79.            
  80. GetNextDoc:
  81.             ' Get the next file name
  82.            strFileName = Dir()
  83.         Wend
  84.     End If
  85.     WordBasic.DisableAutoMacros 0   'Enables auto macros
  86.    Application.ScreenUpdating = True
  87. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement