Solved

Check IP address in VBA

Posted on 2006-11-20
3
1,472 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

773 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