Skype Chat History Export using C# and Interop

Ever wanted to export Skype chat history to text or xml files? There is an solution for that as it seems that there is no such a option in new Skype version.

It is possible thanks to Skype Interop object which allows us to interact and extend the application functionality. In order to start we need to install that object on our machine (installation file is included at the bottom of this article) and add reference to it in our visual studio project.

skype-refence2

 

Next we need to instantiate the interop object and get the current logged in user. We also need to ensure that the Skype application is running, if not we need to start it up.

SKYPE4COMLib.Skype oSkype = new SKYPE4COMLib.Skype();
if (!oSkype.Client.IsRunning) oSkype.Client.Start(true, true);

var owner = oSkype.CurrentUser.FullName + " (" + oSkype.CurrentUser.Handle + ")";</pre>
Next we need to simply loop through all chats
1
foreach (SKYPE4COMLib.Chat oChat in oSkype.Chats)
{

and all chat messages for each member on our contact list

foreach (SKYPE4COMLib.ChatMessage oMessage in oChat.Messages)
{

In this place you can customize it; save it to database, xml files, do an action depending on the chat content etc.

foreach (SKYPE4COMLib.Chat oChat in oSkype.Chats)
{
    try
    {
        var name = oChat.Name;
        var description = oChat.Description;
        var members = new SortedList();
        foreach (SKYPE4COMLib.User oUser in oChat.Members)
        {
            members.Add(oUser.FullName + " (" + oUser.Handle + ")", null);
        }

        var filename = "\\chat";
        foreach (var m in members.Keys)
        {
            filename += " " + m;
        }
        filename = filename.Replace(owner, "").Replace("  ", " ");
        if (filename.Length > 100) filename = filename.Substring(0, 100);
        filename = filename.Trim() + ".txt";

        Console.WriteLine();
        Console.WriteLine("Chat File = " + filename);
        Console.WriteLine("With " + oChat.Messages.Count.ToString() + " Message(s)");

        filename = basePath + filename;

        var iMessageCount = oChat.Messages.Count;
        iCounter = 0;
        var messages = new StringBuilder(21000);
        foreach (SKYPE4COMLib.ChatMessage oMessage in oChat.Messages)
        {
            iCounter++;
            messages.Append(oMessage.Timestamp + "\t" + oMessage.FromDisplayName + "\t" + oMessage.Body + "\r\n");
            if (iCounter % 25 == 0) Console.Write(".");
            if (messages.Length > 20000)
            {
                System.IO.File.AppendAllText(filename, messages.ToString());
                messages = new StringBuilder(21000);
                GC.Collect();
            }
            if (iMessageCount - iCounter > 1000)
                if ((iMessageCount - iCounter) % 1000 == 0)
                {
                    Console.WriteLine("");
                    Console.Write((iMessageCount - iCounter).ToString() + " ");
                }
        }
        System.IO.File.AppendAllText(filename, messages.ToString());
        Console.WriteLine(" OK");
    }
    catch (Exception ex)
    {
        Console.WriteLine();
        Console.WriteLine("Error " + ex.Message);
        Console.WriteLine();
    }
}

The first time your application contacts Skype using Interop object, an Skype security message is displayed allowing to reject or access the Skype data.

skype-message

 

POST UPDATE!
If you are running x64 bit PC then you need to do following:

1. Register Skype interop using below command
C:\Windows\System32\regasm.exe "C:\Program Files (x86)\Common Files\Skype\Skype4COM.dll"

2. In Visual Studio you need to change target platform to x86

skype_x64

This works for me when running Skype version: 6.11.0.102

Enjoy!
SkypeChatHistoryExport

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...Loading...