Implement the VBA macro class for word operations in. net, and write it in VB.net

Source: Internet
Author: User

 

Public class vbwordapp

Private owordapplic as word. applicationclass
Private odoc as word. Document
Public sub vbwordapp ()
'Activate the com word Interface
Owordapplic = new word. applicationclass

End sub

'Open a file (the file must exists) and activate it
Public sub open (byval strfilename as string)
Dim filename as string
Dim onlyread as Boolean
Dim isvisible as Boolean
Dim missing

Filename = strfilename
Onlyread = false
Isvisible = true
Missing = system. reflection. Missing. Value
Odoc = owordapplic. Documents. Open (filename, missing, onlyread, missing, missing)
Odoc. Activate ()

End sub

'Open a document
Public sub open ()
Dim missing
Missing = system. reflection. Missing. Value
Odoc = owordapplic. Documents. Add (missing, missing)
Odoc. Activate ()

End sub

Public sub quit ()
Dim missing
Missing = system. reflection. Missing. Value
Owordapplic. Quit ()
System. runtime. interopservices. Marshal. releasecomobject (owordapplic)
Owordapplic = nothing

End sub

Public sub releaseword (byval strfilename as string)
Dim filename as string
Dim onlyread as Boolean
Dim isvisible as Boolean
Dim missing

Filename = strfilename
Onlyread = false
Isvisible = true
Missing = system. reflection. Missing. Value
Owordapplic. Documents. Close ()

End sub

Public sub save ()
Odoc. Save ()
End sub

Public sub saveas (byval strfilename as string)
Dim missing
Dim filename as string

Missing = system. reflection. Missing. Value
Filename = strfilename

Odoc. saveas (filename, missing, missing)

End sub

Public sub saveashtml (byval strfilename as string)
Dim missing
Missing = system. reflection. Missing. Value

Dim filename as string
Filename = strfilename

Dim format
Format = CINT (word. wdsaveformat. wdformathtml)
Odoc. saveas (filename, format, missing, and missing)

End sub

Public sub inserttext (byval strtext)
Owordapplic. selection. typetext (strtext)

End sub

Public sub insertlinebreak ()
Owordapplic. selection. typeparagraph ()

End sub

Public sub insertlinebreak (byval nline as integer)
Dim I
For I = 1 to nline
Owordapplic. selection. typeparagraph ()
Next

End sub

Public sub inserttable (byval table as datatable)
Dim otable as word. Table
Dim rowindex, colindex as integer
Rowindex = 1
Colindex = 0

Otable = owordapplic. selection. Tables. Add (owordapplic. selection. Range (), numrows: = table. Rows. Count + 1, numcolumns: = table. Columns. Count)

'Assign the column name of the obtained table to the cell.
Dim Col as datacolumn
Dim row as datarow
For each Col in table. Columns
Colindex = colindex + 1
Otable. Cell (1, colindex). range. insertafter (Col. columnname)
Next

'All rows in the table are assigned to the cell.

For each row in table. Rows
Rowindex = rowindex + 1
Colindex = 0
For each Col in table. Columns
Colindex = colindex + 1
Otable. Cell (rowindex, colindex). range. insertafter (row (Col. columnname ))
Next
Next
Otable. allowautofit = true
Otable. applystylefirstcolumn = true
Otable. applystyleheadingrows = true
End sub

Public sub setalignment (byval strtype as string)

Select case strtype

Case "center"
Owordapplic. selection. paragraphformat. Alignment = word. wdparagraphalignment. wdalignparagraphcenter
Case "left"
Owordapplic. selection. paragraphformat. Alignment = word. wdparagraphalignment. wdalignparagraphleft
Case "right"
Owordapplic. selection. paragraphformat. Alignment = word. wdparagraphalignment. wdalignparagraphright
Case "Justify"
Owordapplic. selection. paragraphformat. Alignment = word. wdparagraphalignment. wdalignparagraphjustify

End select
End sub

Public sub setfont (byval strtype as string)
Select case strtype
Case "bold"
Owordapplic. selection. Font. Bold = 1
Case "italic"
Owordapplic. selection. Font. italic = 1
Case "underlined"
Owordapplic. selection. Font. subscript = 0

