Solved

Check IP address in VBA

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

706 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

19 Experts available now in Live!

Get 1:1 Help Now