Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-13-2017, 04:01 PM
Coco3 Coco3 is offline Saving to Sub Folder Windows 7 64bit Saving to Sub Folder Office 2016
Novice
Saving to Sub Folder
 
Join Date: Oct 2017
Posts: 2
Coco3 is on a distinguished road
Default Saving to Sub Folder

Hi,



I found macro that opens a directory and converts all files to PDF. I have tried unsuccessfully to modify the script to save to a sub folder called "PDF".

The code is as follows:

Code:
Sub SaveAllAsPDF()
Dim strFilename As String
 Dim strDocName As String
 Dim strOutFold As String
 Dim strPath As String
 Dim oDoc As Document
 Dim fDialog As FileDialog
 Dim intPos As Integer
 Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
 With fDialog
     .Title = "Select folder and click OK"
     .AllowMultiSelect = False
     .InitialView = msoFileDialogViewList
     If .Show <> -1 Then
         MsgBox "Cancelled By User", , "List Folder Contents"
         Exit Sub
     End If
     strPath = fDialog.SelectedItems.Item(1)
     If Right(strPath, 1) <> "\" Then strPath = strPath + "\"
 End With
 If Documents.Count > 0 Then
     Documents.Close SaveChanges:=wdPromptToSaveChanges
 End If
 If Left(strPath, 1) = Chr(34) Then
     strPath = Mid(strPath, 2, Len(strPath) - 2)
 End If
 strFilename = Dir$(strPath & "*.doc")
 While Len(strFilename) <> 0
     Set oDoc = Documents.Open(strPath & strFilename)
     strDocName = ActiveDocument.FullName
     intPos = InStrRev(strDocName, ".")
     strDocName = Left(strDocName, intPos - 1)
'This instruction converts to PDF
       strDocName = strDocName & ".pdf"
     oDoc.SaveAs FileName:=strDocName, _
         FileFormat:=wdFormatPDF
     oDoc.Close SaveChanges:=wdDoNotSaveChanges
     strFilename = Dir$()
 Wend
 
End Sub
Any help would be greatly appreciated.

Thanks,
Megan
Reply With Quote
  #2  
Old 10-13-2017, 08:34 PM
gmayor's Avatar
gmayor gmayor is offline Saving to Sub Folder Windows 10 Saving to Sub Folder Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Presumably PDF is a pre-existing sub folder of the original folder in which case

Code:
    While Len(strFilename) <> 0
        Set oDoc = Documents.Open(strPath & strFilename)
        strDocName = ActiveDocument.Name 'Use Name not fullname
        intPos = InStrRev(strDocName, ".")
        strDocName = Left(strDocName, intPos - 1)
        'This instruction converts to PDF
        strDocName = strDocName & ".pdf"
        oDoc.SaveAs FileName:=strPath & "PDF\" & strDocName, _
                    FileFormat:=wdFormatPDF 'add the path
        oDoc.Close SaveChanges:=wdDoNotSaveChanges
        strFilename = Dir$()
    Wend
If you want to create the folder then see the Createfolders function at https://www.gmayor.com/useful_vba_functions.htm where you will also find code to ensure the filenames are unique to avoid overwriting duplicated names.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 10-14-2017, 06:43 AM
Coco3 Coco3 is offline Saving to Sub Folder Windows 7 64bit Saving to Sub Folder Office 2016
Novice
Saving to Sub Folder
 
Join Date: Oct 2017
Posts: 2
Coco3 is on a distinguished road
Default Many thanks!

Thank-you very much!

I actually tried that, but got an error and being a total newbie to this, I thought I was wrong. I checked my folder permissions and I have a read-only permissions problem.

Thank you again for your help. And I'll definitely be checking out your site to learn more about this.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Outlook 2010: Saving attachments opens up wrong windows folder to save in jeroen Outlook 0 09-29-2015 01:51 AM
Saving sent mails to specific folder kammil121 Outlook 0 10-22-2014 02:26 AM
Saving Emails to New Folder along with Attachments thundercats9595 Outlook 2 02-01-2014 12:32 PM
Saving to Sub Folder Merging and saving to folder - assistance EvilEdna Mail Merge 1 03-26-2012 08:45 PM
Select Folder for saving in to andywal6 Word 0 06-18-2010 01:30 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:55 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft