Activex Form
This guide will demonstrate how to get form values from activex and use them in word document using UniOffice
.
Before you begin
You should get your API key from your UniCloud account.
If this is your first time using UniOffice SDK, follow this guide to set up a local development environment.
Clone the project repository
In your terminal, clone the examples repository. It contains the Go code we will be using for this guide.
git clone https://github.com/unidoc/unioffice-examples
Navigate to the path document/form-activex
folder in the unioffice-examples directory.
cd unioffice-examples/document/form-activex
How it works
We will see how it works by explaining the following the example code.
First the necessary libraries are imported in the import
section.
The init
function defined in lines 16-24
loads the API key from the system environment variable and sets the license key using license.SetMeteredKey
.
Themain
function is defined in lines 26-47
. Lines 27-28
in this function define the input and output docx file paths. The values of the ActiveX controls are printed by calling the getActiveXValues
function. The input file is provided as an argument to the function here. In line 37
, the values are updated using setActiveXValues(input, output)
. And in line 43
, the new updated values of the ActiveX controls are printed again using getActiveXValues(output)
.
The function getActiveXValues
is defined in lines 47-87
. This function opens the input docm
file and loads it into a document.Document
object. The nested two for loops in lines 55-84
iterate through the document content, processes the ActiveX controls and print the values one by one.
The setActiveXValues
function defined in lines 89-135
gets the input and output file paths and updates the value of the ActiveX
control. This is done by first iterating through the document paragraphs using for i, p := range doc.Paragraphs()
. Then the inner loop iterates through each Run
of the paragraph and gets the ActiveX
control using ctrl := r.Control()
. The rest of the code checks for each control choice and updates its value. Finally in line 134
the new document is saved to file and the result i.e. the value of the error is returned to the caller.
Run the code
To process the input ActiveX enabled document and generate an updated document run the code using the following command.
go run main.go
Sample output
found checkbox: true CheckBox1
found checkbox: false CheckBox2
found textbox: Textbox text 1
found textbox: Textbox text 2
found label: Label1
found label: Label2
found option button: true OptionButton1
found option button: false OptionButton2
found spin button: 0 0 0 900 450
found spin button: 0 0 1 900 450
found combo box: option 1
found combo box: option 2
found command button: CommandButton1
found command button: CommandButton2
found scroll bar: 0 0 5 2250 450
found scroll bar: 0 0 0 2250 450
found toggle button: true ToggleButton1
found toggle button: false ToggleButton2
found checkbox: true CheckBox caption 0
found checkbox: true CheckBox caption 1
found textbox: New textbox value 2 TextBox caption 2
found textbox: New textbox value 3 TextBox caption 3
found label: New label 4
found label: New label 5
found option button: false Option button 6
found option button: true Option button 7
found spin button: 0 0 1 900 450
found spin button: 0 0 0 900 450
found combo box: New combobox value 12
found combo box: New combobox value 13
found command button: Command button 14
found command button: Command button 15
found scroll bar: 0 100 20 2250 450
found scroll bar: 0 100 20 2250 450
found toggle button: true Toggle button 20
found toggle button: true Toggle button 21
Page 1 Page 2