Lets Learn

Opinion Matters

Purging/Deleting a Huge/Large SharePoint List

Posted by Ankush on May 28, 2011

Recently I worked on a case where customer was trying to delete a huge list (it could be a simple list or workflow history list) and was looking for a custom solution to do it.

In case if it is a workflow history list then you can create a new list and set this as a history list and then you can try to delete the list items.

I started working on this and found a very good sample is already available on the web:


But if you look at the code, it gets the ListItem and then it reads the List Item id so that it can create a batch processing to delete the item.

Everything is fine but still the process was slow…I reserached on this and suggested few points which I wanted to highlight here:

1. When you delete the List Items from List, it goes to first recycle bin and then it goes to the second one. So you may want to turn this off when deleting the items. Sometimes it is not practle to delete the items..so think about the overhead.. ..delete the item from a list, then 1st recycle bin and then 2nd recycle bin.

2. Delete Items in batch. instead of deleting them as a big list, delete them in chunk. (for ex: start with 2000 items)

3. This is something not recommended but can be tried in one-off case. Basically you can read List Item ID from the database instead of reading it using Object Model.


using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Data.SqlClient;

namespace PurgeList
class Program
private static int iteration = 5;
private static int Count = 2000;

static void Main(string[] args)
if (args.Length != 1)
Console.WriteLine(“first argument: No of iteration”);
Console.WriteLine(“YourprgramName.exe 5”);
Console.WriteLine(“******Program terminated : Argument missing! Please give one argument ****”);

iteration = Convert.ToInt32(args[0]);

catch (Exception ex)
Console.WriteLine(“Error : Failed to convert arguments: {0}”, ex.Message);

return; // exit
Console.WriteLine(“Current System Time (Start) : ” + DateTime.Now.ToString());

