Solved

Add null value for empty fileds using vbs

Posted on 2009-03-30
6
401 Views
Last Modified: 2013-11-25
I have a script that queries Active Directory groups in an OU and outputs it's members into an excel. The problem is when a group has no members it doesn't insert a blank cell in excel.
On Error Resume Next

Const ADS_SCOPE_SUBTREE = 5
 

Dim objGroup, objExcel, iRow, strUser

'Set objGroup = GetObject("LDAP://cn="*",ou=DNP,ou=Groups,ou=Users and Groups, ou=Org,dc=xxx,dc=xxx,dc=xxx")

strGroup="*"
 

Set objConnection = CreateObject("ADODB.Connection")

Set objCommand =   CreateObject("ADODB.Command")

objConnection.Provider = "ADsDSOObject"

objConnection.Open "Active Directory Provider"

Set objCommand.ActiveConnection = objConnection
 

objCommand.Properties("Page Size") = 1000

objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
 

objCommand.CommandText = _

    "SELECT Name, adSPath FROM 'LDAP://ou=DNP,ou=Groups,ou=Users and Groups, ou=xxx,dc=xxx,dc=xxx,dc=xxx' WHERE objectCategory='group' " & _

        "AND Name='" & strGroup & "'"
 

Set objRecordSet = objCommand.Execute
 

Set objExcel = CreateObject("Excel.Application")

With objExcel

  .SheetsInNewWorkbook = 1

  .Workbooks.Add

  .Visible = True

 .Worksheets.Item(1).Name = objRecordSet.Fields("Name").Value

  irow=1
 

objRecordSet.MoveFirst

Do Until objRecordSet.EOF

  Set objGroup = GetObject(objRecordSet.Fields("adsPath").Value)

  For Each strUser in objGroup.Member

     Set objUser =  GetObject("LDAP://" & strUser)

    .Cells(iRow,1) = objUser.CN

    .Cells(iRow,2) = objUser.description

    .Cells(iRow,3) = objRecordSet.Fields("Name").Value

    irow=irow + 1

  Next

  set objGroup=Nothing

  objRecordSet.MoveNext

Loop
 

 .Columns(1).entirecolumn.autofit

End With
 

Set objExcel = Nothing

Set objGroup = Nothing

Open in new window

0
Comment
Question by:leche671
  • 3
  • 2
6 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 24021408
You could check the membership count first...
On Error Resume Next

Const ADS_SCOPE_SUBTREE = 5

 

Dim objGroup, objExcel, iRow, strUser

'Set objGroup = GetObject("LDAP://cn="*",ou=DNP,ou=Groups,ou=Users and Groups, ou=Org,dc=xxx,dc=xxx,dc=xxx")

strGroup="*"

 

Set objConnection = CreateObject("ADODB.Connection")

Set objCommand =   CreateObject("ADODB.Command")

objConnection.Provider = "ADsDSOObject"

objConnection.Open "Active Directory Provider"

Set objCommand.ActiveConnection = objConnection

 

objCommand.Properties("Page Size") = 1000

objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 

 

objCommand.CommandText = _

    "SELECT Name, adSPath FROM 'LDAP://ou=DNP,ou=Groups,ou=Users and Groups, ou=xxx,dc=xxx,dc=xxx,dc=xxx' WHERE objectCategory='group' " & _

        "AND Name='" & strGroup & "'"

 

Set objRecordSet = objCommand.Execute

 

Set objExcel = CreateObject("Excel.Application")

With objExcel

  .SheetsInNewWorkbook = 1

  .Workbooks.Add

  .Visible = True

 .Worksheets.Item(1).Name = objRecordSet.Fields("Name").Value

  irow=1

 

objRecordSet.MoveFirst

Do Until objRecordSet.EOF

  Set objGroup = GetObject(objRecordSet.Fields("adsPath").Value)
 

  If ubound(objGroup.Member) = 0 Then

    .Cells(iRow,1) = "No membership found"

  Else

  For Each strUser in objGroup.Member

     Set objUser =  GetObject("LDAP://" & strUser)

    .Cells(iRow,1) = objUser.CN

    .Cells(iRow,2) = objUser.description

    .Cells(iRow,3) = objRecordSet.Fields("Name").Value

    irow=irow + 1

  Next

  End If

  set objGroup=Nothing

  objRecordSet.MoveNext

Loop

 

 .Columns(1).entirecolumn.autofit

End With

 

Set objExcel = Nothing

Set objGroup = Nothing

Open in new window

0
 

Author Comment

by:leche671
ID: 24021772
I really have zero experience with vbs scripts just a bunch of copy and pasting. I now receive an error.
Line: 48
Char: 1
Error: "loop" without 'do'

I don't know why the added code would do that. It ran fine before. Any ideas.
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
ID: 24021819
It shouldn't at all...
Did you copy/paste the exact code above?
0
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.

 

Author Comment

by:leche671
ID: 24021983
You're right. I fixed it and ran it again. Now omits the groups that have no members.
0
 

Author Comment

by:leche671
ID: 24022447
Any idea why it's omitting the groups without members?
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 24022859
>> Any idea why it's omitting the groups without members?

It's doing that because iRow is not being incremented when no members are found.

Add
    irow=irow + 1

under this line
    .Cells(iRow,1) = "No membership found"


Regards,

Rob.
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

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…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
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…
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…

911 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

20 Experts available now in Live!

Get 1:1 Help Now