Solved

Check IP address in VBA

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…

734 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