Add toolbars and buttons for Office apps using VB 6 and VB.NET.

zhaozj2021-02-17  49

Add toolbars and buttons for Office apps using VB 6 and VB.NET.

In Office applications, we may need to use Word to make a series of operations, such as executing a macro, by clicking a toolbar button to perform macro code segments, the following instance is adding a toolbar and button to the Office app. Similar to Office applications such as Excel and Acess.

1. Application in VB6:

Reference Microsoft Word X.0 (version number) Object Library and MicoFT Office X.0 (version number) Object Library, as shown below: (the author uses Office XP)

Add the following code:

Private submmand1_click ()

'Define Word Apps

DIM WordApp As New Word.Application

DIM MYDOC As Word.Document

'Define toolbar

Dim mybar as office.commandbar

'Define toolbar button

DIM MyButton as Office.commandbarbutton

DIM ISEXIST AS BOOLEAN

ISEXIST = FALSE

'Open a Word document

Set mydoc = wordapp.documents.open ("f: /test.doc")

Wordapp.visible = TRUE

'If this toolbar is present, you will show this toolbar.

For Each Mybar in Wordapp.comMandbars

If mybar.name = "File Operation" THEN

myBar.visible = TRUE

ISEXIST = TRUE

END IF

NEXT

'If you don't exist, create a toolbar and button.

IF not Isexist kil

Set mybar = wordapp.commandbars.add (_

Name: = "File Operation", _

Position: = msobartop, _

Temporary: = false)

Set myButton = Commandbars ("File Actions") .controls.add

WITH MYBUTTON

.Caption = "file save"

.Tooltiptext = "lihonggen create"

'.PICTURE = loadingPicture ("f: /cd.ico")

'Specify surface picture serial number

.Faceid = 10

.Visible = TRUE

.Enabled = true

'Specify this button macro name

.OnAction = "lihonggen"

End with

myBar.visible = TRUE

END IF

End Sub

2. Application in VB.NET:

Similarly, you need to add a reference to the two object libraries of the MicoSoft Word X.0 (version number) Object Library and Microsoft Office X.0 (version number) Object Library.

Private sub button1_click (byval e as system.EventArgs) handles button1.click 'Defines Word Apps

Dim WordApp as new word.application ()

DIM MYDOC As Word.Document

'Define toolbar

Dim MyBar as Microsoft.Office.core.commandbar

'Define toolbar button

Dim MyButton as Microsoft.Office.core.commandbarbutton

DIM ISEXIST AS BOOLEAN

ISEXIST = FALSE

'Open a Word document

MyDoc = Wordapp.Documents.Open ("f: /test.doc")

Wordapp.visible = TRUE

'If this toolbar is present, you will show this toolbar.

For Each Mybar in Wordapp.comMandbars

If mybar.name = "File Operation" THEN

myBar.visible = TRUE

ISEXIST = TRUE

END IF

NEXT

'If you don't exist, create a toolbar and button.

IF not Isexist kil

mybar = wordapp.commandbars.add (_

Name: = "File Operation", _

Temporary: = false)

MyButton = mybar.commandbars ("File Actions") .controls.add

WITH MYBUTTON

.Caption = "file save"

.Tooltiptext = "lihonggen create"

'Specify picture serial number

.Faceid = 10

.Visible = TRUE

.Enabled = true

'Specify this button macro name

.OnAction = "lihonggen"

End with

myBar.visible = TRUE

END IF

End Sub

Macro recorded in Word:

Generated toolbar and buttons:

Macro operation results:

Readers can expand on the basis of writing!

-------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------

Author: lihonggen0

Personal column: http://www.9cbs.net/develop/author/netauthor/lihonggen0/

For quotation, please refer to the place! The purpose of software is to apply, this article is free to reprint!

-------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------

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

New Post(0)