Jump to content

GAB2OAB Google contacts to Outlook Contacts


Recommended Posts

I was in a hurry to convert 200+ address book from Google Contacts (G Suite, contacts exported in .CSV with Google Takeout, but the contact format is the same for every Google Gmail account)  to Outlook contacts, so I developed this quick and very dirty script.

Starting from a .CSV exported from Google, you can run the script and you have the contacts ported directly (no outlook import) on the Outlook Address book.

In my experience, the fields of Google contacts are not fixed in number (!) and maybe also in position, so I made the choice  to import only certain fields to Outlook, scanning every line.

These fields, exact google names:

        "Given Name"  
        "Family Name"  
        "E-mail 1 - Value"  
        "E-mail 2 - Value"  
        "Phone 1 - Value"  
        "Phone 2 - Value"  
        "Website 1 - Value" 
        "Notes"  
        "Name"  
        "Address 1 - Formatted"  

Also the lines of google contacts can be weird as CR and LF are not "regular".

I include my code, (worked with no errors on a lot of conversions) and also a very small fake adress book with examples of problems I encountered and resolved.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=Icone\Win98-ico\script_file_teal.ico
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

;GOA2OAB
;G.oogle A.ddress B.ook 2 (to) O.utlook A.ddress B.ook
;(C) NSC 2019
; convert a google (takeout) address book to an Outlook Address Book

#include <File.au3>
#include <Array.au3>
#include <_Gollog.au3>
#include <MsgBoxConstants.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
#include <String.au3>
#include <Math.au3>


; *****************************************************************************
#include <OutlookEX.au3> ; super perla pearl outlook create contacts !!!!!
; *****************************************************************************
Global $oItem
Global $oOutlook = _OL_Open()
If @error <> 0 Then MsgBox(16, "OutlookEX UDF", "Error creating a connection to Outlook. @error = " & @error & ", @extended = " & @extended)
; *****************************************************************************



Global $ver = "V.0.8192", $tempdir = "c:\GAB2OAB_temp", $File2convert, $aRetArray, $workfile = $tempdir & "\work-GAB2OAB.csv", $workfile2 = $tempdir & "\WORK2-gab2oab.csv", $FINALab = $tempdir & "\NEW-ab.csv", $A3clean[0], $A4clean[0], $A5clean[0]

