Solved

Check IP address in VBA

Posted on 2006-11-20
3
1,553 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

707 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