+ Reply to Thread
Results 1 to 8 of 8

Automatically run macro before trying to open file

Hybrid View

  1. #1
    Registered User
    Join Date
    11-25-2011
    Location
    Lilydale, Victoria, Australia
    MS-Off Ver
    2013 (365)
    Posts
    63

    Automatically run macro before trying to open file

    Working from home, I often need to connect to the server at work (on VPN), and open an excel file (Excel 2013).
    I have a macro that automatically checks whether the VPN is connected - I wrote that because the VPN often drops out without my noticing, and if I try to open a file on the server, it thinks for half a minute before telling me it can't find the file, or words to that effect.
    The problem is, I can't get it to check the VPN connection before it tries to open the file (so it still just sits). Is there a way to do that?
    While we're at it, I'd also like to only check the VPN when I'm trying to open a file on the work server (which is mapped to my L: drive). Is there a way to do that?
    Running Win7.
    Many thanks.

  2. #2
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Re: Automatically run macro before trying to open file

    Can you post a sample of your existing code and note where it is located (personal macro workbook, specific workbook, add-in...)
    Please click *Add Reputation if I've helped

  3. #3
    Registered User
    Join Date
    11-25-2011
    Location
    Lilydale, Victoria, Australia
    MS-Off Ver
    2013 (365)
    Posts
    63

    Re: Automatically run macro before trying to open file

    This is my code (borrowed and amended from other code I found on the web). The function looks for an item under ipconfig which has "Description...fortissl" in it, which is only there when the VPN is connected (running FortiClient):

    Option Explicit
    
    
    Private Sub workbook_open()
        Dim PathToFile As String, _
        NameOfFile As String, _
        wbTarget As Workbook
        Dim VPNState As Boolean
     VPNState = IsVPNConnected
    If VPNState = False Then GoTo GetOut
         'Set file name and location.
        NameOfFile = "PERSONAL1.xls"
        PathToFile = "L:\New File Setup"
         
         '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
        
    GetOut:
    End Sub
    
    Function IsVPNConnected()
    '
    'Private Sub workbook_open()
    Dim objShell As Object
    Dim objExecObject As Object
    Dim strline As String
    IsVPNConnected = True
    Set objShell = CreateObject("WScript.Shell")
    Set objExecObject = objShell.Exec("%comspec% /c ipconfig.exe /all")
    Do Until objExecObject.StdOut.AtEndOfStream
        strline = objExecObject.StdOut.ReadLine()
    If strline Like "*Description*fortissl" Then GoTo VPNconnected
    Loop
    IsVPNConnected = False
    MsgBox "No VPN"
    VPNconnected:
    End Function

  4. #4
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Re: Automatically run macro before trying to open file

    Try this (I have not changed the IsVPNConnected function):
    Option Explicit
    Sub OpenVPNFiles()
        Dim intFilesSelected As Integer
        Dim strFilePath As String
        Dim i As Integer
        
        If IsVPNConnected Then
        
            'Get files to open
            With Application.FileDialog(msoFileDialogOpen)
                .AllowMultiSelect = True
                .InitialFileName = "L:"
                intFilesSelected = .Show
            
                'Open the selected files
                If intFilesSelected <> 0 Then
                    For i = 1 To .SelectedItems.Count
                        strFilePath = .SelectedItems(i)
                        Workbooks.Open Filename:=strFilePath
                    Next i
                End If
                
            End With
        
        Else
            MsgBox "The VPN is not currently connected."
        End If
         
    End Sub
    
    
    Function IsVPNConnected()
    Dim objShell As Object
    Dim objExecObject As Object
    Dim strline As String
    IsVPNConnected = True
    Set objShell = CreateObject("WScript.Shell")
    Set objExecObject = objShell.Exec("%comspec% /c ipconfig.exe /all")
    Do Until objExecObject.StdOut.AtEndOfStream
        strline = objExecObject.StdOut.ReadLine()
    If strline Like "*Description*fortissl" Then GoTo VPNconnected
    Loop
    IsVPNConnected = False
    MsgBox "No VPN"
    VPNconnected:
    End Function
    You should save all of it to your Personal Macro Workbook so that it is available every time you use Excel and then to run the macro you should assign it to a button which you can put on the Quick Access Toolbar. Then, any time you want to open a file on the L: drive, you just click that button instead of using Excel's usual open file dialog.

    The macro first checks the VPN is connected and if it isn't, displays a warning and does nothing else. If it is connected, it launches the file open dialog at the L: drive so you can select which files there you want to open. This way it will work for any file on that drive that you want to open and only when the VPN is connected.

    I must stress that I have not been able to test your IsVPNConnected function and I do not know enough about shell objects to see if the code seems correct. Providing it does what it is supposed to and only returns true if the VPN is connected then my code should work.
    Last edited by kadeo; 09-10-2015 at 06:30 AM.

  5. #5
    Registered User
    Join Date
    11-25-2011
    Location
    Lilydale, Victoria, Australia
    MS-Off Ver
    2013 (365)
    Posts
    63

    Re: Automatically run macro before trying to open file

    Thanks very much, kadeo. I'll give it a try. Sometimes I open files from the Recent Items list or from other shortcuts. I guess it wouldn't work with those.

  6. #6
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Re: Automatically run macro before trying to open file

    Quote Originally Posted by GCW esq View Post
    ...Sometimes I open files from the Recent Items list or from other shortcuts. I guess it wouldn't work with those.
    I suspect the issue of your half minute lag while it realises the VPN is not active is down to the operating system. The other shortcuts and the recent items list you use effectively just hold the path of the file as a string and clicking them instructs Excel to open a file at that location without the need for the file open dialog. If the file isn't there, the operating system tells Excel and Excel warns you the file is no longer there, in most cases because it has been moved or renamed since the recent item or shortcut was created but in this case because the L: Drive is no longer mapped. Nevertheless, Excel doesn't know it isn't there until it tries to open the file.

    It would be awesome if Excel had a 'BeforeOpen' event that could be monitored, but where would it go? In the workbook you are trying to open? Unless you open the file, Excel wouldn't be able to run any code within it, so it couldn't go there. In the Personal Macro workbook? In order for it to work here it would need to be an application level event which means you need to set up a class to monitor the application events first. There is even a WorkbookOpen event that seems to fire before a selected workbook opens but it doesn't have a cancel option so while you can run code on the open event, you can't stop the open event from happening if certain criteria are not met.

    I think to do what you want is going to require moving outside Excel's functionality and monitoring the operating system events somehow and capturing any requests to open files on the L drive and processing them instead. I'm sure that's possible from within VBA by declaring functions in system libraries, but somebody else is going to need to talk you through that one.
    Last edited by kadeo; 09-10-2015 at 07:48 AM.

  7. #7
    Registered User
    Join Date
    11-25-2011
    Location
    Lilydale, Victoria, Australia
    MS-Off Ver
    2013 (365)
    Posts
    63

    Re: Automatically run macro before trying to open file

    WOW! Excellent explanation. Thanks, kadeo.

  8. #8
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Re: Automatically run macro before trying to open file

    Glad you approve! I love a good explain. Lol Thanks for the rep.

+ 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. [B]File closes automatically, when I open an another file[/B]
    By xlman in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-05-2014, 02:02 AM
  2. Open a csv file automatically at a certain time of the day without having excel open
    By Sparky2000 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-04-2014, 06:52 AM
  3. Automatically open a file in a specific file path when another sheet is opened
    By joshnathan in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-20-2014, 11:05 AM
  4. Replies: 0
    Last Post: 10-02-2013, 01:47 AM
  5. [SOLVED] Automatically Enable Macro when file open.
    By Jack40 in forum Excel General
    Replies: 6
    Last Post: 12-21-2012, 03:00 AM
  6. Schedule Excel File to automatically open and run VB macro
    By MagsNZ in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-12-2012, 10:02 PM
  7. Replies: 0
    Last Post: 06-25-2007, 11:56 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