Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

JesCopyRightWriter - ITextDocument, ITextSelection Sample

0.00/5 (No votes)
11 Mar 2009 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here