Page 2 of 2 FirstFirst 1 2
Results 11 to 15 of 15

Thread: VBA required to convert WORD to PDF for folders and its subfolders

  1. #11
    Seeker amb2301's Avatar
    Join Date
    May 2020
    Posts
    15
    Articles
    0
    Excel Version
    excel 2013


    Register for a FREE account, and/
    or Log in to avoid these ads!

    Quote Originally Posted by amb2301 View Post
    Hi NormS,
    i need a small modification in the code, with your latest script everything is working fine,
    Actually all the converted pdf files are saved in the same path (D:/TEST/testfiles) as per the range given in E14 cell,
    even the sub-folders converted files also getting saved into same path (D:/TEST/testfiles).

    Now instead of that, converted pdf files to be saved in the respective sub-folders itself....
    could you please help me..

    Thanks in Advance.

    Hi NormS,
    I Tried modified this line, it worked as expected
    HTML Code:
      worddoc.ExportAsFixedFormat fo.Path & Application.PathSeparator & VBA.Replace(f.Name, ".docx", ".pdf"), wdExportFormatPDF

  2. #12
    Acolyte macropod's Avatar
    Join Date
    Mar 2017
    Posts
    57
    Articles
    0
    Excel Version
    2010
    Quote Originally Posted by amb2301 View Post
    Now instead of that, converted pdf files to be saved in the respective sub-folders itself....
    The code I posted at ExcelForum already does that... I see no evidence you've even bothered to try that code.
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #13
    Conjurer NormS's Avatar
    Join Date
    Jul 2017
    Posts
    119
    Articles
    0
    Excel Version
    Excel 2016 ProPlus
    Could you cross-post your Word VBA solution here, Paul, and save me the trouble of creating an account on Excelforum?

  4. #14
    Seeker amb2301's Avatar
    Join Date
    May 2020
    Posts
    15
    Articles
    0
    Excel Version
    excel 2013
    Quote Originally Posted by macropod View Post
    The code I posted at ExcelForum already does that... I see no evidence you've even bothered to try that code.
    i m extremely sorry macropod, i just noticed your code there..
    Actually i seen the Admin message(warning message) in excel forum for the cross-post, so i replied admin & missed to see your reply to me...
    Now i just replied to that thread.

    i m sorry for troubling you macropod

    Hi NormS,
    here is the link & thank you too for your codes, you guys are great
    https://www.excelforum.com/excel-pro...ml#post5342800

    HTML Code:
    Option Explicit 
    'Re-used & shared variables
    Dim FSO As Object, oFolder As Object, oSubFolder As Object, oFiles As Object, oItem As Object
      
    Sub CreateDocPDFs()
    Application.ScreenUpdating = False
    Application.DisplayAlerts = wdAlertsNone
    WordBasic.DisableAutoMacros True
    Dim StrFolder As String
    ' Browse for the starting folder
    StrFolder = GetTopFolder
    If StrFolder = "" Then
      MsgBox "No document folder selected", vbExclamation
      Exit Sub
    End If
    ' Search the top-level folder
    Call ProcessFolder(StrFolder & "\")
    ' Search the subfolders for more files
    Call SearchSubFolders(StrFolder)
    ' Return control of status bar to Word
    Application.StatusBar = ""
    WordBasic.DisableAutoMacros False
    Application.DisplayAlerts = wdAlertsAll
    Application.ScreenUpdating = True
    MsgBox "Done!", vbExclamation
    End Sub
      
    Function GetTopFolder() As String
    GetTopFolder = ""
    Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
    If (Not oFolder Is Nothing) Then GetTopFolder = oFolder.Items.Item.Path
    Set oFolder = Nothing
    End Function
      
    Sub SearchSubFolders(strStartPath As String)
    If FSO Is Nothing Then
      Set FSO = CreateObject("scripting.filesystemobject")
    End If
    Set oFolder = FSO.GetFolder(strStartPath)
    Set oSubFolder = oFolder.subfolders
    For Each oFolder In oSubFolder
      Set oFiles = oFolder.Files
      ' Search the current folder
      Call ProcessFolder(oFolder.Path & "\")
      ' Call ourself to see if there are subfolders below
      SearchSubFolders oFolder.Path
    Next
    End Sub
      
    Sub ProcessFolder(StrFolder As String)
    Dim strFile As String, wdDoc As Document
    strFile = Dir(StrFolder & "*.doc")
    ' Process the files in the folder
    While strFile <> ""
      ' Update the status bar is just to let us know where we are
      Application.StatusBar = StrFolder & strFile
      Set wdDoc = Documents.Open(StrFolder & strFile, AddToRecentFiles:=False, ReadOnly:=False, Format:=wdOpenFormatAuto, Visible:=False)
      With wdDoc
        .SaveAs2 FileName:=Split(.FullName, ".doc")(0) & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False
        .Close SaveChanges:=False
      End With
      strFile = Dir()
    Wend
    Set wdDoc = Nothing End Sub
    Last edited by amb2301; 2020-06-03 at 02:41 AM.

  5. #15
    Acolyte macropod's Avatar
    Join Date
    Mar 2017
    Posts
    57
    Articles
    0
    Excel Version
    2010
    Quote Originally Posted by NormS View Post
    Could you cross-post your Word VBA solution here, Paul, and save me the trouble of creating an account on Excelforum?
    There is no need to create an account there just to copy code from the body of a post...
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

Page 2 of 2 FirstFirst 1 2

Posting Permissions

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