首页 > 解决方案 > 如果请求 AD 字段为空,我如何不打印字符串

问题描述

我编写了一个脚本来查询我们的域控制器 AD,并提取数据以创建电子邮件签名,我已经使用大量在线教程完成了这项工作,因为我以前没有任何使用这种语言的经验。

一切都完美无缺,除了一个领域;移动电话。

我想打印一行,前提是该字段包含任何数据,但我不知道该怎么做。我确信这是一个简单的解决方案,但我正在画一个完整的空白。

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

标签: emailvbscriptactive-directorysignature

解决方案


删除strUKMobile = objUser.mobile它现在所在的行以及您正在构建文本的部分,执行以下操作:

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

推荐阅读