C # insert a barcode to a Word document

Source: Internet
Author: User

In word, you can manually add a bar code using the following method: Use the menu "View"> "toolbar"> "control toolbox" to open the control toolbox, use the last button "other controls" in the lower-right corner of the Control Toolkit, select "Microsoft barcode control 9.0" in the pop-up list, and right-click the newly added barcode in the document, use "properties" in the mail menu to open the attribute dialog box. Set the value and style attribute values to display the corresponding barcode.

There is another way to add a bar code. To use a bar code font, you only need to enter the characters of the corresponding bar code and select a specific bar code font.

The first method is discussed here, and the application scenario is to add multiple bar codes to a Word document at a time. In this case, manual addition is not applicable. You can use VBA to add a bar code.

It is easy to find such a programming reference in VBA for word.Code:

 

1 Set Mycb = Activedocument. shapes. addolecontrol (classtype: = " Forms. checkbox.1 " )
2 With Mycb. oleformat. Object
3 . Value =   False
4 . Caption =   " Check if over 21 "
5 End   With

 

This is the VBA code for adding a check box to a Word document. If a bar code object is added, the Code is as follows:

 

1 Set Mycb = Activedocument. shapes. addolecontrol (classtype: = " Barcode. barcodectrl.1 " )
2 With Mycb. oleformat. Object
3 . Value =   " S-102909211000001-12 "
4 . Style =   7
5 End   With

 

The string displayed on the bar code is "S-102909211000001-12 ".

In C #, how do I complete the above operations?

Add Microsoft. office. interOP. add a reference to msbcode9.ocx in the 2052 directory under the Office installation directory. The latter is displayed as barcodelib in Solution Explorer, you need to use this namespace to set the attributes of the barcode.

Use the following code to add a barcode to the Word document:

 

1 Private   Void Btnaddbarcodecontrol_click ( Object Sender, eventargs E)
2 {
3
4 Microsoft. Office. InterOP. Word. Application curword =   Null ;
5 Microsoft. Office. InterOP. Word. Document curdocument =   Null ;
6 Object Mvalue = System. reflection. Missing. value;
7
8 Object Objfilename = Application. startuppath +   " /Testdoc.doc " ;
9
10 Object Objreadonly =   False ;
11
12 Curword =   New Microsoft. Office. InterOP. Word. Application ();
13
14 Curdocument = Curword. Documents. Open (
15 Ref Objfilename
16 , Ref Mvalue
17 , Ref Objreadonly
18 , Ref Mvalue
19 , Ref Mvalue
20 , Ref Mvalue
21 , Ref Mvalue
22 , Ref Mvalue
23 , Ref Mvalue
24 , Ref Mvalue
25 , Ref Mvalue
26 , Ref Mvalue
27 , Ref Mvalue
28 , Ref Mvalue
29 , Ref Mvalue
30 , Ref Mvalue
31 );
32
33 Object Objolecontroltype =   " Barcode. barcodectrl.1 " ;
34
35 Object Objleft =   20 ;
36 Object Objtop =   20 ;
37 Object Objwidth =   200 ;
38 Object Objheight =   50 ;
39
40 Object Comcontrol = Curdocument. shapes. addolecontrol (
41 Ref Objolecontroltype
42 , Ref Objleft
43 , Ref Objtop
44 , Ref Objwidth
45 , Ref Objheight
46 , Ref Mvalue
47 ). Oleformat. object;
48
49
50 (Barcodelib. ibarcodectrl) comcontrol). Value =   " S-102909211000001-12 " ;
51
52 (Barcodelib. ibarcodectrl) comcontrol). Style =   7 ;
53
54 Curdocument. Save ();
55
56 }

 

 

The above C # code is converted from VBA code that completes the same function. It may not be difficult to understand in other places. There are two points to describe --

1. How can I determine the first classtype parameter of the addolecontrol method? The syntax prompt tells us that this is the programming identifier of the ActiveX control. In fact, this programming identifier can be found in the VBA for word programming reference material. The topic of this document is "Ole programming identifier ", the list of programming identifiers of common controls is as follows:

 

To create this control Use this identifier
Check box Forms. checkbox.1
Combo box Forms. combobox.1
Command button Forms. commandbutton.1
Framework Forms. frame.1
Image Forms. image.1
Tag Forms. label.1
List box Forms. listbox.1
Multi-page controls Forms. multipage.1
Option Button Forms. optionbutton.1
Scroll bar Forms. scrollbar.1
Value Adjusting button Forms. spinbutton.1
Tabstrip Forms. tabstrip.1
Text Box Forms. textbox.1
Switch button Forms. togglebutton.1

 

Of course, for a bar code control, its programming identifier must be special. There is no way to find the classtype name used for processing in the VBA method.

2. How is the comcontrol type determined? If you are not sure about its type, you cannot set the value and style attributes of the control.

This is the process of determining the COM object type. visual Basic Reference, monitoring and viewing Microsoft. visualBasic. information. for the value of typename (comcontrol) (before setting the OLE Control attribute), you can obtain the string "ibarcodectrl" (for the principle, refer to "get system. _ True Type of comobject ", which is not described in detail). In the barcodelib namespace, the interface named ibarcodectrl exists.

If you want to add common office controls instead of bar code controls, the method is similar. Taking adding a text box as an example, objolecontroltype should be "forms. textbox.1 ", check Microsoft. visualBasic. information. the value of typename (comcontrol) is imdetext. To use this interface, add a reference to Microsoft. VBE. interOP. forms. This namespace contains interfaces for common office controls. You can use the following statement to set the initial display string for this text box:

 

1 (Microsoft. VBE. InterOP. Forms. imdctext) comcontrol). Text =   " In1_value " ;

 

 

 

P.s. For details about how C # operates Word documents in the form of COM, see sharemeteor's article:. net1.1. Use C # To automatically generate word2003 documents (implemented by operating the COM component ).

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.