Click here to Skip to main content
Click here to Skip to main content

JesCopyRightWriter - ITextDocument, ITextSelection Sample

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

Introduction

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).

Background

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()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	// 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 );
            }
        }
    }
    else
    {
        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.

History

  • Version 1.0 - 11-Mar-2009

License

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

Share

About the Author

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

Comments and Discussions

 
GeneralMy vote of 5 PinmemberFebil Chacko Thanikal8-Aug-11 6:23 

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

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

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