Msoffice tips: transition from initial binding to later binding

Source: Internet
Author: User

Author: water and smoke

WriteProgramInevitably, it is necessary to deal with Microsoft's office, especially excel, access, and word. You should be familiar with office when using application software. Otherwise, it would be incredible if I wanted. Familiar with office, of course, you should know how to use its "macro", a little deeper, you need to understand some VBA. In the future, I will talk about this little knowledge and tips in the application, especially in combination with the use of office to compile programs.Code. In my experience, as long as you know how to do it, you don't have to worry about not doing it. I don't know how to do it now. I forgot what I used to know. I used to think about how to do it. Of course, I usually want to think more about it. It is another thing to remember.

The Office objects are very similar, such as having a common base class and implementing the same interface. Therefore, we can draw a line between them and learn how to control a word, such as Excel.

There are a lot of Office versions. To adapt to different versions, when dealing with office in VB. NET, I am used to using the later binding method and setting option strict off. However, when writing code, I am not familiar with object attribute methods of it (such as Excel), so I still reference its COM, set option strict on to ensure that all the Code related to it is correct. After running the command, delete the com reference and set Option strict off.

For example, access:

Reference COM, option strict on:

Public   Class Accessapplication

Public   Shared   Sub Show ()
Dim Access As   New Microsoft. Office. InterOP. Access. Application
Access. Visible =   True
System. Threading. thread. Sleep ( 5000 ) ' Close after 5 seconds
Access. Quit (InterOP. Access. acquitoption. acquitsavenone)
Access =   Nothing
End sub

End Class

Later binding, option strict off:

Public   Class Accessapplication

Public shared sub show ()
dim access as Object
access = Createobject ( " access. application " )

access. visible = true
system. threading. thread. sleep ( 5000 )

DimAcquitsavenoneAs Integer = 2
Access. Quit (acquitsavenone)
Access= Nothing
End sub

End Class

Both are called:

Private   Sub Button#click ( Byval Sender As System. object, Byval E As System. eventargs) Handles Button1.click
Accessapplication. Show ()
End sub

As mentioned above, let's take a look at the following:

Public   Class Officeapplication

Public   Shared   Sub Show ( Byval Office As Office)
Dim Mapplication As   Object
Mapplication =   Createobject ( String . Format ( " {0}. Application " , Office. tostring ))

Mapplication. Visible= True
System. Threading. thread. Sleep (5000)

Mapplication. Quit ()
Mapplication= Nothing
End sub

Public EnumOffice
Word
Excel
Access
End Enum
End Class

Word, Excel, and access are displayed in sequence:

Private   Sub Button#click ( Byval Sender As System. object, Byval E As System. eventargs) Handles Button1.click
Officeapplication. Show (officeapplication. Office. Word)
Officeapplication. Show (officeapplication. Office. Excel)
Officeapplication. Show (officeapplication. Office. Access)
End sub

Note: The above will be available for reference only. Lzmtw 20060521.

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.