Solved

Add null value for empty fileds using vbs

Posted on 2009-03-30
6
425 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
[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
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
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…
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…

687 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