【问题标题】:How to import multiple vCard VCF contact files into Outlook using Powershell?如何使用 Powershell 将多个 vCard VCF 联系人文件导入 Outlook?
【发布时间】:2017-03-29 06:23:27
【问题描述】:

如何使用 Powershell 将多个 vCard VCF 联系人文件导入 Outlook?

我创建 vcf 的脚本是:

    # Création d'un fichier.vcf

New-Item C:\TEST\$($LastName)_$($Name).vcf -type file -force -value "
BEGIN:VCARD
VERSION:2.1
N;LANGUAGE=fr;CHARSET=utf-8:$($Agence) 
FN;CHARSET=utf-8:$($LastName), $($Name)
ORG;CHARSET=utf-8:$($Company)
TITLE;CHARSET=utf-8:$($Title)
TEL;WORK;VOICE:$($WorkNum)
TEL;WORK;VOICE:$($WorkNum2)
TEL;CELL;VOICE:$($MobNum)
LABEL;WORK;PREF;CHARSET=utf-8;ENCODING=QUOTED-PRINTABLE:$($WorkAdress)
X-MS-OL-DEFAULT-POSTAL-ADDRESS:1
EMAIL;CHARSET=utf-8;PREF;INTERNET:$($Email)
X-MS-IMADDRESS;CHARSET=utf-8:$($Email) 
PHOTO;TYPE=JPEG;ENCODING=BASE64:$($encodedImage)

X-MS-OL-DESIGN;CHARSET=utf-8:<card xmlns='http://schemas.microsoft.com/office/outlook/12/electronicbusinesscards' ver='1.0' layout='left' bgcolor='ffffff'><img xmlns='' align='fit' area='16' use='cardpicture'/><fld xmlns='' prop='name' align='left' dir='ltr' style='b' color='000000' size='10'/><fld xmlns='' prop='org' align='left' dir='ltr' color='000000' size='8'/><fld xmlns='' prop='blank' size='8'/><fld xmlns=' prop='telwork' align='left' dir='ltr' color='d48d2a' size='8'><label align='right' color='626262'>Bureau</label></fld><fld xmlns='' prop='telhome' align='left' dir='ltr' color='d48d2a' size='8'><label align='right' color='626262'>Domicile</label></fld><fld xmlns='' prop='email' align='left' dir='ltr' color='d48d2a' size='8'/><fld xmlns='' prop='addrwork' align='left' dir='ltr' color='000000' size='8'/><fld xmlns='' prop='im' align='left' dir='ltr' color='000000' size='8'><label align='right' color='626262'>Mess. instant.</label></fld><fld xmlns='' prop='blank' size='8'/><fld xmlns='' prop='blank' size='8'/><fld xmlns='' prop='blank' size='8'/><fld xmlns='' prop='blank' size='8'/><fld xmlns=' prop='blank' size='8'/><fld xmlns='' prop='blank' size='8'/><fld xmlns='' prop='blank' size='8'/><fld xmlns='' prop='blank' size='8'/></card>
END:VCARD"`

