Processing
 

Is it possible to use VBA functions in Word field codes?

07/09/2016 08:34#1

Joined at: 11 months ago

Post: 4

Thank: 0

Thanked: 0

I have a Word document with some functions. I want to access these functions in formula fields, but this was harder than anticipated.

Let's say I have a function defined as

Public Function Area(R As Double) As Double   Area = 3.14 * R * R End Function 

and that I want to use this in a field, like this

{ = Area(RadiusBookmark) } 

This seems pretty straight forward, but I get a syntax error. If I omit the parameter, like this

{ = Area } 

I get an "undefined bookmark" error, which leads me to believe that only bookmarks are available to the formula code fields.

18/09/2016 08:39Top#2

Joined at: 5 months ago

Post: 3

Thank: 0

Thanked: 0

Word doesn't allow you to use VBA functions in a field.

One common approach is to use a field with a DocVariable, and have some VBA that is triggered by a button press or a document event, that sets the value of the variable to the result of a function, and then refreshes the field value.

First, add a DOCVARIABLE field to your document:

My function returns ‘DOCVARIABLE MyFuncResult \* MERGEFORMAT’!

Then add this VBA:

Option Explicit  Private Function Foo() As String   Foo = "BAR" End Function  Public Sub setvar()    Const MYVAR As String = "MyFuncResult"    Dim var As Variable    On Error Resume Next   Set var = ThisDocument.Variables(MYVAR)   On Error GoTo 0    If var Is Nothing Then     Set var = ThisDocument.Variables.Add(MYVAR)   End If    var.Value = Foo    ThisDocument.Fields.Update  End Sub 

And you should see the document content as:

My function returns ‘BAR’!

Similar articles

VBA regex matching over two lines

5 months ago - Reply: 1 - Views: 44

Table output to Word-2011 from Rstudio using knit

5 months ago - Reply: 0 - Views: 44

Write a Collection of paragraphs into a new document

5 months ago - Reply: 1 - Views: 72

how to write paragraphs without show to document

5 months ago - Reply: 1 - Views: 67

numericals in word macro

5 months ago - Reply: 1 - Views: 46

Can not find proofing tools

5 months ago - Reply: 0 - Views: 2

Possible values for <w:outlineLvl> in Word OOXML

5 months ago - Reply: 1 - Views: 8

C# restrict editing partial word paragraph

6 months ago - Reply: 2 - Views: 146

Macro translation Please (possible virus)

5 months ago - Reply: 1 - Views: 35

Doc/Docx Fastest Reading Method for Searching

5 months ago - Reply: 0 - Views: 3

Save embedded file / content from word

5 months ago - Reply: 0 - Views: 6

Counting words in Word document, including footnores

5 months ago - Reply: 1 - Views: 24

c# access extract word ole object

5 months ago - Reply: 1 - Views: 23

Using word wildcards to find unaccepted changes

5 months ago - Reply: 1 - Views: 46

vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re