条形码控件需要引用MSBCODE9.OCX组件。该组件的默认位置是C:Program FilesMicrosoft OfficeOffice122052MSBCODE9.OCX
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Word = Microsoft.Office.Interop.Word;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Word.Application curWord = null;
Word.Document curDocument = null;
object mvalue = System.Reflection.Missing.Value;
curWord = new Microsoft.Office.Interop.Word.Application();
try
{
string path = @"D:***.doc";
object filepath = path;
object obj1 = "BARCODE.BarCodeCtrl.1";
curDocument = curWord.Documents.Open(ref filepath, ref mvalue, ref mvalue, ref mvalue, ref mvalue, ref mvalue,
ref mvalue, ref mvalue, ref mvalue, ref mvalue, ref mvalue, ref mvalue, ref mvalue, ref mvalue, ref mvalue,
ref mvalue);
object range = curWord.ActiveWindow.Selection.Range;
curDocument.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageHeader;
object s = curDocument.ActiveWindow.Selection.InlineShapes.AddOLEControl(ref obj1, ref mvalue).OLEFormat.Object;
((BARCODELib.IBarCodeCtrl)s).Style = 7; ((BARCODELib.IBarCodeCtrl)s).Value = "S45132465789s";
curDocument.ActiveWindow.Selection.InlineShapes[1].Width = 300f;
curDocument.ActiveWindow.Selection.InlineShapes[1].Height = 70f;
curDocument.ActiveWindow.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
curDocument.Save();
curDocument.Close(ref mvalue, ref mvalue, ref mvalue);
curWord.Quit(ref mvalue, ref mvalue, ref mvalue);
curDocument = null;
curWord = null;
}
catch (Exception ex)
{
curDocument.Close(ref mvalue, ref mvalue, ref mvalue);
curWord.Quit(ref mvalue, ref mvalue, ref mvalue);
curDocument = null;
curWord = null;
}
}
}
}