Click here to Skip to main content
13,087,810 members (79,056 online)
Click here to Skip to main content
Add your own
alternative version


5 bookmarked
Posted 11 Mar 2009

JesCopyRightWriter - ITextDocument, ITextSelection Sample

, 11 Mar 2009
Rate this:
Please Sign up or sign in to vote.
A simple DevStudio Add-in to demonstrate Text Object Model (TOM)


JesCopyRightWriter is a simple DevStudio Add-in to demonstrate ITextDocument and ITextSelection Text Object Model(TOM) objects. This Add-in inserts a simple copy-right information file header in an opened file of VisualStudio 6.0 (SP5).


When I decided to submit some articles to CodeProject, I thought that it would be nice if I have an Add-in of my own that will insert file headers with copy-right information. When I searched for help on the internet, I found a lot of articles written in Visual Basic, C# etc., but not many in VC++. However, I got some ideas about TOM and its interfaces (Thanks also to Derek Lakin for his article WinDiff Visual Studio Add-in). After continuous attempts, I could write some working lines of code.

Using the Tool/Code

To configure the Add-in, follow the steps below:

  1. Download and save Add-in in local drive.
  2. Open Visual Studio.
  3. Select Tools -> Customize menu.
  4. Select Add-ins and Macro Files tab.
  5. Browse and select Add-in DLL and click on close button.
  6. Click on newly appeared toolbar button. This will add file header to presently opened file.

The code-snippet for retrieving presently open document name and adding new lines to this file is shown below:

STDMETHODIMP CCommands::JesCopyRightWriterCommandMethod()

	// Jes
    VERIFY_OK( m_pApplication->EnableModeless( VARIANT_FALSE ));
    // Retrieve active source file
    CComPtr<IDispatch> pIActiveDocDisp;
    m_pApplication->get_ActiveDocument( &pIActiveDocDisp );
    CComQIPtr<ITextDocument, &IID_ITextDocument> pActiveSrc( pIActiveDocDisp );
    if( pActiveSrc )
        CComPtr<IDispatch> pITextSelDisp;
        if( SUCCEEDED( pActiveSrc->get_Selection( &pITextSelDisp )))
            // Retrieve date information
            SYSTEMTIME stSysTime;
            GetSystemTime( &stSysTime );

            // Declare Copy-right information lines
            const int NUM_COPY_RIGHT_INFO_LINES = 7;
            CString csCopyRightInfoLines[ NUM_COPY_RIGHT_INFO_LINES ];
            // Line 1
            csCopyRightInfoLines[ 0 ] = _T( "/*\n" );
            // Line 2
            csCopyRightInfoLines[ 1 ].Format
		( _T( " * Copy-Right(c) %d Jellow T. K., Refer CPOL Clauses.\n" ),
                 	stSysTime.wYear );
            // Line 3
            csCopyRightInfoLines[ 2 ] = _T( " *\n" );
            // Line 4
            // Retrieve file name
            CComBSTR bstrSrcFile;
            pActiveSrc->get_Name( &bstrSrcFile );
            CString csSrcFileName( bstrSrcFile );
            csCopyRightInfoLines[ 3 ].Format( _T( " * %s - TODO : 
					Add file description here.\n" ),
                                              csSrcFileName );
            // Line 5
            csCopyRightInfoLines[ 4 ] = _T( " *\n" );
            // Line 6
            csCopyRightInfoLines[ 5 ].Format( _T( " * @version:    1.0            
				Date:  %2d-%2d-%4d\n" ),
                                   	stSysTime.wDay, stSysTime.wMonth, stSysTime.wYear );
            // Line 7
            csCopyRightInfoLines[ 6 ] = _T( " */\n" );

            // Update file with Copy-Right information
            CComQIPtr<ITextSelection, &IID_ITextSelection> pITextSel( pITextSelDisp );
            pITextSel->StartOfDocument( CComVariant( 0 ));
            pITextSel->NewLine( CComVariant( 0 ));
            pITextSel->NewLine( CComVariant( 0 ));
            pITextSel->StartOfDocument( CComVariant( 0 ));

            for( int nIdx = 0; nIdx < NUM_COPY_RIGHT_INFO_LINES; ++nIdx )
                CComBSTR bstrCopyRightLineInfo = csCopyRightInfoLines[ nIdx ];
                pITextSel->put_Text( bstrCopyRightLineInfo );
        AfxMessageBox( _T( "Could not find open source file..." ));

    VERIFY_OK( m_pApplication->EnableModeless( VARIANT_TRUE ));

    // Jes End
	return S_OK;

Points of Interest

While I checked for help of ITextDocument and ITextSelection in MSDN, I found that tom.h is needed for compilation. But when I included this header file, I got multiple or redefinition compilation-error. These interface definitions were already present in \ObjModel\TextAuto.h of VC98 standard header files. But I was not able to find some of the documented methods in this header file (\ObjModel\TextAuto.h). I don't know the exact reason.


  • Version 1.0 - 11-Mar-2009


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jellow TK
Software Developer (Senior)
India India
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Febil Chacko Thanikal8-Aug-11 5:23
memberFebil Chacko Thanikal8-Aug-11 5:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170813.1 | Last Updated 11 Mar 2009
Article Copyright 2009 by Jellow TK
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid