Solved

Check IP address in VBA

Posted on 2006-11-20
3
1,439 Views
Last Modified: 2012-06-27
I have an Excel application that I want to make impossible to run anywhere but in my local shop.  I would like a function to check the IP address, subnet, and default gateway.  I would then build that into my app to check on startup and if the gateway and subnet values are not within range shut down the application - or maybe even delete the app itself.
0
Comment
Question by:MGothelf
  • 2
3 Comments
 
LVL 76

Accepted Solution

by:
David Lee earned 125 total points
ID: 17983874
Greetings, MGothelf.

Try this.  It uses WMI to test the settings of all the adapters in the system where IP is enabled.  If any of them have the correct settings, then it returns true.

Function TestIPSettings() As Boolean
    Const wbemFlagReturnImmediately = &H10
    Const wbemFlagForwardOnly = &H20
    Dim objWMIService As Object, _
        colItems As Object, _
        strDefaultIPGateway As String, _
        strIPAddress As String, _
        strIPSubnet As String
    Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
    Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled=True", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
    For Each objItem In colItems
        strDefaultIPGateway = Join(objItem.DefaultIPGateway, ",")
        strIPAddress = Join(objItem.IPAddress, ",")
        strIPSubnet = Join(objItem.IPSubnet, ",")
        'Change the address to test against on the following line
        If strDefaultIPGateway = "192.168.0.1" Then
            'Change the address or partial address on the following line
            If Left(strIPAddress, 9) = "192.168.0" Then
                'Change the subnet mask on the following line
                If strIPSubnet = "255.255.255.0" Then
                    TestIPSettings = True
                    Exit For
                End If
            End If
        End If
    Next
    Set colItems = Nothing
    Set objWMIService = Nothing
End Function

Cheers!
0
 

Author Comment

by:MGothelf
ID: 17988174
That works just fine, but I see a problem in this approach...

I put the code in the Workbook_Open() procedure.  If the user sets security level to HIGH that would disable macros, and so skip this procedure.  Is there a way to force the procedure to run?  Or is there a way to say if macros are disabled don't open the workbook?
0
 
LVL 76

Expert Comment

by:David Lee
ID: 17988736
Sorry, that's a question specific to Excel, not VB.  I know a bit about VBA in Excel, but not enough to be able to answer that question.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

937 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now