Click here to Skip to main content
15,885,546 members
Articles / Desktop Programming / Win32
Tip/Trick

Add-in Word - Simple Paste PrintScreen

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
13 May 2013CPOL 10.4K   1   1
Add-in Word for PrintScreen

Introduction

The purpose of this article is creating an Add-In for Word that automates logging printscreens.

Background

This add-in was designed to facilitate the creation of manuals, registration testing and other tasks where it is used very printscreen. 

Creating the code 

First create a Word 2007 Add-in solution, like this: 

Image 1

Creating the Code Add-In   

After solution create, let's create a button:  

C#
//
// Any source code blocks look like this
//
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
   CheckIfMenuBarExists();
   AddMenuBar();
}
 
//
// Checks whether there is a button with id equal to 200
//
private void CheckIfMenuBarExists()
{
   try
   {
    Office.CommandBarButton foundMenu = (Office.CommandBarButton)
        this.Application.CommandBars.ActiveMenuBar.FindControl(
          Office.MsoControlType.msoControlButton, System.Type.Missing, 200, true, true);
 
    if (foundMenu != null)
    {
        foundMenu.Delete(true);
    }
   }
   catch (Exception ex)
   {
    MessageBox.Show(ex.Message);
   }
}
 
//
// Add button with caption 'Monitor PrintScreen'
//
private void AddMenuBar()
{
   try
   {
    Office.CommandBar menubar = (Office.CommandBar)Application.CommandBars.ActiveMenuBar;
 
    // Add the menu.                
    menuCommand = (Office.CommandBarButton)menubar.Controls.Add(
           Office.MsoControlType.msoControlButton, missing, missing, missing, true);
    menuCommand.Style = MsoButtonStyle.msoButtonIconAndWrapCaptionBelow;
    menuCommand.Caption = "Monitor PrintScreen";
    menuCommand.Tag = "200";
    menuCommand.FaceId = 65;
    menuCommand.Click += new _CommandBarButtonEvents_ClickEventHandler(menuCommand_Click);                        
   }
   catch (Exception e)
   {
    MessageBox.Show(e.Message);
   }
}

Clipboard Monitor

C#
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SetClipboardViewer(IntPtr hWndNewViewer);
 
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern bool ChangeClipboardChain(IntPtr hWndRemove, IntPtr hWndNewNext);
 
private const int WM_DRAWCLIPBOARD = 0x0308;// WM_DRAWCLIPBOARD message
private IntPtr _clipboardViewerNext;
 
public delegate void ClipboardHandler(object sender, ClipboardArgs e);
public event ClipboardHandler OnNewClipboard;
 
public ClipboardManager()
{
    InitializeComponent();
}
 
public void Start()
{
    _clipboardViewerNext = SetClipboardViewer(this.Handle);
}
 
public void Stop()
{
    ChangeClipboardChain(this.Handle, _clipboardViewerNext);
}
 
protected override void WndProc(ref Message m)
{
   base.WndProc(ref m);    
   if (m.Msg == WM_DRAWCLIPBOARD)
   {
    IDataObject iData = Clipboard.GetDataObject();      
 
    if (iData.GetDataPresent(DataFormats.Bitmap))
    {
        Bitmap image = (Bitmap)iData.GetData(DataFormats.Bitmap);
        
        if (OnNewClipboard != null)
        {                        
            OnNewClipboard(this, new ClipboardArgs(image));
        }
    }
    }
}
 
public class ClipboardArgs
{
   public ClipboardArgs(Bitmap image)
   {
    this.Image = image;
   }
   
   public Bitmap Image { get; set; }
 
}

Inclusion of the new image in the document

C#
void manager_OnNewClipboard(object sender, ClipboardArgs e)
{
        Application.ActiveDocument.Content.Paragraphs.Add(
          Globals.ThisAddIn.Application.Selection.Range);
 
    Globals.ThisAddIn.Application.Selection.Paste();
    Application.ActiveDocument.Content.Paragraphs.Add(
      Globals.ThisAddIn.Application.Selection.Range);

    Globals.ThisAddIn.Application.Selection.MoveDown(WdUnits.wdParagraph, 2, Type.Missing);
}

Result of this code

After clicking the button, all the print screens will be included in the Word document. 

Image 2

Points of Interest

In this article learn how to create:

  • An Add-in for Word 2007.
  • A monitor area transfer.

History

  • 12th March, 2013: Article published.

License

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


Written By
Software Developer TOTVS
Brazil Brazil
Graduate from Fabrai in 2004.
Graduate Studies from UFMG in 2006.
I'm currently working for TOTVS company.

Comments and Discussions

 
QuestionNeed more information Pin
vikuseth13-May-13 23:36
vikuseth13-May-13 23:36 
can you please explain some thing more on this utility ? How it is being used in daily work .

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.