+ Reply to Thread
Results 1 to 3 of 3

How to improve this word-macro code

Hybrid View

  1. #1
    Forum Contributor IonutC's Avatar
    Join Date
    01-28-2016
    Location
    Bucharest, Romania
    MS-Off Ver
    Office 2019
    Posts
    486

    How to improve this word-macro code

    Hi guys,

    I have a macro code that export all the words files from a folder into pdf.

    I shared the code with other peers and it seems that for them it doesn't work. If they have 10 words only 3 of them are exported as pdf and 1 from those 3 has an odd name.
    ***All of those 10 files are .docx files.

    If is possible, can you please advise what I've done wrong?

    Also is there a possibility to change the destination folder of the saved files? Currently the code exports all the pdfs into C/Documents. This can be changed?

    Ps. For me this code is working perfectly.

    Thank you in advance,
    Ionut

    Sub ConvertWordsToPdfs()
    
        Dim directory   As Variant
        Dim file        As Object
        Dim files       As Object
        Dim folder      As Variant
        Dim n           As Long
        Dim newName     As String
        Dim wdDoc       As Document
    
            With Application.FileDialog(msoFileDialogFolderPicker)
                .Show
                If .SelectedItems.Count = 0 Then Exit Sub
                directory = .SelectedItems(1)
            End With
    
            With CreateObject("Shell.Application")
                Set folder = .Namespace(directory)
                Set files = folder.Items
                files.Filter 64, "*.docx"
            End With
    
                For Each file In files
                    n = InStrRev(file.Name, ".")
                    newName = Left(file.Name, n) & "pdf"
    
                    Set wdDoc = Documents.Open(file.Path)
    
                    wdDoc.SaveAs2 FileName:=newName, FileFormat:=wdFormatPDF
    
                    wdDoc.Close SaveChanges:=False
                Next file
    
    End Sub
    Last edited by IonutC; 06-28-2016 at 04:24 AM.
    Please consider:
    Be polite. Thank those who have helped you.
    Click the star icon in the lower left part of the contributor's post and add Reputation. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .
    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.
    Posting code between tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

  2. #2
    Forum Contributor IonutC's Avatar
    Join Date
    01-28-2016
    Location
    Bucharest, Romania
    MS-Off Ver
    Office 2019
    Posts
    486

    Re: How to improve this word-macro code

    Guys,

    Any help would be much appreciated here!

  3. #3
    Forum Contributor IonutC's Avatar
    Join Date
    01-28-2016
    Location
    Bucharest, Romania
    MS-Off Ver
    Office 2019
    Posts
    486

    Re: How to improve this word-macro code

    Sub ConvertWordsToPdfs()
    
        Dim directory   As Variant
        Dim file        As Object
        Dim files       As Object
        Dim folder      As Variant
        Dim n           As Long
        Dim newName     As String
        Dim wdDoc       As Document
        Dim Path       As String, FileFormat As String, filename As String
            With Application.FileDialog(msoFileDialogFolderPicker)
                .Show
                If .SelectedItems.Count = 0 Then Exit Sub
                directory = .SelectedItems(1)
            End With
    
            With CreateObject("Shell.Application")
                Set folder = .Namespace(directory)
                Set files = folder.Items
                files.Filter 64, "*.docx"
            End With
    
                For Each file In files
                    n = InStrRev(file.Name, ".")
                    newName = Left(file.Name, n) & "pdf"
    
                    Set wdDoc = Documents.Open(file.Path)
                
                    wdDoc.SaveAs2 directory & "\" & newName, FileFormat:=17
                  
                    wdDoc.Close SaveChanges:=False
                Next file
    
    End Sub

+ 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. Improve my code: If, or, then
    By Alex532 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-01-2013, 07:19 PM
  2. How to improve the running speed of this VBA macro code?
    By sellim in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-24-2012, 01:45 PM
  3. [SOLVED] To improve Efficiency of code, code running too long
    By andywsw in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-19-2012, 05:54 PM
  4. Replies: 2
    Last Post: 02-10-2010, 12:41 PM
  5. [SOLVED] Anyone help to improve my code?
    By Andrew in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-05-2006, 10:15 AM
  6. [SOLVED] Improve consistency in how Excel and Word 2003 close.
    By Florence Moosengale in forum Excel General
    Replies: 5
    Last Post: 11-29-2005, 08:30 PM
  7. Re: Improve code
    By rjamison in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-13-2005, 08:05 PM
  8. [SOLVED] Improve code
    By Gareth in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-20-2005, 11:06 AM

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