Our first step will be to create .doc template file where we will insert barcode image. First of all lets define place where barcode will be placed.
Bookmark will help us quickly find position where barcode will be placed.
Now open Visual Studio and create new project.
Select Visual C# -> Console Application.
Add 4 references to your project. Make right click on the project name at the Solution Explorer:
and choose “Add Reference…”
Choose three references from .NET tab:
And last one from COM tab: Microsoft Word 11.0 Object Library
Now proceed to the source code. First of all add this to the beginning:
Imports System
Imports System.Collections.Generic
Imports System.Drawing
Imports System.IO
Imports System.Text
Imports System.Windows.FormsImports Bytescout.BarCode
Next add <STAThread()> _ before Main function. This attribute needed for Word COM object.
<STAThread()> _
Sub Main()
This commands will create barcode:
‘ Create new barcode
Dim barcode As New Barcode()‘ Set symbology
barcode.Symbology = SymbologyType.Codabar
‘ Set value
barcode.Value = “123456”‘ Add checksum to barcode
barcode.AddChecksum = True
Next piece of code shows you how to open .doc file. Pay your attention that input path and output path must be full! Missing object is needed for parameters that we will not use.
‘ Create word instance
Dim appWord As New Microsoft.Office.Interop.Word.Application()‘ Hide word
appWord.Visible = False‘ Create missing object
Dim mis As Object = System.Reflection.Missing.Value
‘ Template file
Dim fileInput As Object = “C:input.doc”
‘ Template file
Dim fileOutput As Object = “C:output.doc”‘ Open document
Dim docWord As Microsoft.Office.Interop.Word.Document = appWord.Documents.Open(fileInput, mis, mis, mis, mis, mis, mis, mis, mis, mis, mis, mis, mis, mis, mis, mis)
Now lets find bookmark that we defined at the beginning:
‘ Set bookmark name
Dim bookmarkName As Object = “MyBookmark”‘ Get bookmark location
Dim bookmarkLocation As Microsoft.Office.Interop.Word.Range = docWord.Bookmarks(bookmarkName).Range()
It’s time to generate barcode, copy it to the clipboard and paste to the bookmark location:
‘ Get barcode image
Dim image As Bitmap = DirectCast(barcode.GetImage(), Bitmap)‘ Copy image to the clipboard
Clipboard.SetDataObject(image)‘ Paste barcode image to the document
bookmarkLocation.Paste()
Final step. Now we will save document and clear resources.
‘ Save to the new document
docWord.SaveAs(fileOutput, mis, mis, mis, mis, mis, mis, mis, mis, mis, mis, mis, mis, mis, mis, mis)‘ We needn’t save changes
Dim saveChanges As Object = False‘ Close word application
appWord.Quit(saveChanges, mis, mis)‘ Release COM object
System.Runtime.InteropServices.Marshal.ReleaseComObject(appWord)
Full source code (VB.NET)
Imports System
Imports System.Collections.Generic
Imports System.Drawing
Imports System.IO
Imports System.Text
Imports System.Windows.FormsImports Bytescout.BarCode
Module Module1
<STAThread()> _
Sub Main()
‘ Create new barcode
Dim barcode As New Barcode()‘ Set symbology
barcode.Symbology = SymbologyType.Codabar
‘ Set value
barcode.Value = “123456”‘ Add checksum to barcode
barcode.AddChecksum = True‘ Create word instance
Dim appWord As New Microsoft.Office.Interop.Word.Application()‘ Hide word
appWord.Visible = False‘ Create missing object
Dim mis As Object = System.Reflection.Missing.Value
‘ Template file
Dim fileInput As Object = “C:input.doc”
‘ Template file
Dim fileOutput As Object = “C:output.doc”‘ Open document
Dim docWord As Microsoft.Office.Interop.Word.Document = appWord.Documents.Open(fileInput, mis, mis, mis, mis, mis, mis, mis, mis, mis, mis, mis, mis, mis, mis, mis)‘ Set bookmark name
Dim bookmarkName As Object = “MyBookmark”‘ Get bookmark location
Dim bookmarkLocation As Microsoft.Office.Interop.Word.Range = docWord.Bookmarks(bookmarkName).Range()‘ Get barcode image
Dim image As Bitmap = DirectCast(barcode.GetImage(), Bitmap)‘ Copy image to the clipboard
Clipboard.SetDataObject(image)‘ Paste barcode image to the document
bookmarkLocation.Paste()‘ Save to the new document
docWord.SaveAs(fileOutput, mis, mis, mis, mis, mis, mis, mis, mis, mis, mis, mis, mis, mis, mis, mis)‘ We needn’t save changes
Dim saveChanges As Object = False‘ Close word application
appWord.Quit(saveChanges, mis, mis)‘ Release COM object
System.Runtime.InteropServices.Marshal.ReleaseComObject(appWord)
End SubEnd Module
Full source code (Visual C# .NET)
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;using Bytescout.BarCode;
namespace InsertIntoMicrosoftWordDocument
{
class Program
{
[STAThread]
static void Main(string[] args)
{
// Create new barcode
Barcode barcode = new Barcode();// Set symbology
barcode.Symbology = SymbologyType.Codabar;
// Set value
barcode.Value = “123456”;// Add checksum to barcode
barcode.AddChecksum = true;// Create word instance
Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();// Hide word
appWord.Visible = false;// Create missing object
object mis = System.Reflection.Missing.Value;
// Template file
object fileInput = @”C:input.doc”;
// Template file
object fileOutput = @”C:output.doc”;// Open document
Microsoft.Office.Interop.Word.Document docWord = appWord.Documents.Open(ref fileInput, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis);// Set bookmark name
object bookmarkName = “MyBookmark”;// Get bookmark location
Microsoft.Office.Interop.Word.Range bookmarkLocation = docWord.Bookmarks.get_Item(ref bookmarkName).Range;// Get barcode image
Bitmap image = (Bitmap)barcode.GetImage();// Copy image to the clipboard
Clipboard.SetDataObject(image);// Paste barcode image to the document
bookmarkLocation.Paste();// Save to the new document
docWord.SaveAs(ref fileOutput, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis, ref mis);// We needn’t save changes
object saveChanges = false;// Close word application
appWord.Quit(ref saveChanges, ref mis, ref mis);// Release COM object
System.Runtime.InteropServices.Marshal.ReleaseComObject(appWord);
}
}
}
Screenshot of the table in the Word document (.doc) before:
Screenshot of the Word document with barcode image inserted into the table using Bytescout BarCode SDK: