Formfields in Tables (VBA)

N

Nick Marquis

I am trying to find out the required VBA commands to establish whether
specific formfields are present in specific table cells or any command that
identifies whether any cell contains a formfield.

Any help please?
 
N

Nick Marquis

Hi,

Unfortunately I don't want to know if the formfield exists.

I want to know whether it's in a specific cell or if a specific cell
contains the required formfield.

Any more info?
 
D

David Sisson

Unfortunately I don't want to know if the formfield exists.

Yeah, but the logic is there.

This seems to work.

Sub CheckFFTable()

Dim aDoc As Document
Dim tbl As Table
Dim Rng As Range
Dim MyCell As Range

Set aDoc = ActiveDocument
Set tbl = aDoc.Tables(1)
Set MyCell = tbl.Cell(2, 2).Range

'Remove end of cell marker
MyCell.MoveEnd wdCharacter, -1

On Error GoTo NoExist
If MyCell.FormFields("Text1").Creator <> 0 Then
MsgBox "Bookmark is here"
On Error GoTo 0
End If

Exit Sub
NoExist:
MsgBox "Bookmark not here"

End Sub
 
N

Nick Marquis

Thanks. That's great.


David Sisson said:
Yeah, but the logic is there.

This seems to work.

Sub CheckFFTable()

Dim aDoc As Document
Dim tbl As Table
Dim Rng As Range
Dim MyCell As Range

Set aDoc = ActiveDocument
Set tbl = aDoc.Tables(1)
Set MyCell = tbl.Cell(2, 2).Range

'Remove end of cell marker
MyCell.MoveEnd wdCharacter, -1

On Error GoTo NoExist
If MyCell.FormFields("Text1").Creator <> 0 Then
MsgBox "Bookmark is here"
On Error GoTo 0
End If

Exit Sub
NoExist:
MsgBox "Bookmark not here"

End Sub
 
F

fumei via OfficeKB.com

Alternatively, use a formfield object.

This lists the names of all formfields in the cell, as it is possible you may
have mor ethan one.
Sub CheckItOut()
Dim r As Range
Dim oFF As FormField
Set r = ActiveDocument.Tables(1).Cell(2, 3) _
.Range
If r.FormFields.Count > 0 Then
For Each oFF In r.FormFields
MsgBox oFF.Name
' or whatever you want to do with it
Next
End If
End Sub

Or, if you know there is one, you can check its name. This checks to see if:

1. the cell has one formfield
2. if it does, is its name "Yadda"?

Sub CheckItOut2()
Dim r As Range
Dim oFF As FormField
Set r = ActiveDocument.Tables(1).Cell(2, 3) _
.Range
If r.FormFields.Count = 1 Then
Set oFF = r.FormFields(1)
If oFF.Name = "Yadda" Then
' do whatever
Else
MsgBox "Yes, there is a formfield, " & _
"but it is NOT Yadda."
End If
Else
MsgBox "There is either none, or more than one."
End If
End Sub

You could also use, logically, the result of that formfield. That is, if it
IS named "Yadda", and it has "DabbaDabbaDo" in it, then do something. But if
it has "Blah", do something else.

Sub CheckItOut3()
Dim r As Range
Dim oFF As FormField
Set r = ActiveDocument.Tables(1).Cell(2, 3) _
.Range
If r.FormFields.Count = 1 Then
Set oFF = r.FormFields(1)
If oFF.Name = "Yadda" Then
Select Case oFF.Result
Case "DabbaDabbaDo"
' do whatever for dabbadabbado
Case "Blah"
' do whatever for Blah
Case Else
' do whatever if it is not dabba OR blah
End Select
Else
MsgBox "Yes, there is a formfield, " & _
"but it is NOT Yadda."
End If
Else
MsgBox "There is either none, or more than one."
End If
End Sub

Nick said:
Thanks. That's great.
[quoted text clipped - 27 lines]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top