Mike's Blog


Business Intelligence keeps me live and communicating!

Using CopySourceAsHTML in a Windows 2003 VPC environment

Introduction
CopySourceAsHTML will give you an error while using it in a Microsoft Virtual PC (I'm probably the last Mohican whose using MS VPC, but that's mine problem). The exact message is "Requested Clipboard operation did not succeed".


Error message

While searching the Internet I found lots of people complaining about this error using all kinds of apps (including in MOC labs of Microsoft which use a VPC). For me this error only occurs during this add-in. In my last blog I already talked about debugging an add-in is easy, so I was ready to solve this problem.

CopySourceAsHTML
Code is exponentially more readable when certain parts of that code are differentiated from the rest by using a different color text. Reading code in Visual Studio is generally much easier than trying to read code in an editor like Notepad.

Chances are you may have your own blog by now, or at least have spent some time reading them. Normally, when you try to post a cool code snippet to your blog it ends up being plain old text, which isn't the easiest thing to read. This is where the CopySourceAsHTML add-in comes in to play. This add-in allows you to copy code as HTML, meaning you can easily post it to your blog or Web site and retain the coloring applied through Visual Studio.

After installing the CopySourceAsHTML add-in, simply select the code you want to copy and then select the Copy Source as HTML command from the right-click menu. After selecting this option you will see the dialog box.


Options for CopySourceAsHTML

From here you can choose what kind of HTML view you want to create. This can include line numbers, specific tab sizes, and many other settings. Of course I use Jan Schreuder's style guide for the file style. This style creates an easy browsable frame inside your page

border-top: windowtext 1pt solid;
border-top-color: #CCCCCC;
padding-top: 1pt;
border-left: windowtext 1pt solid;
border-left-color: #CCCCCC;
padding-left: 1pt;
border-right: windowtext 1pt solid;
border-right-color: #CCCCCC;
padding-right: 1pt;
border-bottom: windowtext 1pt solid;
border-bottom-color: #CCCCCC;
padding-bottom: 1pt;
width: 100%;
overflow: auto;
background-color: #F5F5F5;
Jan's File Style

After clicking OK, the HTML is saved to the clipboard. For instance, suppose you were starting with the following code snippet inside Visual Studio:

namespace mynamespace.Service
{
[WebService(Namespace="http://domain/Application/Service/V1.0")]
public class MyService : System.Web.Services.WebService
{
...
[WebMethod]
public string Foo()
{ ... }
[WebMethod]
public void Bar(string s1, string s2)
{ ... }
...
}
}

Normal Copied Code

After you select Copy As HTML and configure the HTML to include line numbers, this code will look like this.

namespace mynamespace.Service
 {
   [WebService(Namespace="http://domain/Application/Service/V1.0")]
   public class MyService : System.Web.Services.WebService
   {
   ...
 
     [WebMethod]
     public string Foo()
     { ... }
 
     [WebMethod]
     public void Bar(string s1, string s2)
     { ... }
 
   ...
   }
 }
CopySourceAsHTML Code

Modify CopySourceAsHTML  
Reading this blog Copying Visual Studio code snippets to the clipboard as HTML I found a nice conversation between the owners of "FormatToHtml macro" and "CopySourceAsHTML". When I was reading this configuration I saw one of my blogger's Bryant Likes also mentioned the error I described. On another forum Requested Clipboard operation did not succeed, people were complaining about the same error as well. While reading all the post, someone mentioned it could be that the clipboard is locked for the current thread, which can be solved by creating a new thread. I introduced the new thread into the add-in and it's working perfectly. In the next frames I will show you the modifications.

namespace JTLeigh.Tools.CopySourceAsHtml
{
 
    internal static class Clipboard
    {
 
        public static string GetRtf()
        {
            if (!System.Windows.Forms.Clipboard.ContainsText(TextDataFormat.Rtf))
            {
                return null;
            }
            return System.Windows.Forms.Clipboard.GetText(TextDataFormat.Rtf);
        }
 
        public static void SetHtml(string html)
Clipboard.cs original source

namespace JTLeigh.Tools.CopySourceAsHtml
{
 
    internal static class Clipboard
    {
 
        private static string _rtf = null;
 
        public static string GetRtf()
        {
            return _rtf;
        }
 
        private static void GetClipboardPrivate()
        {
            if (!System.Windows.Forms.Clipboard.ContainsText(TextDataFormat.Rtf))
            {
                _rtf = null;
            }
            _rtf = System.Windows.Forms.Clipboard.GetText(TextDataFormat.Rtf);
    }
 
        public static void StartThread()
        {
            Thread thread = new Thread(new ThreadStart(GetClipboardPrivate));
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            thread.Join();
        }
 
        public static void SetHtml(string html)
Clipboard.cs modified source

try
                {
                    Clipboard.StartThread();
                    rtf = Clipboard.GetRtf();
                    if (rtf == null)
                    {
                        MessageBox.Show(Resources.NoRtfDataOnClipboardMessage, Resources.NoRtfDataOnClipboardCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
                catch (ExternalException)
                {
                    MessageBox.Show(Resources.UnableToAccessClipboardMessage, Resources.UnableToAccessClipboardCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
Connect.cs modified source

Compile the new code and overwrite the current CopySourceAsHtml.AddIn in \Documents and Settings\<user name>\My Documents\Visual Studio 2005\Addins.

Summary
Anything that makes it easier to share and understand code benefits all of us as it means more people will go to the trouble of sharing knowledge and learning from each other. CopySourceAsHTML was written by Colin Coller and can be downloaded from www.jtleigh.com/CopySourceAsHtml. I will notice Colin to show him mine solution to use his add-in in Virtual PC environment. Meanwhile I will ask the moderator of this site, also a fan of this add-in to post a compiled dll, so other users can easily use this add-in in a VPC.

You can download an already modified DLL here.

Comments

Mike's Blog said:

Since my colleagues Dennis and Alex are writing articles by the minute, it was time for me to do the
# November 15, 2006 9:39 AM

Colin said:

Mike, Thanks for the plug, and for the excellent fix! I'll definitely include this in the next release. :) Colin
# November 23, 2006 5:50 PM

David Truxall said:

I kept getting an error when trying to use CopySourceAsHTML in Visual Studio 2005. The error was that...

# January 15, 2007 12:15 PM

dmwc said:

看了許多人在討論source code的時候,總是行號、顏色和格式都相當整齊,一定是是利用了某個特殊工具軟體,在偶然的機會,終於發現原大家是使用了 CopySourceAsHtml for VisualStudio

# February 13, 2007 2:00 AM

Khal Danh (.NET Newbie) said:

Don't forget to import "System.Threading" when updating Connect.cs.

# April 16, 2007 9:43 AM

Pascal Naber said:

For the Summer Classes we use VS.NET beta 2 in a VPC image. For this reason I compiled the CopySourceAsHtml

# August 14, 2007 3:34 PM

Jesse Ezell Blog said:

When performing Clipboard operations (SetDataObject, SetImage, etc.) in a .NET program under VPC, you

# January 25, 2008 4:43 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Please add 4 and 7 and type the answer here: