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
Bookmarks