Word for Mac 2011: Problems accessing Text Boxes programmatically
I’ve recently moved a little app I wrote in VBA which runs fine on Windows Word 2010 to Mac Word 2011. Ideally, I would like to to seamlessly work on both platforms.
I’m using legacy form fields. (They are called Text Form Field on Word for Windows, Text Box Form Control on Word for Mac)
The Text Boxes I am accessing within VBA have been named: firstName, lastName etc. within the Word document itself.
Here are some code snippets
Const FIRSTNAME = 1
Const LASTNAME = 2
……
Const MAXBOOKMARKS= 22
Dim strRepBookMarks(MAXBOOKMARKS) As String
strRepBookMarks(FIRSTNAME) = "firstName"
strRepBookMarks(LASTNAME) = "lastName"
……
For iBookMark = 1 To MAXBOOKMARKS
docReport.FormFields(strRepBookMarks(iBookMark)).R esult = docBase.FormFields(strRepBookMarks(iBookMark)).Res ult
Next iBookMark
However, I get the following error message when executing it on Word for Mac:
Run-time error ‘5941’
The requested member of the collection does not exist.
From the Immediate Window, I get the same error (iBookMark is 1) on:
debug.print docBase.FormFields(strRepBookMarks(iBookMark)).Res ult
However, on:
debug.print docBase.FormFields("firstName").Result
it returns the string: Mary which is the current value of the text box within the word document.
Some other information:
debug.print strrepbookmarks(firstname)
returns the string: firstName
debug.print firstname
returns 1, which is the value of the Constant I previously defined
What appears to be happening, is that docBase.FormFields(strRepBookMarks(iBookMark)).Res ult
is being evaluated as: docBase.FormFields(1).Result
rather than: docBase.FormFields(“firstName”).Result
which is what I think it should be given that strRepBookMarks(iBookMark) = “firstName”
This works correctly when running on Windows Word 2010. I have also executed the debug.print statements shown above on Word for Windows and it works as expected. (
By this I mean from the Immediate Window on Word for Windows, both
debug.print docBase.FormFields(strRepBookMarks(iBookMark)).Res ult
AND
debug.print docBase.FormFields("firstName").Result
return the string: Mary which is the current value of the text box within the word document.
Any ideas….?
|