Word (1) in VB. NET Office)

Source: Internet
Author: User

Word (1) in VB. NET Office)

Here we provide a class for Word operations, which provides basic functions for word document operations. In the next article, I will provide examples of implementation of this class. For your reference
The program references Microsoft Word 14.0 Object Library using word 2007 + VS2010

'*************************************** * ***************************** 'Author: zhang yuge, QQ: 3107073263 group: 309816713 '. If you have any questions or suggestions, please contact me, everyone makes progress together '************************************ * ********************************* Imports Microsoft. office. interopPublic Class Class_Word1 Public ZWordApplic As Word. application Private ZDocument As Word. document Public Sub New () 'generates a class instance ZWordApplic = New Word. application ZWordApplic. vis Visible = True End Sub create a Word document Public Sub NewDocument () ZDocument = ZWordApplic. documents. add () 'create a document End sub' use the template to create a document Public Sub ModulNewDocument (ByVal FileAddress As String) ZDocument = ZWordApplic. documents. add (FileAddress) End Sub 'open a document Public Sub OpenWordDocument (ByVal FileAddress As String, ByVal IsReadOnly As Boolean) Try ZDocument = ZWordApplic. documents. open (FileAddress, Nothing, IsReadOnly) Catch ex As Exception MsgBox ("the address you entered is incorrect") End Try End Sub 'close a document Public Sub CloseWordDocument () ZWordApplic. quit () System. runtime. interopServices. marshal. releaseComObject (ZWordApplic) ZWordApplic = Nothing End Sub 'Close all open documents Public Sub CloseAllDocuments () 'zwordapplic. documents. close (Word. wdSaveOptions. wdDoNotSaveChanges) ZWordApplic. documents. close (Word. wdSaveOptions. wdDoNotSaveChan Ges) End Sub 'Save the document Public Sub Save () Try ZDocument. save () MsgBox ("saved successfully") Catch ex As Exception MsgBox (ex. message) End Try End Sub 'save As Public Sub SaveAs (ByVal FileAdress As String) Try ZDocument. saveAs2 (FileAdress) MsgBox ("saved as successful! ") Catch ex As Exception MsgBox (ex. message) End Try End Sub 'insert text Public Sub InsertText (ByVal text As String) ZWordApplic. selection. typeText (text) End Sub 'insert table Public Sub InsertTabel (ByVal Tabel As DataTable) Dim ZTabel As Word. table ZTabel = ZDocument. tables. add (ZWordApplic. selection. range, Tabel. rows. count + 1, Tabel. columns. count) 'add the header For I = 1 To Tabel. columns. count ZTabel. rows (1 ). cells (I ). range. insertAfter (Tabel. columns (I-1 ). columnName) Next 'add table data For I = 2 To Tabel. rows. count + 1 For j = 1 To Tabel. columns. count ZTabel. rows (I ). cells (j ). range. insertAfter (Tabel. rows (I-2 ). item (j-1 ). toString) Next ZTabel. allowAutoFit = True ZTabel. applyStyleFirstColumn = True ZTabel. applyStyleHeadingRows = True End Sub 'Insert the image Public Sub InsertPic (ByVal PicAddress As String) Try ZWordApplic. selection. inlineShapes. addPicture (PicAddress, False, True) Catch ex As Exception MsgBox ("incorrect image address") End Try End Sub 'read the text Public Sub ReadText () ZWordApplic. selection. wholeStory () ZWordApplic. selection. copy () End SubEnd Class


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.