How to use bookmarks to read or write data to Word documents [C #] (

Source: Internet
Author: User
The Ministry of materials raised the need to help the system develop a small item that can extract and write data before the launch of the entire system, so as to facilitate online bidding and automatic contract generation.

Comrade Yan, an old teacher, handed over this arduous task to me [the reason is that he has too much work at hand]. After some discussion, we decided to use the bookmarks provided by word as the container for storing temporary data. I think it is Article Not much. View the object model of the office and find that bookmarks has its own attribute range, which can be used to extract and write data, but the problem arises. How can we get this range? I tried many methods, and even thought about whether it could be implemented by using the bookmarkdonefile domain. Comrade Xin thought of using two bookmarks, by determining the location of the two bookmarks, we can retrieve the data in the middle. But these are not good methods. We focus on how to write the range when editing the Word documents. An accidental discovery solves this problem. It is hard to imagine, and it depends on inspiration and inspiration to solve the problem. It cannot be blocked, but this discovery must be attributed to fan Xiong. Below I paste this development process and the original code, hoping to help it users who have the same requirements as me. Enter a string of spaces in an existing Word Document [custom length, as shown in 1], and then select the escape space. Then, insert a bookmarks, as shown in 2.

Figure 1

Figure 2

   As you can see, bookmarks are separated into a pair of brackets, and the content in the brackets is the content of range. You can write as you like.CodeHaha ~~~

The source code is as follows:

 Private void button#click (Object sender, eventargs E)
       {

           Object missingvalue = system. reflection. Missing. value;
           Object Mytrue = false;
           Object filename = this.txt _ file. Text. Trim (). tostring ();
           

           Word. _ application oword = new word. applicationclass ();
           Word. _ document odoc;

Odoc = oword. Documents. Open (ref filename, ref missingvalue,
Ref Mytrue, ref missingvalue,
Ref missingvalue, ref missingvalue, ref missingvalue,
Ref missingvalue, ref missingvalue, ref missingvalue,
Ref missingvalue, ref missingvalue, ref missingvalue,
Ref missingvalue );

MessageBox. show (odoc. bookmarks. count. tostring ());
// write a Word document
Object TMP = "msr_01 ";
word. range tmprng = oword. activedocument. bookmarks. get_item (ref TMP ). range;
tmprng. TEXT = "luneng Engineering Ltd. 221";

           // Because the bookmarks are automatically deleted after the value is included, You need to automatically generate

           Object orng = tmprng;
           Odoc. bookmarks. Add (TMP. tostring (), ref orng );
           Object bsavechange = true;
           Odoc. Close (ref bsavechange, ref missingvalue, ref missingvalue );
       }

More: 1. [This is the data written into the Word document. If you want to obtain the data, you can use range. Text to obtain the data.]  

     2. The required using instruction set is: using word = Microsoft. Office. InterOP. word;

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.