+ Reply to Thread
Results 1 to 11 of 11

Adapting VBA from Word to work in Excel

Hybrid 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

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2411
    Posts
    26,726

    Re: Adapting VBA from Word to work in Excel

    You are trying to open a file named "Path & DirN" instead of using the variables Path and DirN to build a file name. Maybe try this change to that line of code:
                                
       Workbooks.Open Filename:=Path & "\" & DirN
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

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

    Re: Adapting VBA from Word to work in Excel

    Thank you for your input. I receive the same error still. If I change it to Workbooks.Open Filename:="\" & Path & DirN the error states "\C:\ExcelTest\Testworkbook1.xls" cannot be found (cause of the \ in front of the drive), so why wouldnt Workbooks.Open Filename:=Path & DirN work? Seems like it should.

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2411
    Posts
    26,726

    Re: Adapting VBA from Word to work in Excel

    See comment below:
    Last edited by 6StringJazzer; 01-26-2014 at 09:07 PM.

  5. #5
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2411
    Posts
    26,726

    Re: Adapting VBA from Word to work in Excel

    My bad. I didn't read the code carefully enough. The code I provided is suitable for ThisWorkbook.Path. But you are using a local variable called Path that has the delimiter appended. (That is why I never use a variable name that is the same as a built-in object method.)

    Anyway, for your code, the correct line of code is, as you say:

       Workbooks.Open Filename:=Path & DirN
    I am guessing you have already tried it by now and found it works.

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

    Re: Adapting VBA from Word to work in Excel

    Thank you for your assistance. Workbooks.Open Filename:=Path & DirN should work but it is not. I am so perplexed by this. As I said im a total novice though i felt i was getting somewhere and this has stumped me all weekend.This part of the code works in Word, must be complicated to adapt to Excel.
    Is there a better way to achieve what Im after, Folder Browser> Run Macro on all .xls files in Selected Folder and SubFolders? Im must be wrong somewhere. So stumped. Again thank you for your input.

  7. #7
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2411
    Posts
    26,726

    Re: Adapting VBA from Word to work in Excel

    First--I should have said this right away--when asking help for an error, always give the error message that you see. "should work but it is not" What does "not work" mean in this case?

    This should work the same in Excel as it does in Word. I would suggest setting a breakpoint on that line of code then when it comes to that line type the following into the Immediate Window:

    print Path & DirN

    and see if the result is a full pathname that you expect.

    Setting a breakpoint: Select the line of code and hit F9
    Open the immediate window: Hit CTRL+g

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

    Re: Adapting VBA from Word to work in Excel

    Quote Originally Posted by 6StringJazzer View Post
    First--I should have said this right away--when asking help for an error, always give the error message that you see. "should work but it is not" What does "not work" mean in this case?

    This should work the same in Excel as it does in Word. I would suggest setting a breakpoint on that line of code then when it comes to that line type the following into the Immediate Window:

    print Path & DirN

    and see if the result is a full pathname that you expect.

    Setting a breakpoint: Select the line of code and hit F9
    Open the immediate window: Hit CTRL+g
    Thank you. The error message I receive is "Run-time error '1004': Method 'Open' of object 'Workbooks' failed".

    I have done what you suggested with the break point, upon entering "print Path & DirN" I get the path as C:\ExcelTest\TestFile.Xls.

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

    Re: Adapting VBA from Word to work in Excel

    Correction: It does work! Just tried with the above file, the one I was working with had an error. All of this time I never thought to try a different folder/file! Thank you for your help. Now I just have to research how to handle broken/errored files.

  10. #10
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Adapting VBA from Word to work in Excel

    Hi, scapegoat9595,

    Your post (#6) does not comply with Rule 8 of our Forum RULES. Do not crosspost your question on multiple forums without including links here to the other threads on other forums.

    Cross-posting is when you post the same question in other forums on the web. The last thing you want to do is waste people's time working on an issue you have already resolved elsewhere. We prefer that you not cross-post at all, but if you do (and it's unlikely to go unnoticed), you MUST provide a link (copy the url from the address bar in your browser) to the cross-post.

    Expect cross-posted questions without a link to be closed and a message will be posted by the moderator explaining why. We are here to help so help us to help you!

    Read this to understand why we ask you to do this, and then please edit your first post to include links to any and all cross-posts in any other forums (not just this site).

    http://www.mrexcel.com/forum/excel-q...ork-excel.html

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

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

    Re: Adapting VBA from Word to work in Excel

    Can I ask one more question: How can I add an underscore at the end of outputted PDF file name?

+ 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. 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