65.9K
CodeProject is changing. Read more.
Home

JesCopyRightWriter - ITextDocument, ITextSelection Sample

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.67/5 (4 votes)

Mar 11, 2009

CPOL

1 min read

viewsIcon

21253

downloadIcon

226

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