C # Delete TAG content by Tag name in word operations

Source: Internet
Author: User

 

CodeYou have already tested it. Add the try catch Statement by yourself. Remember to release the resource after an error occurs.

After this operation is performed, the operated tag will be removed.

 

Code

Private   Void Button#click ( Object Sender, eventargs E)
{
Clearbookmarkfromdoc (@"C: \ test.doc","A");
}


Public   Void Clearbookmarkfromdoc ( String Filepath, String Bkname)
{
Object Filepath = Filepath As   Object ;
Object Missing = Type. missing;
Object Objtrue =   True   As   Object ;

// Create a word. Application
Microsoft. Office. InterOP. Word. Application mywordapp =   New Microsoft. Office. InterOP. Word. applicationclass ();
Microsoft. Office. InterOP. Word. Document myworddoc = Mywordapp. Documents. Open ( Ref Filepath, Ref Missing, Ref Missing, Ref Missing,
Ref Missing, Ref Missing, Ref Missing, Ref Missing,
Ref Missing, Ref Missing, Ref Missing, Ref Missing,
Ref Missing, Ref Missing, Ref Missing, Ref Missing );

Microsoft. Office. InterOP. Word. Range bmrange =   Default (Microsoft. Office. InterOP. Word. Range );

Microsoft. Office. InterOP. Word. bookmarks bkmrs = Myworddoc. bookmarks;

Object Bkindex = Bkname As   Object ;
Microsoft. Office. InterOP. Word. Bookmark BKM = Bkmrs. get_item ( Ref Bkindex );

Textbox1.text = BKM. Name;
BKM. range. Text =   String . Empty;

Myworddoc. Save ();
Myworddoc. Close ( Ref Objtrue, Ref Missing, Ref Missing );
Mywordapp. Quit ( Ref Objtrue, Ref Missing, Ref Missing );
}

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.