for (int iterate = 0; iterate < iteration; iterate++)
int start = GetMinID();
Console.WriteLine(“Start Index: ” + start.ToString());
using (SPSite site = new SPSite(“http://anksps2010“))
using (SPWeb web = site.OpenWeb())
SPList wflist = web.Lists[“Workflow History”]; //workflow history
string wflistID = wflist.ID.ToString();

Console.WriteLine(“No of items before deletion: ” + wflist.ItemCount);

Console.WriteLine(“Building query… “);
StringBuilder batchString = new StringBuilder();
batchString.Append(“xml version=\”1.0\” encoding=\”UTF-8\”?>”);
int end = start + Count – 1;
for (int i = start; i <= end; i++)
batchString.Append(“<SetList Scope=\”Request\”>” + wflistID + “”);
batchString.Append(“<SetVar Name=\”ID\”>” + Convert.ToString(i) + “”);
batchString.Append(“<SetVar Name=\”Cmd\”>Delete”);




web.AllowUnsafeUpdates = true;
Console.WriteLine(“Executing query… “);
Console.WriteLine(“Batch Execution (Start) : ” + DateTime.Now.ToString());
string result = web.ProcessBatchData(batchString.ToString());
web.AllowUnsafeUpdates = false;

Console.WriteLine(“Batch Execution (END) : ” + DateTime.Now.ToString());

catch (Exception ex)
Console.WriteLine(“Process batch error : ” + ex.Message);

Console.WriteLine(“No of items before deletion: ” + wflist.ItemCount);
using (SPSite site1 = new SPSite(“http://anksps2010“))
using (SPWeb web1 = site1.OpenWeb())
Console.WriteLine(“No of item after deletion: ” + web1.Lists[“Workflow History”].ItemCount);


Console.WriteLine(“Current System Time (End) : ” + DateTime.Now.ToString());
Console.WriteLine(“———–Program Completed————“);
catch (Exception ex)
Console.WriteLine(“Error : ” + ex.Message);
Console.WriteLine(“******Program terminated due to error **** “);



public static int GetMinID()

string connString = @”Data Source=.\SHAREPOINT;Initial Catalog=WSS_Content_c20feb22657e4e2ab82f7db433f3e4c7;Integrated Security=SSPI”;

using (SqlConnection objConn = new SqlConnection(connString))

string sqlString = “Select min(tp_ID) as col1 from dbo.alluserdata where tp_listid = ‘6463BECE-3560-4D15-B965-B245F3203BEE'”;// workflow list id

SqlCommand cmd = new SqlCommand(sqlString, objConn);
int id = Convert.ToInt32(cmd.ExecuteScalar());
return id;

catch (Exception ex)
Console.WriteLine(“SQL ERROR: ” + ex.Message);
throw ex;




Try this and let me know how it goes!!!!!

Update # 1:

While working on the sample available here (http://code.msdn.microsoft.com/CleanWorkflowHistory), I hit a bug (or should I say a couple of bugs).

Bug # 1:PurgeSiteRecycleBin function again picks the item from the 1st stage (end-user) recycle bin. You need to specify ItemState to get the items from the second stage recycle bin. So currently, It deletes the item twice from the 1st stage recycle bin.

bug # 2: There is no need of PurgeSiteRecycleBin function at all. Basically, when you delete the item using batch processing, it goes into 1st stage re-cycle bin. But when you delete the item from re-cycle bin, it updates the content database so it will land in 2nd stage re-cycle bin. So unless, you want to empty the 2nd stage recycle bin,you can safely remove the code.


Posted in SharePoint 2010, Sp 2007 | Tagged: , , , , | Leave a Comment »

SharePoint Object Model Development and 64bit

Posted by Ankush on May 27, 2011

Recently i worked on a case where customer was using SharePoint object model on a 64bit machine in an ASP.NET application and it was failing. The code was failing when I tried to create SPSite object

Here is the error message:


The Web application at http://ank2007:45366 could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application


I could browse to this site without any problem and SharePoint is installed on the same box. I tried setting the target platform to x64,“Any CPU” , x86 but it didn’t help. I can very well create a webpart and can use the same code without any problem. So here is the environment details:

  • Your machine is 64 bit
  • MOSS 2007 is 64 bit
  • VS is 32 bit

So I started treoubleshooting the problem:

1. First I checked where is the server hosted (You can’t connect via OM to another SP instance on a different server.)
2. Ran the application as an administrator
3. I noticed that when I add reference for SharePOint.dll in Asp.Net web application, it reports an error about Search.dll.
4. As I was not using any serach part so I deleted the code using a post script

cd $(TargetDir)
del Microsoft.SharePoint.Search.dll
del Microsoft.SharePoint.Search.xml

After this I tried a lots of stuff – ProcMon, Fusion to see if there is any mismatch. But finally I got the solution 🙂

Basically when you try to debug the application, it try to load the 64bit MOSS dlls in 32 bit space (as VS is 32bit) and it fails with all the errors.  So this is what I did to make the deployed application to run in 64 bit:

Used http://support.microsoft.com/kb/894435 and tried this

ASP.NET 2.0, 64-bit version

To run the 64-bit version of ASP.NET 2.0, follow these steps:

  1. Click Start, click Run, type cmd, and then click OK.
  2. Type the following command to disable the 32-bit mode:

    cscript %SYSTEMDRIVE%\inetpub\adminscripts\adsutil.vbs SET W3SVC/AppPools/Enable32bitAppOnWin64 0

  3. Type the following command to install the version of ASP.NET 2.0 and to install the script maps at the IIS root and under:

    %SYSTEMROOT%\Microsoft.NET\Framework64\v2.0.50727\aspnet_regiis.exe -i

  4. Make sure that the status of ASP.NET version 2.0.50727 is set to Allowed in the Web service extension list in Internet Information Services Manager.


After this, I checked the app pool identity and matched it with the SharePoint ‘s app pool identity and it worked after that.

And it worked like a charm 🙂



Posted in Sp 2007 | Tagged: , , , | 3 Comments »

Question # 9 : You want to cache objects (say, from a database) but still allow them to be garbage collected when necessary

Posted by Ankush on April 25, 2011

Here comes another question after a long wait 🙂

any possiable solution?

Posted in Questions | Tagged: , | Leave a Comment »

libOPC version 0.0.1 is released

Posted by Ankush on April 25, 2011

Source: http://blogs.msdn.com/b/dmahugh/archive/2011/04/19/libopc-version-0-0-1-released.aspx

Text taken from above article:

The first release of libOPC, a new API for Open XML development, was published on Codeplex last week. This API is the first open-source cross-platform API for developers working with Open Packaging Convention (OPC) packages as used by Open XML, XPS, and other formats. Full source code is available, and it’s written in portable C99, so can be used on all popular variants of Linux/Unix, Mac OS, Windows, Android, and many other platforms. The API uses other common cross-platform open-source APIs for some of the low-level details, including ZLIB for opening ZIP-compressed packages and libXMLfor parsing the XML streams from the parts in the package.

Historically, there have been two popular .NET APIs for Open XML development: System.IO.Packaging (which first appeared in .NET 3.0) and the Open XML SDK, released in early 2007. There’s also a COM-based native packaging API available for non-.NET Windows developers.

The libOPC API is roughly analogous to System.IO.Packaging, in that it’s focused on the details of OPC and MCE (parts 2 and 3 of IS 29500), but doesn’t provide higher-level abstractions for WordprocessingML, SpreadsheetML or PresentationML (as covered in parts 1 and 4 of IS 29500).  I say “roughly” because libOPC doesn’t yet address some of the things that System.IO.Packaging handles (e.g., digital signatures) but does include some more advanced capabilities not available in System.IO.Packaging, such as the opc_generate functionality described below, which is essentially the same as the document reflector functionality of the Open XML SDK.

The key new feature in libOPC is its cross-platform capabilities. If you’re working on a non-Microsoft platform, or working with embedded systems that have limited OS support for XML and ZIP, you now have a very fast, simple API that you can use to implement Open XML read and write capabilities in your applications. And libOPC is designed from the ground up to be wrapper-friendly, for use from programming languages other than C.

For full read, click on the above link!!

Posted in Open XML SDK | Tagged: , , | Leave a Comment »

Debug a SharePoint Workflow

Posted by Ankush on April 25, 2011

Its been a while since I blogged anything. Main reason was, I was on vacation and since then I have come back, I was really really busy.  It was really good to visit places like Mathura and Muzaffarnagar (my birthplace). Anyway, here I am with my first blog after my vacation.

Recently I worked on case where customer has developed a workflow to send multiple emails (using SMTP Server) when an item was added to the list. The workflow was getting fired but it sending emails only for the first list item. For the second list item, it was kind of stuck(because there was no email sent for the second list item and so forth).

I started with very basic troubleshooting steps, like checking ULS logs, Event Viewer and nothing major was there.  I wish I could have debugged the workflow but it was not possible to install Visual Studio on the problematic server. I wish I could have got workflow debug information and then I got one. As we know it is very painful to debug the workflow. So the easy thing is, open
the web.config and have these entries present

<add name=”System.Workflow LogToFile” value=”1″ />
<add name=”System.Workflow.Runtime” value=”All” />
<add name=”System.Workflow.Runtime.Hosting” value=”All” />
<add name=”System.Workflow.Runtime.Tracking” value=”All” />
<add name=”System.Workflow.Activities” value=”All” />
<add name=”System.Workflow.Activities.Rules” value=”All” />

This will create a log file under C:\WINNT\system32\inetsrv.

Once you have the log file, you can view what activates being executed and if there was any error. And this really helped.  I was able to pin point one activity where the second workflow instance was stuck and it turns out that customer was keeping a constant task id. We changed this to create an new one for each workflow instance and it worked.

Try this in your workflow projects and see if this helps!! And if it does, post a comment here.!!!

Posted in Sp 2007 | Tagged: , , , | Leave a Comment »

How to Insert a Hyperlink in Excel using Open XML SDK 2.0

Posted by Ankush on March 4, 2011

Consider a scenario, you want to insert a hyperlink in Excel 2010 (or Excel 2007) file programmatically using a web application. You should not use Office Object Model in a web application, Please refer http://support.microsoft.com/kb/257757. So what we can do??

As we know that Office 2007 and 2010 file format is XML based (just rename any Office 2007/2010 document to .zip and you can see a lots of XML files which makes a document and they are simply zipped in a one file). To manipulate these files, MS has introduced a library, Open XML SDK 2.0. The SDK is built on the System.IO.Packaging API and provides strongly-typed classes to manipulate documents that adhere to the Office Open XML File Formats Specification. The Office Open XML File Formats specification is an open, international, ECMA-376, Second Edition and ISO/IEC 29500 standard. The Open XML file formats are useful for developers because they are an open standard and are based on well-known technologies: ZIP and XML.

The Open XML SDK 2.0 simplifies the task of manipulating Open XML packages and the underlying Open XML schema elements within a package. The Open XML SDK 2.0 encapsulates many common tasks that developers perform on Open XML packages, so that you can perform complex operations with just a few lines of code. Some benefits if you use Open XML SDK:

Open Format: These formats use XML and ZIP, and they are fully documented. Anyone can get the full specs on the formats and it is a royalty free license for anyone that wants to work with the files.

Compressed: Files saved in these new XML formats are less than 50% the size of the equivalent file saved in the binary formats. This is because we take all of the XML parts that make up any given file, and then we ZIP them. We chose ZIP because it’s already widely in use today and we wanted these files to be easy to work with.

Robust: Between the usage of XML, ZIP, and good documentation the files get a lot more robust. By compartmentalizing our files into multiple parts within the ZIP, it becomes a lot less likely that an entire file will be corrupted (instead of just individual parts). The files are also a lot easier to work with, so it’s less likely that people working on the files outside of Office will cause corruptions.

Backward compatible: You don’t have to use the new version of Office to take advantage of these formats. You can use Office Compatibility pack to open the Office 2007 document in older version.

Binary Format support: You can still use the current binary formats with the new version of Office. In fact, people can easily change to use the binary formats as the default if that’s what they’d rather do.

New Extensions: The new formats has new extensions (.docx, .pptx, .xlsx) so you can tell what format the files you are dealing with are, but to the average end user they’ll still just behave like any other Office file. Double click & it opens in the right application.

Is also important to know that what SDK CAN’T do:

The Open XML SDK 2.0:

  • Does not replace the Microsoft Office Object Model and provides no abstraction on top of the file formats. You must still understand the structure of the file formats to use the Open XML SDK 2.0.
  • Does not provide functionality to convert Open XML formats to and from other formats, such as HTML or XPS.
  • Does not guarantee document validity of Open XML Formats when you use the Open XML SDK 2.0 or if you decide to manipulate the underlying XML directly.
  • Does not provide application behavior such as layout functionality in Word or recalculation, data refresh, or adjustment functionalities in Excel.

So lets have a look at the solution. I will try to build the solution from the scratch so that you can learn how you can use Open XML tools and leverage this information in other exercise as well.

So lets start!!

Step 1: Download the Open XML SDK and the tool from http://www.microsoft.com/downloads/en/details.aspx?FamilyId=C6E744E5-36E9-45F5-8D8C-331DF206E0D0&displaylang=en. Please note that there are 2 links, one is for the tool and the second one is the actual library.

Step 2: So the objective is to insert a link into a cell. For the simplicity, we will assume that we want to insert the link into cell A1 of Sheet1. Lets create an Excel 2010 workbook and place a hyperlink in cell A1 manually. Once done, Excel file will look like this


Save this file and close it.

Step 3: If you haven’t changed the default installation location of the tool, they will appear here: C:\Program Files (x86)\Open XML SDK\V2.0\tool. Go ahead and run OpenXmlSdkTool.exe from here.

Step 4: You can simply open Office file in this tool and look at the different XML parts. (To understand different XML parts, have a look:https://ankushbhatia.wordpress.com/2010/03/04/basics-of-spreadsheet/). So go ahead and open the file in this tool. Once done, it will look like this:


Step 5: We know that we inserted the hyperlink in cell A1 of Sheet1 so go ahead and browse the Sheet1.xml file. Once you select the Sheet1, it gives you different child elements of it. Please select hyperlinks and click on “reflect Code” to view the XML. Make sure to follow the correct tree structure to get to Sheet1.xml and hyperlink object. After this, it will appear as:


Pay attention to the right side. There are 2 windows. Upper window shows XML of the section you have selected in the “Document Explorer” and the bottom window shows the C# code to generate the XML..Kind of cool….isn’t?? So now what we have..? We have XML which is required to generate a hyperlink and the equivalent C# code. Lets have a closure look at the XML

<x:hyperlinks xmlns:x=”http://schemas.openxmlformats.org/spreadsheetml/2006/main”&gt;

<x:hyperlink ref=”A1″ r:id=”rId1″ xmlns:r=”http://schemas.openxmlformats.org/officeDocument/2006/relationships&#8221; />


There is no information about the hyperlink text. If you look at the relationship ID “A1″, it appears as

<?xml version=”1.0″ encoding=”UTF-8″ standalone=”true”?>

<Relationships xmlns=”http://schemas.openxmlformats.org/package/2006/relationships“><Relationship TargetMode=”External” Target=”http://www.microsoft.com/” Type=”http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink” Id=”rId1″/></Relationships>

So where is the actual text (remember I entered Microsoft)?

As you know that Excel keeps all the text strings in a separate XML file, SharedSTring.xml and refer it back based on the index. So lets see how the data appear in cell A1

Have a look at the SheetData [where it keeps all the data . It is specific to a sheet] in the tool


If you look at the XML, it contains the information about the row which contains the data.

<x:sheetData xmlns:x=”http://schemas.openxmlformats.org/spreadsheetml/2006/main”&gt;

<x:row r=”1″ spans=”1:1″ x14ac:dyDescent=”0.25″ xmlns:x14ac=”http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac”&gt;

<x:c r=”A1″ s=”1″ t=”s”>





Lets try to decode it.

<x:row r=”1″ spans=”1:1″ x14ac:dyDescent=”0.25″ xmlns:x14ac=”http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac”&gt; :

r=1 means there is one row.

There is one child element <x:c r=”A1″ s=”1″ t=”s”>.

X:c -> Column
r=A1 -> Referenced cell is “A1″

S=1 and t=”S” it’s a string value and value is referenced from Shared String table. And S=1 means that a style is applied and the defination of the style is taken from index 1 which can be found from Style.xml

Now analyze the SharedSTring.XML using the same tool


Here you will see the Microsoft string. It is referred back in SheetData using x:v node. The starting index is 0.

So to summarize, we need to do:

  1. Create a hyperlink object and insert it before the PageMargin node. Please note that Excel is enforcing a strict ordering of the elements. So make sure to add it before PageMargines node.
  2. Create an entry in the SharedString Table
  3. Create an entry in the SheetData section

So why we did this exercise. Every now and then we face situation where we want to these kind of things in Office using Open XML. So easy thing is, do the stuff in UI and analyze the XML and C# code and then write your own.

Lets move on and utilize this information.

Step 6: Create a new Excel file and save it. We will insert a hyperlink in this file. Create a new VS 2010 project and open the new blank file.

Step 7: Add a reference to DocumentFormat.OpenXML and WindowsBase.dll from .NET tab

Step 8: Add a namespace using XL = DocumentFormat.OpenXml.Spreadsheet;

Step 9: So basically you need to

a. Open the workbook
b. Get to the sheet where you want to add the hyperlink
c. Add the hyperlink
d. Add an entry in SheetData
e. Add an entry in SharedString.xml

The below code does exactly that.

using (SpreadsheetDocument document = SpreadsheetDocument.Open(@"C:\Users\abhatia\Desktop\Sample1.xlsx", true))


               IEnumerable<Sheet> sheets = document.WorkbookPart.Workbook.GetFirstChild<Sheets>().Elements<Sheet>().Where(s => s.Name == "Sheet1");

               if (sheets.Count() == 0)


                   // The specified worksheet does not exist.



               string relationshipId = sheets.First().Id.Value;

               WorksheetPart worksheetPart = (WorksheetPart)document.WorkbookPart.GetPartById(relationshipId);

               Hyperlinks hyperlinks1 = new Hyperlinks();

               Hyperlink hyperlink1 = new Hyperlink() { Reference = "A1", Id = "UNIQUE" };


               PageMargins pageMargins = worksheetPart.Worksheet.Descendants<PageMargins>().First();

               worksheetPart.Worksheet.InsertBefore<Hyperlinks>(hyperlinks1, pageMargins);


               worksheetPart.AddHyperlinkRelationship(new System.Uri("http://www.google.com/", System.UriKind.Absolute), true, "UNIQUE");

               // Create an element in SheetData

               // Get the SharedStringTablePart. If it does not exist, create a new one.

               SharedStringTablePart shareStringPart;

               if (document.WorkbookPart.GetPartsOfType<SharedStringTablePart>().Count() > 0)


                   shareStringPart = document.WorkbookPart.GetPartsOfType<SharedStringTablePart>().First();




                   shareStringPart = document.WorkbookPart.AddNewPart<SharedStringTablePart>();


               // Insert the text into the SharedStringTablePart.

               int index = InsertSharedStringItem("www.google.com", shareStringPart);

               // Insert cell A1 into the new worksheet.

               Cell cell = InsertCellInWorksheet("A", 1, worksheetPart);

               // Set the value of cell A1.

               cell.CellValue = new CellValue(index.ToString());

               cell.DataType = new EnumValue<CellValues>(CellValues.SharedString);

               // Save the new worksheet.




Here are the helper functions (taken from MS site):

// Given text and a SharedStringTablePart, creates a SharedStringItem with the specified text 

// and inserts it into the SharedStringTablePart. If the item already exists, returns its index.

private static int InsertSharedStringItem(string text, SharedStringTablePart shareStringPart)


    // If the part does not contain a SharedStringTable, create one.

    if (shareStringPart.SharedStringTable == null)


        shareStringPart.SharedStringTable = new SharedStringTable();


    int i = 0;

    // Iterate through all the items in the SharedStringTable. If the text already exists, return its index.

    foreach (SharedStringItem item in shareStringPart.SharedStringTable.Elements<SharedStringItem>())


        if (item.InnerText == text)


            return i;




    // The text does not exist in the part. Create the SharedStringItem and return its index.

    shareStringPart.SharedStringTable.AppendChild(new SharedStringItem(new DocumentFormat.OpenXml.Spreadsheet.Text(text)));


    return i;



// Given a column name, a row index, and a WorksheetPart, inserts a cell into the worksheet. 

       // If the cell already exists, returns it. 

       private static Cell InsertCellInWorksheet(string columnName, uint rowIndex, WorksheetPart worksheetPart)


           Worksheet worksheet = worksheetPart.Worksheet;

           SheetData sheetData = worksheet.GetFirstChild<SheetData>();

           string cellReference = columnName + rowIndex;

           // If the worksheet does not contain a row with the specified row index, insert one.

           Row row;

           if (sheetData.Elements<Row>().Where(r => r.RowIndex == rowIndex).Count() != 0)


               row = sheetData.Elements<Row>().Where(r => r.RowIndex == rowIndex).First();




               row = new Row() { RowIndex = rowIndex };



           // If there is not a cell with the specified column name, insert one.  

           if (row.Elements<Cell>().Where(c => c.CellReference.Value == columnName + rowIndex).Count() > 0)


               return row.Elements<Cell>().Where(c => c.CellReference.Value == cellReference).First();




               // Cells must be in sequential order according to CellReference. Determine where to insert the new cell.

               Cell refCell = null;

               foreach (Cell cell in row.Elements<Cell>())


                   if (string.Compare(cell.CellReference.Value, cellReference, true) > 0)


                       refCell = cell;




               Cell newCell = new Cell() { CellReference = cellReference };

               row.InsertBefore(newCell, refCell);


               return newCell;





I have made several assumptions while writing code for this.

  1. I have assumed that Excel file is a new blank file i.e. there is no hyperlink present.
  2. Sheet1 name is hardcoded.
  3. File Name is Hardcoded.
  4. Can you think of anything by looking at the code? 🙂

Run this code and everything will work. When you will open the Excel sheet and click on the link, it will be treated as hyperlink.

But still one thing is missing. It is not underlined and appear in the blue color. Because you need to apply a style to do that. I leave this exercise to my readers to check how the styles are applied and how they can use this information. In case of anything, let me know 🙂

Please try this approch in every situation where you use Open XML and let me know in case of any questions.

Posted in Open XML SDK, Spreadsheet | Tagged: , , , | 9 Comments »

Question 8 # How to get the Return Values from Each Delegate in a Multicast Delegate

Posted by Ankush on March 3, 2011

Lets say you have created a multicast delegate.  You need to be able to access the return value of each delegate that is invoked in the multicast delegate.

How will you do that?

Posted in Questions | Tagged: , , | Leave a Comment »

Clear Office Clipboard Programmatically

Posted by Ankush on March 1, 2011

Lets say you are automating Word and looking for a way to clear an Office Clipboard.


Have a look in the left of the screen shot and it will show you the Office Clipboard. So you look into the Office Object Modal but there is no method/property to clear the clipboard.

Any solutions? Our old friend, win32 API come for the rescue.

Here is the solution:

Step 1: Declare the namespaces

   1: Imports Word = Microsoft.Office.Interop.Word

   2: Imports System.Runtime.InteropServices


Step 2:

Declare the local variables and APIs

   1: Private Const WM_LBUTTONDOWN As Long = &H201&

   2:    Private Const WM_LBUTTONUP As Long = &H202&


   4:    ' Variables to hold Word Objects

   5:    WithEvents oAppWD As Word.Application

   6:    Public oDoc As Word.Document


   8:    ' API to be used

   9:    Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Int32)

  10:    Private Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hWnd1 As Int32, ByVal hWnd2 As Int32, ByVal lpsz1 As String, ByVal lpsz2 As String) As Int32

  11:    Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Int32

  12:    Private Declare Function PostMessage Lib "user32.dll" Alias "PostMessageA" (ByVal hWnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32

  13:    Declare Function BringWindowToTop Lib "user32" (ByVal hWnd As Int32) As Int32

Step 3: Actual code

   1: Private Function MakeLong(ByVal nLoWord As Integer, ByVal nHiWord As Integer) As Int32

   2:        MakeLong = nHiWord * 65536 + nLoWord

   3:    End Function



   6:    Private Sub btnClearWord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearWord.Click

   7:        Dim hMain As Int32, hWord As Int32, hClip As Int32, hWindow As Int32, hParent As Int32

   8:        Dim lParameter As Int32

   9:        Dim sTask As String

  10:        Dim HWND As Int32


  12:        'Open the selected File

  13:        oAppWD = DirectCast(System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application"), Word.Application)

  14:        oAppWD.Visible = True

  15:        oDoc = oAppWD.ActiveDocument


  17:        oAppWD.Activate()

  18:        oDoc.Activate()

  19:        oDoc.ActiveWindow.SetFocus()

  20:        oDoc.ActiveWindow.Activate()


  22:        Sleep(2000)

  23:        MessageBox.Show("Doing it.....")


  25:        HWND = FindWindow("OpusApp", vbNullString)


  27:        ' Make Office Clipboard Visible

  28:        oAppWD.CommandBars("Office Clipboard").Visible = True


  30:        BringWindowToTop(HWND)


  32:        ' Get the handles of the respective Windows Of the Office 

  33:        sTask = "Office Clipboard"

  34:        hMain = HWND

  35:        hWord = FindWindowEx(hMain, 0, "MsoCommandBarDock", vbNullString)


  37:        hParent = hWord : hWindow = 0

  38:        hWindow = FindWindowEx(hParent, 0, "MsoCommandBar", sTask)

  39:        If hWindow Then

  40:            hParent = hWindow : hWindow = 0

  41:            hWindow = FindWindowEx(hParent, 0, "MsoWorkPane", vbNullString)

  42:            If hWindow Then

  43:                hParent = hWindow : hWindow = 0

  44:                '   hClip = FindWindowEx(hParent, 0, "bosa_sdm_Microsoft Office Word 12.0", vbNullString)

  45:                hClip = FindWindowEx(hParent, 0, "bosa_sdm_msword", vbNullString)



  48:            End If

  49:        End If



  52:        If hClip = 0 Then

  53:            MsgBox("Cant find Clipboard window")

  54:            Exit Sub

  55:        End If

  56:        ' Pass the message 120,18 are the respective co-ordinates of the Clear all button.

  57:        lParameter = MakeLong(120, 18)

  58:        ' Send the Message

  59:        Call PostMessage(hClip, WM_LBUTTONDOWN, 0&, lParameter)

  60:        Call PostMessage(hClip, WM_LBUTTONUP, 0&, lParameter)

  61:        Sleep(100)


  63:    End Sub

Try this and let me know how it goes for you Smile

Posted in Office 2007, Word | Tagged: , , , | 2 Comments »

Open XML SDK Learning Resources

Posted by Ankush on February 24, 2011

Open XML SDK 2.0 FAQ

Building Office Open XML Files

Introducing the Office (2007) Open XML File Formats

How to: Manipulate Office Open XML Formats Documents

Building Server-Side Document Generation Solutions Using the Open XML Object Model
http://msdn.microsoft.com/en-us/library/bb735940.aspx (Part 1 of 2)
http://msdn.microsoft.com/en-us/library/bb735939.aspx (Part 2 of 2)

How to use the Office XML file format and the packaging components from the .NET Framework 3.0 to create a simple Excel 2007 workbook or a simple Word 2007 document

How Do I…Open XML SDK v2.0

Nanipulating Excel 2007 and PowerPoint 2007 Files with the Open XML Object Model (Part 1 of 2)

Manipulating Excel 2007 and PowerPoint 2007 Files with the Open XML Object Model (Part 2 of 2)

Reading Data from SpreadsheetML

How to use components of the .NET Framework 3.0 to create and then to stream an Office Word 2007 document and an Office Excel 2007 workbook to a client computer

Welcome to the Open XML Developer Workshop

Open XML Explained e-book

Good Site to Search ECMA standard online :

Open XML SDK Documentation

Open XML Format SDK 2.0: Getting Started Best Practices

Office Open XML Formats: Retrieving Lists of Excel 2007 Worksheets

Manipulating Excel 2007 and PowerPoint 2007 Files with the Open XML Format API (Part 1 of 2)

Manipulating Excel 2007 and PowerPoint 2007 Files with the Open XML Format API (Part 2 of 2)

2007 Office System Sample: Open XML Format SDK 2.0 Code Snippets for Visual Studio 2008

Writing Large Excel Files with the Open XML SDK

Parsing and Reading Large Excel Files with the Open XML SDK

How to efficiently generate OpenXML Workbook from Excel files containing very large amount of data

Very very very good blogs to through..Its must if you would like to know more about formats:

Where is the documentation for Office’s docx/xlsx/pptx formats? Part 1: Office 2007

Where is the documentation for Office’s docx/xlsx/pptx formats? Part 2: Office 2010

I’ll keep on updating this list and will try to categorize it as it becomes bigger 🙂

Posted in Open XML SDK | Tagged: , , , , | 4 Comments »

Question 7: How to find whether a DLL or EXE is managed or unmanaged

Posted by Ankush on February 24, 2011

What is best way to check if an assembly is managed or not?



Posted in Questions | Tagged: , | 2 Comments »

%d bloggers like this: