Export Android contacts contacts2.db to vCard vcf on Windows

The other day I need to restore some contacts on an Android phone, I had Titanium Backup files, though restoring the contacts through there didn’t seem to work.

I searched for a solution but there wasn’t really anything for Windows to restore the contacts so I developed my own VBA script.

I will assume you already have a backup contacts2.db file, it is found the zip file com.android.providers.contacts.

Firstly, download SQLite Manager add-on for Firefox, run it, Connect Database contacts2.db. Under Tables, right-click data table and chose Export Table click OK and save as data.csv.

Download convert-contacts2.db data-to-vcard-vcf.xlsm, open, and enable macros if necessary. Run the macro convertDatatoContacts and select the data.csv file.

You should now have a list of contacts, delete any contacts that you don’t want to import by deleting the whole row. When done run the macro and a vCard.vcf file will be created in the same folder as the Excel WorkBook.

Copy vCard.vcf to the root  directory of your Android phone. On the phone enter Contacts, Settings, Import/Export and Import from storage. The contacts will be imported.


VBA automatically saves Excel 2003 Workbook in compatibility mode as Excel 2007 Workbook

Lets say you have a neat little Excel 2003 macro, when you run your macro in Excel 2007, Excel runs it in Compatibility Mode, and any benefits (such as the 16384 columns) you were hoping to use are still unavailable. So how can we enable these benefits depending on the Excel version? Simply by including the following code in the Auto_Open subroutine.

Public Sub auto_open()
    'add some smarts if opened in Excel 2007 or later
    Dim oldFileName As String
    oldFileName = ThisWorkbook.Name
    Dim tempi As Integer 'used to store position of '.' before file extension in workbook file name
    tempi = InStrRev(oldFileName, ".xls", -1, vbTextCompare)
    If Application.Version > 11 And Len(oldFileName) - tempi = 3 Then 'assume running in compatability mode
        Application.DisplayAlerts = False
        Dim newFileName As String
        newFileName = Mid$(oldFileName, 1, tempi) & "xlsm"
        If fileExist(newFileName) Then 'if the new workbook version already exists, then open it and close this one
            'open the new workbook by emulating double clicking the file, as this is the only way to run the auto_open
            Shell "Excel """ & ThisWorkbook.Path & "\" & newFileName & """", 3 '3 = vbMaximizedFocus
            'close this Excel application
        Else 'if the new workbook version doesn't exist, then save it as new workbook version
            'save as macro enables office 2007 workbook
            ActiveWorkbook.SaveAs Filename:=newFileName, FileFormat:=52, CreateBackup:=False '52 = xlOpenXMLWorkbookMacroEnabled
            'create a timer to call the same new workbook as it will be now opened in non compatibility mode
            Application.OnTime Now + TimeValue("00:00:01"), "auto_open"
        End If
        Application.DisplayAlerts = True
    End If
End Sub

Some brief info on how this works, so far the only way I have found to run in Non-Compatibility Mode is to save the Workbook as an Excel Macro Enabled Workbook and reopen the file, or if the file already exists then open that file and close the Excel 2003 Workbook. The main problem is the showing of a UserForm on the reopen or existing open. The bove code is the only way I have found so far, and it involves some trickery. It can be simplified if portions are separated into the Workbook_Activate subroutine, but I wanted to provide a copy and paste solution with minimal fiddling around. The only change in the above code is the UserForm1 name.

One of the problems was the Workbook_Activate or Auto_open not running when called via the normal Open method. I actually had to ask ExpertsExchange, the expert was very helpful.

Any comments or suggestions welcome as always.