Results 1 to 11 of 11

Adapting VBA from Word to work in Excel

Threaded View

  1. #1
    Registered User
    Join Date
    12-16-2013
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    8

    Adapting VBA from Word to work in Excel

    Hello, I am a total novice when it comes to VBA. I have been using a macro in Word created from pieces all over the internet (credit to a user named matt198992 for recursion script). The code prompts a user for folder, then runs a macro called Publish as PDF to all word files in the folders/subfolders.

    I want to adapt the code in Excel, but im having trouble. Error on the line "Workbooks.Open Filename:="Path & DirN". All help would be truly appreciated. Thank you.

    Also posted on http://www.mrexcel.com/forum/excel-q...ml#post3697882



    Sub BatchExceltoPDF()
        
         Dim strFolder As String
     Set fd = Application.FileDialog(msoFileDialogFolderPicker)
     With fd
         .Title = "Select the folder to Convert."
         If .Show = -1 Then
             strFolder = .SelectedItems(1) & "\"
             
            Application.Run "personal.xls!Recurrer", (strFolder)
             
         Else
             MsgBox "You did not select a folder"
             strFolder = ""
         End If
     End With
    
        End Sub
    
        Sub Recurrer(Path As String)
    
            Dim DirN        As String
            Dim DirList()   As String
            Dim ndx         As Long
            Dim pos         As Long ' added
           
            ' Add vbSystem, vbHidden, etc., if you want such files
            DirN = Dir(Path, vbDirectory)
           
            Do While DirN <> ""
                If DirN = "." Or DirN = ".." Then
                    ' Ignore
                Else
                    If (GetAttr(Path & DirN) And vbDirectory) = vbDirectory Then
                        If (Not DirList) = True Then
                            ReDim DirList(0 To 0)
                        Else
                            ReDim Preserve DirList(0 To UBound(DirList) + 1)
                        End If
                        DirList(UBound(DirList)) = DirN
                    Else
                        ' DirN has a file name
                        pos = InStrRev(DirN, ".")
                        If pos > 0 Then
                            If InStr("xls xlsx xlsm", LCase(Right$(DirN, Len(DirN) - pos))) Then
                                ' The file is a xls, xlsx or xlsm
                                ' Do whatever with it
                                
                             
        Workbooks.Open Filename:="Path & DirN"
                             
        Application.Run "personal.xls!PublishasPDF"
        
        ActiveWorkbook.Close
    
    
                             
                             
                            End If
                        End If
                    End If
                End If
               
                DirN = Dir ' This just gets the next name before going round again
               
            Loop
           
            ' Now process the saved subdirectories
            If (Not DirList) = True Then
            Else
                For ndx = 0 To UBound(DirList)
                    Recurrer Path & DirList(ndx) & Application.PathSeparator
                Next
            End If
           
        End Sub
    
    Sub PublishasPDF()
    '
    ' PublishasPDF Macro
    Dim strName As String
    
    
    With ActiveWorkbook
      strName = .FullName
      strName = Left(strName, InStrRev(strName, ".")) & "pdf"
     ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strName, Quality:=xlQualityStandard, _
            IncludeDocProperties:=False, IgnorePrintAreas:=False, OpenAfterPublish:= _
            False
            
        End With
        
       End Sub
    Last edited by scapegoat9595; 01-26-2014 at 11:49 PM. Reason: amend with link to same post on other site

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Open Word Work Order Template with excel generated work order #
    By Tivka in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-26-2012, 02:24 PM
  2. Get excel to work with Word?
    By Niffe783 in forum Excel General
    Replies: 1
    Last Post: 02-10-2011, 11:06 AM
  3. Adapting Bubblesort to work with Long datatype array
    By Jeroen1000 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 09-30-2009, 08:26 AM
  4. Getting Word to work with Excel
    By mike.wasson@gmail.com in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-07-2006, 08:20 AM
  5. Replies: 1
    Last Post: 02-18-2005, 11:06 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