How to insert barcodes into Word (.DOC) document using Bytescout BarCode SDK and Visual Basic .NET (or C#) with Word automation - ByteScout

How to insert barcodes into Word (.DOC) document using Bytescout BarCode SDK and Visual Basic .NET (or C#) with Word automation

  • Home
  • /
  • Articles
  • /
  • How to insert barcodes into Word (.DOC) document using Bytescout BarCode SDK and Visual Basic .NET (or C#) with Word automation

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.

  1. Open your MS Word appplication.
  2. Place cursor to the position where barcode will be placed.
  3. Select Insert->Bookmark from MS Word main menu
  4. Input bookmark name (i used “MyBookmark”) and press Enter
  5. Save document and close it

Bookmark will help us quickly find position where barcode will be placed.

Now open Visual Studio and create new project.

Visual Studio 2005 - New Project command in menu

Select Visual C# -> Console Application.

Console application type selection type in New Project wizard

Add 4 references to your project. Make right click on the project name at the Solution Explorer:

Select ConsoleApplication2 in the Solution Explorer

and choose “Add Reference…”

Use Add Reference command to add reference to required assemblies

Choose three references from .NET tab:

  1. Bytescout.BarCode

    Select Bytescout.BarCode.dll to add reference and click OK to add

  2. System.Drawing

    Add reference to System.Drawing assembly and click OK

  3. System.Windows.Forms

    Select Windows.Forms assembly and click OK to add the reference

And last one from COM tab: Microsoft Word 11.0 Object Library

Select Microsoft Word 11.00 object library on COM tab and click OK to add reference

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

Imports 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.Forms

Imports 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 Sub

End 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 Word document before (.doc template)

Screenshot of the Word document with barcode image inserted into the table using Bytescout BarCode SDK:

The table in the Word document with the barcode image inserted automatically using Bytescout BarCode SDK and Visual Basic or C# for ,NET

Tutorials:

prev
next