【问题讨论】:

    标签: powershell outlook vcf-vcard


    【解决方案1】:

    我的问题没有找到答案,所以我使用了其他技术。

    首先,我创建了一个 Csv,并使用此 Powershell 代码将其转换为 XL:

        # Creating and Adding the first line of the Csv
      $String = "FullName;CompanyName;Email1Address;Fileas;JobTitle;BusinessTelephoneNumber;Business2TelephoneNumber;MobileTelephoneNumber;BusinessAdress;PathImage"
    
      $PathCsv = "C:\TEST\Contacts.csv"
      $CsvExists = Test-Path $PathCsv 
    
     if($CsvExists -eq $True) {
         Clear-Content $PathCsv 
         Add-Content $PathCsv -value $String
     } else {
         New-Item $PathCsv -type file  -value $String   
     }
    
     For Each .... {
    
        # Storing Information in variables
      $Name = "..."
      $Last [...] 
    
        # Adding the information in the Csv
      $string2= "$($Name) $($LastName);$($Corp);$($Email);$($Agence);$($Title);$($NumWork);$($NumWork2);$($NumCell);$($WorkAdress);$($path)"
    
      Add-Content -path $PathCsv -value $string2
    
    
         # Converting Csv to Xls
      $PathXls = "C:\TEST\Contacts.xls"
      $XlsExists= Test-Path $PathXls
      $xl = new-object -comobject excel.application
      $xl.visible = $true
      $Workbook = $xl.workbooks.open($PathCsv) 
      $Worksheets = $Workbooks.worksheets
    
     if($XlsExists -eq $True) {
       Remove-Item $PathXls -force
     }  
      $Workbook.SaveAs($PathXls,1) 
      $Workbook.Saved = $True
    
     $xl.Quit()
     }
    

    其次,我用这个 VBScript 创建了 Outlook 联系人:

      Sub creater()
    
          Dim strPath
          Dim num1 
          Dim num1toreplace 
          Dim num2 
          Dim num2toreplace 
          Dim num3 
          Dim num3toreplace 
          Dim Corp 
    
            Const olContactItem = 2
    
          Set objOutlook = CreateObject("Outlook.Application")
          Set objExcel = CreateObject("Excel.Application")
          Set objWorkbook = objExcel.Workbooks.Open("C:\TEST\Contacts.xls")
    
    
           x = 2
    
        Do Until objExcel.Cells(x, 1).Value = ""
    
          Set objContact = objOutlook.CreateItem(olContactItem)
    
          'Add FullName
           objContact.FullName = objExcel.Cells(x, 1).Value
    
          'Add CompanyName
           objContact.CompanyName = objExcel.Cells(x, 2).Value
    
          'Add Email1Address
           objContact.Email1Address = objExcel.Cells(x, 3).Value
    
          'Add Fileas
           objContact.Fileas = objExcel.Cells(x, 4).Value
    
          'Add JobTitle
           objContact.JobTitle = objExcel.Cells(x, 5).Value
    
          'Add BusinnesNum
           objContact.BusinessTelephoneNumber  = objExcel.Cells(x, 6).Value
    
          'Add BusinnesNum2
           objContact.Business2TelephoneNumber = objExcel.Cells(x, 7).Value
    
          'Add MobileNum
           objContact.MobileTelephoneNumber = objExcel.Cells(x, 8).Value
    
          'Add BusinessAddress
           objContact.BusinessAddress = objExcel.Cells(x, 9).Value
    
          'Add Picture
           strPath = objExcel.Cells(x, 10).Value
          If Not strPath = vbNullString Then
             objContact.AddPicture (strPath)
          End If
    
    
          objContact.Save
    
          x = x + 1
        Loop
    
          objExcel.Quit
    
     End Sub 
     creater()
    

    【讨论】:

      【解决方案2】:

      几个选项:

      我。 Outlook 对象模型 - 调用 Namespace.OpenSharedItem(它支持 MSG、vCard 和 iCal 格式)并传递 VCF 文件名。 Outlook 将返回ContactItem 对象的实例,您可以将其保存或移动到任何文件夹。

      二。如果使用Redemption 是一个选项,它通过Import 方法支持vCard 格式(在十几种其他格式中,包括vCard)(由RDOMail 和从它派生的对象公开,例如RDOContactItem)。

      set Session = CreateObject("Redemption.RDOSession")
      Session.MAPIOBJECT = Application.Session.MAPIOBJECT 'or you can use Logon, LogonHostedExchangeMailbox, etc.
      set Contact = Session.GetDefaultFolder(olFolderContacts).Items.Add
      Contact.Import "c:\Temp\test.vcf ", olVCard
      Contact.Save
      

      【讨论】:

      • 感谢您的回答;我没有“赎回”,所以我想使用第一个选项,但我不明白如何使用对象模型 .OpenSharedItem 将 vcf 文件导入 Outlook?
      • 我不确定我是否理解问题 - Application.Session.OpenSharedItem 将返回 ContactItem 对象。
      猜你喜欢
      • 1970-01-01
      • 2011-05-21
      • 2013-08-26
      • 1970-01-01
      • 1970-01-01
      • 2011-12-30
      • 1970-01-01
      • 2014-04-07
      • 1970-01-01
      相关资源
      最近更新 更多