End select
End sub

'Disable all the style
Public sub setfont ()

Owordapplic. selection. Font. Bold = 0
Owordapplic. selection. Font. italic = 0
Owordapplic. selection. Font. subscript = 0

End sub

Public sub setfontname (byval strtype as string)

Owordapplic. selection. Font. Name = strtype

End sub

Public sub setfontsize (byval nsize as integer)

Owordapplic. selection. Font. size = nsize
End sub

 

Public sub insertpagebreak ()
Dim PBreak as integer
PBreak = CINT (word. wdbreaktype. wdpagebreak)
Owordapplic. selection. insertbreak (PBreak)

End sub

'Go to a predefined bookmark, if the bookmark doesn' t exists the application will raise an error

Public sub gotobookmark (byval strbookmarkname as string)

Dim missing
Missing = system. reflection. Missing. Value
Dim bookmark
Bookmark = CINT (word. wdgotoitem. wdgotobookmark)
Dim namebookmark
Namebookmark = strbookmarkname
Owordapplic. selection. Goto (bookmark, missing, missing, namebookmark)
End sub

Public Function bookmarkexist (byval strbookmarkname as string) as Boolean
Dim exist as Boolean
Exist = odoc. bookmarks. exists (strbookmarkname)
Return exist
End Function

Public sub gototheend ()
Dim missing, Unit
Missing = system. reflection. Missing. Value
Unit = word. wdunits. wdstory
Owordapplic. selection. endkey (unit, missing)
End sub

Public sub gotothebeginning ()
Dim missing, Unit
Missing = system. reflection. Missing. Value
Unit = word. wdunits. wdstory
Owordapplic. selection. homekey (unit, missing)
End sub

Public sub gotothetable (byval ntable as integer)

Dim missing, what, Which, count
Missing = system. reflection. Missing. Value

What = word. wdunits. wdtable

Which = word. wdgotodirection. wdgotoabsolute

Count = 1

Owordapplic. selection. Goto (what, Which, 1, missing)
Owordapplic. selection. Find. clearformatting ()

Owordapplic. selection. Text = ""

End sub

Public sub gotorightcell ()

Dim missing, Direction
Missing = system. reflection. Missing. Value

Direction = word. wdunits. wdcell
Owordapplic. selection. moveright (Direction, missing, missing)
End sub

Public sub gotoleftcell ()

Dim missing, Direction
Missing = system. reflection. Missing. Value

Direction = word. wdunits. wdcell
Owordapplic. selection. moveleft (Direction, missing, missing)
End sub

Public sub gotodowncell ()
Dim missing, Direction
Missing = system. reflection. Missing. Value

Direction = word. wdunits. wdline
Owordapplic. selection. movedown (Direction, missing, missing)
End sub

Public sub gotoupcell ()
Dim missing, Direction
Missing = system. reflection. Missing. Value

Direction = word. wdunits. wdline
Owordapplic. selection. moveup (Direction, missing, missing)
End sub

'This function doesn' t work
Public sub insertpagenumber (byval strtype as string, byval bheader as Boolean)
Dim missing, alignment, bfirstpage, BF
Missing = system. reflection. Missing. Value

Bfirstpage = false
BF = true
Select case strtype

Case "center"
Alignment = word. wdpagenumberalignment. wdalignpagenumbercenter
Owordapplic. selection. headerfooter. pagenumbers. Item (1). Alignment = word. wdpagenumberalignment. wdalignpagenumbercenter

Case "right"
Alignment = word. wdpagenumberalignment. wdalignpagenumberright
Owordapplic. selection. headerfooter. pagenumbers. Item (1). Alignment = word. wdpagenumberalignment. wdalignpagenumberright

Case "left"
Alignment = word. wdpagenumberalignment. wdalignpagenumberleft
Owordapplic. selection. headerfooter. pagenumbers. Add (alignment, bfirstpage)

End select
End sub
Public sub insertpic (byval filename as string)
Dim missing
Missing = system. reflection. Missing. Value
Owordapplic. selection. inlineshapes. addpicture (filename, false, true, missing)
End sub

End Class

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.