VB Thousand Miles - Operation Word and Excel

zhaozj2021-02-08  247

This article will tell you how to connect the Office application using the VB code and briefly contact the method of entering data in the file. In fact, it is possible to use the code with Word and Excel in VB and control them. Note, please note that you first need to install Office applications on the machine to access them in the VB code.

Here is some examples to tell you how to talk to these programs and control them.

Option expedition

DIM XLSAPP as Excel.Application

Dim WRDAPP As Word.Application

As long as the associated object library has been selected, the assignment of object variables in the application is possible. The Microsoft Excel 8.0 object library is relative to Excel, while the Microsoft Word 8.0 object library is for Word.

In the VB IDE environment, select "Reference" from the Project menu, you can see all libraries available to the system.

Private submmand1_click ()

Set XLsApp = Excel.Application

With xlsapp

'Show Excel

.Visible = TRUE

'Create a New Workbook

.Workbooks.add

'Put Text in To The Cell That Is Selected

.Activecell.value = "hi"

'Put Text Into A3 Regardless of The Selected Cell

.Range ("A3"). Value = "this is an example of connecting to excel"

End with

End Sub

In the above block, we built an object in the variable XLSAPP, so Excel visible to the user. When the Excel is started like this, does not include a workbook, so you must create or perform open operations. Here, we have established a new workbook, then you can operate the information, or print, or save, or you want to do anything.

Private sub fascist2_click ()

'close the workbook

Xlsapp.Workbooks.close

'Close Excel

Xlsapp.quit

End Sub

The above code performs the function of closing the program. First, turn off the workbook, which will appear a prompt dialog box, ask if the user wants to save the modification; then, exit the application.

Private submmand3_click ()

SET WRDAPP = New Word.Application

With wrdapp

'Show Word

.Visible = TRUE

'Create New Document

.Documents.add

'Add text to the document

.Activedocument.content.text = "hi"

.Activedocument.content.text = "this is a test esample"

End with

End Sub

In this code, set the object of the reference Word program in the variable WRDAPP. Similarly, when Word is started in this way, it will not contain a document, so it is necessary to establish or open operations. Here is a new document, then you can operate, print, save, send mail, etc. ... However, placing text in the Word document is not easy! Especially when working with Excel. In order to place text in a particular place, you need to have a Bookmark tag. This means that a template is needed in advance.

Private sub fascist4_click ()

'Close the current document

WRDAPP.ActiveDocument.Close

'Close Word

WRDAPP.QUIT

End Sub

The function of the above code is to close the application. First, turn off the current document, which may require a user to save modifications. Then, exit the program.

Private Sub Form_Unload (Cancel AS Integer)

'Clear THE MEMORY

Set XLSApp = Nothing

SET WRDAPP = Nothing

End Sub

The last code is to close the VB application. This is a good habit of excellent programmers programming.

Well I hope this brief tutorial is helpful. It does not touch on much of what you can do to the office applications overce the ye're open, but shop give you an idea of ​​how to get started.

Ok, simple introduction is over. I hope to throw the jade, let you more freely operate the Office application!

转载请注明原文地址:https://www.9cbs.com/read-3479.html

New Post(0)