+ Reply to Thread
Results 1 to 8 of 8

Bug in Excel 2003 Application.Run with dashes in filenames

Hybrid View

  1. #1
    Registered User
    Join Date
    11-25-2007
    Posts
    46

    Bug in Excel 2003 Application.Run with dashes in filenames

    I am using Excel 2003. The macro below opens a workbook "Book1.xls" and runs a simple macro "Opening" which in this case is just a simple message box for test purporses. This macro will fail if the workbook is named "Book-1.xls".

    I even tested with the workbook in subdirectories which had dashes in the folder name, and the macro works as long as the file name itself does not have a dash.

    Sub RunMacro_NoArgs()
         'Macro purpose:  Use the application.run method to execute
         'a macro without arguments from another workbook
         
        Dim PathToFile As String, _
        NameOfFile As String, _
        wbTarget As Workbook, _
        CloseIt As Boolean
        Dim namelength As Integer
         
         'Set file name and location. You will need to update this info!
         NameOfFile = "Book1.xls"
         PathToFile = "C:\Documents and Settings\user\Desktop"
         
         'Attempt to set the target workbook to a variable.  If an error is
         'generated, then the workbook is not open, so open it
        On Error Resume Next
        Set wbTarget = Workbooks(NameOfFile)
         
        If Err.Number <> 0 Then
             'Open the workbook
            Err.Clear
            Set wbTarget = Workbooks.Open(PathToFile & "\" & NameOfFile)
            CloseIt = True
        End If
         
         'Check and make sure workbook was opened
        If Err.Number = 1004 Then
            MsgBox "Sorry, but the file you specified does not exist!" _
            & vbNewLine & PathToFile & "\" & NameOfFile
            Exit Sub
        End If
        On Error GoTo 0
    
        Application.Run (wbTarget.Name & "!ThisWorkbook.Opening")
        
        If CloseIt = True Then
             'If the target workbook was opened by the macro, close it
            wbTarget.Close savechanges:=False
        Else
             'If the target workbook was already open, reactivate this workbook
            ThisWorkbook.Activate
        End If
         
    End Sub
    Code originally from VBA Express Portal

  2. #2
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    Are you sure the pathtofile is correct
    "C:\Documents and Settings\user\Desktop"
    user is not a normal Windows login id


    Suggest add these changes to your code
    Dim sWinID As String
    'get windows login ID
    sWinID = Environ("Username") 
    PathToFile = "C:\Documents and Settings\" & sWinID & "\Desktop"
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

  3. #3
    Registered User
    Join Date
    11-25-2007
    Posts
    46

    Bug in Applicaton.Run

    I removed the actual user ID to provide some anonymity with my post, the real one is normal.

    Any idea why Excel gags on "Book-1.xls" but okay with "Book1.xls". I think a dash is a legal Windows/Dos character?

  4. #4
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    I use Excel 2003 on Windows XP SP2

    Your code works for me even if their is a - in the file name (As long as I use my ID in place of user in the path)

    Have you tried opening a file saved to a different location than the desktop

  5. #5
    Registered User
    Join Date
    11-25-2007
    Posts
    46
    All my trials have been with the calling workbook on the desktop and workbooks to open in folders on the desktop. When I discovered the issue I created the workbook Book1.xls directly on the desktop to speed troubleshooting.

    I was planning on moving a folder to my desktop from a server, run the macro to open each file in the folder and do it's business. Then move the folder back to server. Seemed easiest to move to the desktop.

    I can give this a try.

  6. #6
    Registered User
    Join Date
    11-25-2007
    Posts
    46
    I did try in a normal folder in my computer. This time leaving in my WinID in the example. Still fails with the name containing a dash vs no dash. Also tested with an underscore Book_1.xls, and this works fine.

    I have been changing all my files to not have dashes as a work around.

    Sub RunMacro_NoArgs()
         'Macro purpose:  Use the application.run method to execute
         'a macro without arguments from another workbook
         
        Dim PathToFile As String, _
        NameOfFile As String, _
        wbTarget As Workbook, _
        CloseIt As Boolean
        Dim namelength As Integer
         
         'Set file name and location. You will need to update this info!
         NameOfFile = "Book-1.xls"
         PathToFile = "C:\Documents and Settings\us265779\My Documents\My Code"
         
         'Attempt to set the target workbook to a variable.  If an error is
         'generated, then the workbook is not open, so open it
        On Error Resume Next
        Set wbTarget = Workbooks(NameOfFile)
         
        If Err.Number <> 0 Then
             'Open the workbook
            Err.Clear
            Set wbTarget = Workbooks.Open(PathToFile & "\" & NameOfFile)
            CloseIt = True
        End If
         
         'Check and make sure workbook was opened
        If Err.Number = 1004 Then
            MsgBox "Sorry, but the file you specified does not exist!" _
            & vbNewLine & PathToFile & "\" & NameOfFile
            Exit Sub
        End If
        On Error GoTo 0
    
        Application.Run (wbTarget.Name & "!ThisWorkbook.Opening")
        
        If CloseIt = True Then
             'If the target workbook was opened by the macro, close it
            wbTarget.Close savechanges:=False
        Else
             'If the target workbook was already open, reactivate this workbook
            ThisWorkbook.Activate
        End If
         
    End Sub
    Last edited by mudraker; 12-20-2007 at 01:03 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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