Search code examples
vb6vbscriptactive-directoryinfopath

Create a user in Active Directory from an Infopath form


I've been searching the web for a while now and still can't seem to find anything useful on this topic.

What I am trying to create is a button in my Infopath form that will create a user account in Active Directory. The code will need to pull through information stored in the fields of the Infopath form.

I have chosen to use Visual Basic, as I believe that is a good language scripting with AD.

I have successfully run this code:

Sub CTRL1_12_OnClick(eventObj)
 MsgBox "Output: "
End Sub

which returns: 'Output: ' in a VB dialogue box when I click on my infopath button. However, when I try:

Set objOU=GetObject("LDAP://OU=People,OU=contoso,DC=contoso,DC=com")
Set objUser=objOU.Create("user","CN=Linda Mitchell")
objUser.Put "sAMAccountName","linda.mitchell"
objUser.SetInfo()

All I get is an error message in Infopath:

The following error occurred:

ActiveX component can't create object: 'GetObject' File:script.vbs Line:14

When I put the same code in to notepad, save as a vbs file and then execute is runs fine and create a user in active directory, what I don't understand is why when I run the same code it doesn't work.


Solution

  • I have now found out what the problem was. I needed to set the form to 'full trust' Tools>form options> Security and Trust. I also had to sign the form with my certificate.

    Now I am able to run the code from that button.