; Gollog Autogenerated Gui
$mygui = "G.oogle A.ddress B.ook 2 (to) O.utlook A.ddress B.ook " & $ver & "           NSC 2019"
gollog_ini($mygui, 600, 200, 50, 50, "0x0033cc", "0xA8D9BD", "courier", 8, 600, @ScriptDir & "\GAB2OAB_log.txt") ; initialization with colors and font choice, more options see the UDF
Gollog(">>>>>>>> GAB2OAB " & $ver)
$File2convert = FileOpenDialog("Select Google Address Book File", @DesktopDir & "\", "CSV (*.csv)", BitOR($FD_FILEMUSTEXIST, $FD_MULTISELECT))
Gollog("selected " & $File2convert)

If Not FileExists($tempdir) Then
    Gollog("creating " & $tempdir)
    DirCreate($tempdir)
EndIf

If FileExists($workfile) Then
    Gollog("deleting " & $workfile)
    FileDelete($workfile)
EndIf
If FileExists($workfile2) Then
    Gollog("deleting " & $workfile2)
    FileDelete($workfile2)
EndIf
If FileExists($FINALab) Then
    Gollog("deleting " & $FINALab)
    FileDelete($FINALab)
EndIf

cleanfile()

readfile()

cleanfile2()

readfile2()

write2Outlook()


Exit

Func write2Outlook()

    Gollog("---> Assigning Fields...")
    Sleep(300)

    Local $FirstName = 4 ; initialize with Yomi name - always empty in tested companies, low class workaround
    Local $LastName = 4
    Local $Email1Address = 4
    Local $Email2Address = 4
    Local $MobileTelephoneNumber = 4
    Local $OtherTelephoneNumber = 4
    Local $Webpage = 4
    Local $Body = 4
    Local $Fullname = 4
    Local $BusinessAddress = 4


    For $z = 0 To UBound($A5clean, 2) - 1
        ;If $A5clean[2][$z] = "Additional Name" Then $MiddleName = $z
        ; limit @ 10
        If $A5clean[1][$z] = "Given Name" Then $FirstName = $z
        If $A5clean[1][$z] = "Family Name" Then $LastName = $z
        If $A5clean[1][$z] = "E-mail 1 - Value" Then $Email1Address = $z
        If $A5clean[1][$z] = "E-mail 2 - Value" Then $Email2Address = $z
        If $A5clean[1][$z] = "Phone 1 - Value" Then $MobileTelephoneNumber = $z
        If $A5clean[1][$z] = "Phone 2 - Value" Then $OtherTelephoneNumber = $z
        If $A5clean[1][$z] = "Website 1 - Value" Then $Webpage = $z
        If $A5clean[1][$z] = "Notes" Then $Body = $z
        If $A5clean[1][$z] = "Name" Then $Fullname = $z
        If $A5clean[1][$z] = "Address 1 - Formatted" Then $BusinessAddress = $z
    Next
    Gollog("Fields Found !")

    For $i = 2 To UBound($A5clean, 1) - 1


        Gollog("Contact 2 Outlook line " & $i)
        ;Gollog($A5clean[$i][1])
        ; Create a contact with first- and lastname
        ; *****************************************************************************
        $oItem = _OL_ItemCreate($oOutlook, $olContactItem, "", "", "FullName=" & $A5clean[$i][$Fullname], "BusinessAddress=" & $A5clean[$i][$BusinessAddress], "FirstName=" & $A5clean[$i][$FirstName], "LastName=" & $A5clean[$i][$LastName], "Email1Address=" & $A5clean[$i][$Email1Address], "Email2Address=" & $A5clean[$i][$Email2Address], "WebPage=" & $A5clean[$i][$Webpage], "MobileTelephoneNumber=" & $A5clean[$i][$MobileTelephoneNumber], "OtherTelephoneNumber=" & $A5clean[$i][$OtherTelephoneNumber], "Body=" & $A5clean[$i][$Body])
        If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemCreate ", "Error creating a contact'. @error = " & @error & ", @extended = " & @extended)
        ; Add a picture to the contact
        ;$oItem.AddPicture(@ScriptDir & "\The_Outlook.jpg")
        $oItem.Save()
        ; *****************************************************************************

    Next
    MsgBox(64, "OutlookEX UDF: the magic was made!", "All contacts successfully created ")

    _OL_Close($oOutlook) ; close the connection to Outlook
EndFunc   ;==>write2Outlook

Func cleanfile()

    Gollog('start check for unwanted LF... / final rogue "')
    Sleep(300)
    Local $A2Clean = FileReadToArray($File2convert)
    ;_ArrayDisplay($A2Clean, "original file", Default, 8)


    Local $dimLIST = UBound($A2Clean) - 1
    Local $string2write = ""
    Local $Astring1 = ""
    Local $Astring2 = ""

    Local $salvaAstring1 = ""
    Local $openLF = 0 ; we are between "LF" across lines

    For $i = 0 To $dimLIST
        Gollog('workin on line ' & $i)
        ;Gollog($A2Clean[$i])

        $Astring1 = StringToASCIIArray($A2Clean[$i])

        ;_ArrayDisplay($Astring1)
        Local $DimAstring1 = UBound($Astring1) - 1

        If $openLF = 1 Then
            ;check new line if start with "
            $Astring2 = StringToASCIIArray($A2Clean[$i])
            If $Astring2[0] = "34" Then

                ;       gollog('found initial " ')
                $openLF = 2
            Else
                Gollog('error new line not starting with "')
                MsgBox(48, "error", 'error new line not starting with "')
            EndIf
        EndIf

        If $Astring1[$DimAstring1] = "34" Then
            ;   gollog('First line -> Found final "')

            If $openLF <> 0 Then
                $salvaAstring1 = $salvaAstring1 & $A2Clean[$i]
            Else
                $salvaAstring1 = $A2Clean[$i]
            EndIf

        Else
            _ArrayAdd($A3clean, $salvaAstring1 & $A2Clean[$i])
            ;Gollog("adding to cleaned array state:" & $openLF)
            $openLF = 0
            $salvaAstring1 = ""
        EndIf

    Next
    Gollog('end clean from rogue "')
    ;_ArrayDisplay($A3clean, 'cleaned from rogue "')

    ;___________________________________P-PP-PP,P-------------------------------------------------
    Gollog('Start joining lines based on ODD " ')
    Sleep(300)
    ;_MathCheckDiv ( $iNum1 [, $iNum2 = 2] ) pari e dispari perla pearl



    Local $hFileOpen = FileOpen($workfile2, $FO_APPEND)
    If $hFileOpen = -1 Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the file.")
        Return False
    EndIf

    Local $dimLIST = UBound($A3clean) - 1
    Local $Astring1 = ""
    Local $Astring2 = ""
    Local $dimSTRING = ""
    Local $AapiciINriga
    Local $numeroapiciINriga = 0
    Local $sommaNumeroapiciINrighe = 0
    Local $salvaAstring1 = ""
    Local $dispari = 0


    For $i = 0 To $dimLIST
        Gollog("workin' on line " & $i)
        $Astring1 = StringToASCIIArray($A3clean[$i])
        ;_ArrayDisplay($Astring1)
        ;Gollog($A3clean[$i])
        $AapiciINriga = _ArrayFindAll($Astring1, '34')

        If $AapiciINriga <> -1 Then
            $numeroapiciINriga = UBound($AapiciINriga)
            ;   Gollog("N° " & $numeroapiciINriga & "  apici in riga " & $i)
            $sommaNumeroapiciINrighe += $numeroapiciINriga
            If _MathCheckDiv($sommaNumeroapiciINrighe) = 1 Then
                $salvaAstring1 = $salvaAstring1 & $A3clean[$i]
                $dispari = 1
            Else
                Local $towrite = $salvaAstring1 & $A3clean[$i]
                ;       gollog("scrivo " & $towrite)

                FileWriteLine($hFileOpen, $towrite)
                $salvaAstring1 = ""
                $sommaNumeroapiciINrighe = 0
                $dispari = 0
            EndIf
        Else
            ;   Gollog("zero apici in riga " & $i)
            If $dispari = 1 Then ;linea vuota o senza apici ma " aperte dispari
                $salvaAstring1 = $salvaAstring1 & $A3clean[$i]
            Else
                Local $towrite = $salvaAstring1 & $A3clean[$i]
                ;       gollog("scrivo " & $towrite)

                FileWriteLine($hFileOpen, $towrite)
                $salvaAstring1 = ""
                $sommaNumeroapiciINrighe = 0
                $dispari = 0
            EndIf
        EndIf

    Next

    Gollog('END joined lines based on ODD "')
    ;_ArrayDisplay($A4clean, 'joined lines based on ODD "')
    FileClose($hFileOpen)

EndFunc   ;==>cleanfile


Func readfile()
    gollog("reading work file and creating multiD array")
    Sleep(300)
    $A4clean = FileReadToArray($workfile2)
    If @error = 1 Or @error = 2 Then
        gollog("error reading work2 file " & @error)

    Else
        ;       _ArrayDisplay($A4clean, "cleaned 1", Default, 8)
    EndIf
EndFunc   ;==>readfile


Func cleanfile2()

    Local $dimLIST = UBound($A4clean) - 1
    Local $Astring1 = ""
    Local $conta = 0
    Local $salvaconta = 0
    Local $dimSTRING = ""
    $salvalinea = ""
    Local $standardNumeroVirgole = 0
    Local $hFileOpen = FileOpen($FINALab, $FO_APPEND)
    If $hFileOpen = -1 Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the file.")
        Return False
    EndIf


    Gollog('Checkin , number')
    For $i = 0 To $dimLIST
        ;Gollog('workin on line ' & $i)
        $Astring1 = StringToASCIIArray($A4clean[$i])
        ;_ArrayDisplay($Astring1)
        $dimSTRING = UBound($Astring1) - 1

        For $y = 0 To $dimSTRING
            If $Astring1[$y] = "44" Then ; secondo step ,
                $conta += 1
            EndIf
        Next

        If $i = 0 Then
            $standardNumeroVirgole = $conta
            gollog("----------numero virgole -> " & $standardNumeroVirgole)
        EndIf

        If $conta = $standardNumeroVirgole Then
            FileWriteLine($hFileOpen, $A4clean[$i])
        EndIf

        If $conta > $standardNumeroVirgole Then
            Gollog('>>> Line ' & $i & ' Virgole' & $conta)
            $toprecise = $A4clean[$i]
            $tofinal = ''

            While StringInStr($toprecise, '"') <> 0
                $tofinal = preciseremoval($toprecise) ;super smart
                $toprecise = $tofinal

            WEnd
            FileWriteLine($hFileOpen, $toprecise)
        EndIf

        If $conta < $standardNumeroVirgole Then
            Gollog('<<< Line ' & $i & ' Virgole' & $conta)
            $salvalinea = $salvalinea & $A4clean[$i]
            $salvaconta += $conta
            If $salvaconta = $standardNumeroVirgole Then
                $salvaconta = 0
                FileWriteLine($hFileOpen, $salvalinea)
                $salvalinea = 0
            EndIf
        EndIf

        $conta = 0
    Next

    FileClose($hFileOpen)

EndFunc   ;==>cleanfile2

Func writeWORKfile2()
    Gollog("writing work file")
    If _FileWriteFromArray($workfile, $A4clean) <> 1 Then
        gollog("error writing work file " & @error)
    EndIf
EndFunc   ;==>writeWORKfile2

Func readfile2()
    gollog("reading work file and creating multiD array")

    If _FileReadToArray($FINALab, $A5clean, Default, ",") = 0 Then
        MsgBox(48, "errore", @error)
    EndIf

    ;_ArrayDisplay($A5clean, "cleaned 2", Default, 8)

EndFunc   ;==>readfile2



Func preciseremoval($Pclean)
    ; llllllllllllllllllllllllll ---- pass 1 for ",,"
    $Astring1 = StringToASCIIArray($Pclean)
    ;_ArrayDisplay($Astring1)
    $dimSTRING = UBound($Astring1) - 1

    Local $salvadelete = ""
    Local $cond1 = 0
    For $y = 0 To $dimSTRING

        If $cond1 = 1 Then ; , removed so exit from loop
            If $Astring1[$y] = "34" Then ; terzo step " in chiusura
                ;   gollog('Found right "')
                If $salvadelete = "" Then
                    $salvadelete = String($y)
                Else
                    $salvadelete = $salvadelete & ";" & $y
                EndIf

                $cond1 = 0
                ExitLoop
            EndIf
        EndIf

        If $cond1 = 1 Then
            If $Astring1[$y] = "44" Then ; secondo step ,

                If $salvadelete = "" Then
                    $salvadelete = String($y)
                Else
                    $salvadelete = $salvadelete & ";" & $y
                EndIf
            EndIf
        EndIf

        If $Astring1[$y] = "34" And $cond1 = 0 Then ;PRimo step  per primo ->"
            $cond1 = 1
            ;gollog('Found left "')

            If $salvadelete = "" Then
                $salvadelete = String($y)
            Else
                $salvadelete = $salvadelete & ";" & $y
            EndIf
            ;Gollog("debug" & $salvaApici)
        EndIf

    Next

    ;   Gollog("apici e virgole da togliere: " & $salvadelete)
    ;gollog("virgole  togliere: " & $salvaVirgole)

    If _ArrayDelete($Astring1, $salvadelete) = -1 Then
        Gollog("error in arraydelete apici " & @error)
        gollog(StringFromASCIIArray($Astring1))
    Else
        gollog(' PASS 1 removed unwanted ",," from line ')
    EndIf


    $Astring2 = StringFromASCIIArray($Astring1)
    $Pclean = $Astring2

    Return $Pclean


EndFunc   ;==>preciseremoval

You will need the OutlookEX.au3 ,  look at this link and also my logging UDF  _Gollog.au3, attached.

Used on Outlook  from 2010 to 2016/365 with no problems.

Hope this script can help someone.

 

TEST1.csv _GOLLOG.au3

Edited by t0nZ
Link to post
Share on other sites

As soon as I find some spare time I will create a new function in OutlookTools to import contacts from a CSV file :)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2021-11-10 - Version 1.6.0.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (NEW 2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (NEW 2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to post
Share on other sites
  • 3 weeks later...

Done! Version 0.4.0.0 has a new function: _OLT_CSV_Import

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2021-11-10 - Version 1.6.0.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (NEW 2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (NEW 2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By water
      Extensive library to control and manipulate Microsoft Outlook. This UDF holds the functions to automate items (folders, mails, contacts ...) in the background. Can be seen like an API.
      There are other UDFs available to automate Outlook:
      OutlookEX_GUI: This UDF holds the functions to automate the Outlook GUI. OutlookTools: Allows to import/export contacts and events to VCF/ICS files and much more.  Threads: Development - General Help & Support - Example Scripts - Wiki
      BTW: If you like this UDF please click the "I like this" button. This tells me where to next put my development effort

      KNOWN BUGS (last changed: 2020-02-09)
      None
    • By Epulone
      For a specific requirement the outbound email are managed by an on-prem EDGE server. The EDGE re-write the local on-prem email address with another domain [exposed domain - mx record-]. Everything is working fine but we are having an issue when the internal local users are requesting an invite meeting by  Outlook calendar . In that case when the receiver accept the request invite it generate an error "domain not found". In this scenario the EDGE server is not able to rewrite also the calendar MIME request  (V-card) header inside the message so when the receiver accepts the request the reply [mechanism] will fails as will be used the email address of the local user  [domain not exposed ] and not the external one.
      I was wondering if Autoit is able to manipulate somehow the invite heade- calendar MIME (V-card))- re-witing the user email.   Thanks    
    • By Page2PagePro
      Excel VBA's IDE registers a Control-y as "cut this line of code".
      For those prone to Undo/Redo (Ctrl+Z/Ctrl+Y) you may find frustration when your code in the editor does not redo, but in fact clears your active line of code while killing redo history.
      Though not perfect, I keep this tool running in background on startup.
      The purpose is to allow Cltr+Y to act normally throughout Windows and Office and only interact *differently* with the "Microsoft Visual Basic for Applications" window that is active.
      If the Standard Menu bar exists, it'll try to click the ReDo (Blue Arrow to the right), else "Alt+e, r" keystrokes (less desired).
       
      Here's the code:
      Opt('MustDeclareVars', 1) Opt("WinTitleMatchMode", 1) HotKeySet("^y", "TriggerRedo") While 1 Sleep(10) WEnd Func TriggerRedo() ConsoleWrite("TriggerRedo()" & @CRLF) Local $title = "Microsoft Visual Basic for Applications - " Local $hWnd If WinExists($title) And WinActive($title) Then ;~ Parent Window Handle $hWnd = WinGetHandle($title) Local $aWindowPos = WinGetPos($hWnd) ;~ Control Bar Handle, Position and If Visible Local $sControlID = "[CLASS:MsoCommandBar; TEXT:Standard;]" Local $hStandardBar = ControlGetHandle($hWnd, "", $sControlID) Local $bIsVisible = ControlCommand($hWnd, "", $sControlID, "IsVisible") If $hStandardBar And $bIsVisible Then ConsoleWrite("Using Mouse Click." & @CRLF) ;~ Determine Redo button location on visible Control Bar Local $aBarPos = ControlGetPos($hWnd, "", $sControlID) Local $mX = $aWindowPos[0] + $aBarPos[0] + 217 + Int(23/2) Local $mY = $aWindowPos[1] + $aBarPos[1] + 27 + Int(22/2) MouseClick("Left", $mX, $mY, 1, 0) Else ConsoleWrite("Using VBA Send Keys." & @CRLF) $sControlID = "[CLASS:MsoCommandBar; TEXT:Menu Bar;]" Local $hMenuBar = ControlGetHandle($hWnd, "", $sControlID) ControlSend($hWnd, "", $hMenuBar, "!e") ;~ Send("r") $sControlID = "[CLASS:MsoCommandBarPopup; TEXT:Edit;]" Local $hPopupBar = ControlGetHandle($hWnd, "", $sControlID) ControlSend($hWnd, "", $hPopupBar, "r") EndIf Else ConsoleWrite("Using NATIVE Send Keys." & @CRLF) HotKeySet("^y") Send("^y") ;~ may cause "yyy..." when held HotKeySet("^y", "TriggerRedo") EndIf EndFunc ;==>TriggerRedo Hope this inspires someone.
       
       
    • By levila
      Hi guys, i would like to write a codding to send email with Bold/Italic text and new paragraph line. Example like below:
       
       
      Each line have 1 gab line.
      $oOApp = ObjCreate("Outlook.Application") $oOMail = $oOApp.CreateItem ($olMailItem) $oOMail.Save With $oOMail .To = ($to) .Subject = $subj .HTMLBody = "HEADLINE" & "Body Text" & "this is for testing purpose" .attachments.add ($attachment) .Send EndWith  
    • By PoojaKrishna
      Hi,
      I am trying to print items from Outlook in a Citrix machine. I am printing the items into PDF files using ‘Microsoft Print to PDF option’. I am using OutlookEX.au3 for printing items.
      Everything is working fine except handling the ‘Save Print Output As’ dialog appearing while trying to print the file to PDF.
      I have set the default printer to ‘Microsoft Print to PDF’ from the control panel ( Control Panel\All Control Panel Items\Devices and Printers) and using the following method to print the item.
      _OL_ItemPrint($oOutlook, $OL_Item) ;print item I am not able to handle the ‘Save Print Output As’ dialog initiated by the print statement in the Citrix machine. The WinActive method always returns false. I have tried with ControlSetText, ControlClick and Send methods also.
      Can anyone please help?
      Func _SavePDF($sFilePath) WinActivate ( "Save Print Output As", "") WinWaitActive ( "Save Print Output As", "",5 ) If WinActive("Save Print Output As") Then sleep(500) Send($sFilePath) sleep(500) Send("{ENTER}") sleep(500) EndIf EndFunc;=>_SavePDF  
×
×
  • Create New...