Search code examples
emailvbscriptactive-directorysignature

How do I not print a string if request AD field is empty


I've written a script that queries our Domain controllers AD, and pulls out data to create an email signature, I've done this using numerous tutorials online as I've previously not had any experience with this language.

Everything works perfectly, with the exception of one field; mobile phone.

I would like to print a line, only if that field contains any data, but I can't figure out how to do that. I'm sure it's a simple solution but I'm drawing a complete blank.

On Error Resume Next 
Set objSysInfo = CreateObject("ADSystemInfo") 

' ########### This section connects to Active Directory as the currently logged on user 
 strUser = objSysInfo.UserName

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


strUKPhone = "+44 (0)20 3457 7633" 
strUKMobile = objUser.mobile
strEmail = objuser.mail 
strWeb = objuser.wWWHomePage 
strNotes = objuser.info 
strExt = objuser.ipPhone 
strDDI = objuser.homephone 
strSALUTATION = "Kind regards," 
strEmailTEXT = "E " 
strWebTEXT = "W "
strAddressTEXT = "A "
strPhoneTEXT = "T "
strMobileTEXT = "M "

' ########### Sets up word template 

Set objWord = CreateObject("Word.Application") 
Set objDoc = objWord.Documents.Add() 
Set objSelection = objWord.Selection 

objSelection.Style = "No Spacing" 
Set objEmailOptions = objWord.EmailOptions 
Set objSignatureObject = objEmailOptions.EmailSignature 
Set objSignatureEntries = objSignatureObject.EmailSignatureEntries 

' ########### Calls the variables from above section and inserts into word template, also sets initial font typeface, colour etc. 


objselection.TypeText strEmailTEXT 
  objSelection.Font.Color = RGB (000,000,000)
  objselection.Font.Bold = false 
Set objLink = objSelection.Hyperlinks.Add(objSelection.Range, "mailto: " & strEmail, , , strEmail) 
  objLink.Range.Font.Name = "Arial" 
  objLink.Range.Font.Size = 8.5 
  objLink.Range.Font.Bold = false 
objSelection.Font.Color = RGB (000,000,000)
objSelection.TypeText "  |  "
  objSelection.Font.Color = RGB (181,021,059)
objSelection.TypeText strMobileTEXT
  objSelection.Font.Color = RGB (000,000,000)
objSelection.TypeText strUKMobile 
  objSelection.TypeParagraph()
  objSelection.Font.Color = RGB (181,021,059)


Set objSelection = objDoc.Range() 
objSignatureEntries.Add "Email Signature", objSelection 
objSignatureObject.NewMessageSignature = "Email Signature" 
objSignatureObject.ReplyMessageSignature = "Email Signature" 
Set objSelection = objDoc.Range()
objDoc.Saved = True 

objWord.Quit

Solution

  • Remove the line strUKMobile = objUser.mobile where it is now and in the part where you are building the text, do this:

    objselection.TypeText strEmailTEXT 
      objSelection.Font.Color = RGB (000,000,000)
      objselection.Font.Bold = false 
    Set objLink = objSelection.Hyperlinks.Add(objSelection.Range, "mailto: " & strEmail, , , strEmail) 
      objLink.Range.Font.Name = "Arial" 
      objLink.Range.Font.Size = 8.5 
      objLink.Range.Font.Bold = false 
    objSelection.Font.Color = RGB (000,000,000)
    
    ' Only write this if the mobile phone has a value
    If Not (IsNull(objUser.mobile) Or IsEmpty(objUser.mobile)) Then
        strUKMobile = objUser.mobile
        objSelection.TypeText "  |  "
          objSelection.Font.Color = RGB (181,021,059)
        objSelection.TypeText strMobileTEXT
          objSelection.Font.Color = RGB (000,000,000)
        objSelection.TypeText strUKMobile 
          objSelection.TypeParagraph()
          objSelection.Font.Color = RGB (181,021,059)
    End If