PDA

View Full Version : Solved: problem with variables in VBA



phill952000
11-28-2008, 05:29 AM
Hi,

i currently have my word set up to extract data from text fields (bookmarks) and saving them into different variables.

i then use the variables in saving the document as i want my document to save using what is in the text fields as its name.

i have done this succesfully and the variables are displaying the correct information however before the variables there is some text that says "FORM TEXT" and i do not want this to happen.

what can i do to remove "FORMTEXT" from preceding the variables.

cheers

TonyJollans
11-28-2008, 09:51 AM
It sounds like you have Field Codes displayed (or set to behave in code as though displayed - using TextRetrievalMode) but I can't quite see what you have done. can you post your code?

fumei
11-28-2008, 10:22 AM
"extract data from text fields (bookmarks) " If you are getting FORMTEXT, then you are likely getting data from "bookmarks", you are NOT getting data from text formfield itself. Formfields and Bookmarks are NOT the same.

Attached is a demo showing this. There is data ("Gerry") in the text formfield.

Click "With Codes" on the top toolbar. It displays a message box:

Formfield Result = Gerry
Bookmark text = FORMTEXT

Click "Without Codes" on the top toolbar. It displays a message box:

Formfield Result = Gerry
Bookmark text = Gerry

Formfields have bookmarks. They are NOT bookmarks.

If you are getting FORMTEXT, then you likely (as Tony mentions) have field codes displayed. Further, you are likely getting your data using .Range.Text of the bookmark, rather than .Result of the formfield.

Again, formfields HAVE bookmarks, but are not bookmarks themselves. If you want the result of the formfield it is better to use the actual value of the formfield...NOT the value of its bookmark.

phill952000
12-01-2008, 06:25 AM
thanks alot guys, much appriciated

i have rectified the problem

fumei
12-01-2008, 11:58 AM
Care to share how?

phill952000
12-02-2008, 01:06 AM
yeah sure sorry, i am new to this forum thing lol.

my original code was this:

pNumber = ActiveDocument.Bookmarks("Text27").Range.Text

which was, for some reason, attatching the word "FORMTEXT" before it was displaying the value of the variable.

i then changed my code to this:

Selection.GoTo What:=wdGoToBookmark, Name:="Text27"
pNumber = Selection.text

which seemed to do the trick but now i have to unprotect the document before it will allow me to save it otherwise i get an error.

cheers guys

fumei
12-02-2008, 01:28 PM
1. Avoid using Selection if possible, and there is absolutely no need (whatsoever) to make any Selection here.

2. try to read posted replies carefully. The best way to get values out of formfields is using their .Result.

Your original code?
pNumber = ActiveDocument.Bookmarks("Text27").Range.Text
As I explained the reason you got the FORMTEXT is because you were using .Range.Text of the bookmark.

Proper code:
pNumber = ActiveDocument.Formfields("Text27").Result


NOT, repeat NOT:
Selection.GoTo What:=wdGoToBookmark, Name:="Text27"
pNumber = Selection.text
1. that is TWO instuctions
2. it uses Selection, which means the visual screen display changes. There is no need to do this.

phill952000
12-03-2008, 01:41 AM
thanks for your reply,

your proposed code work a treat

again many thanks

fumei
12-03-2008, 12:51 PM
You are welcome.

lucas
12-05-2008, 09:54 AM
mark this one solved too Phill please.