Word VBA - if function and form fields.

  • Hi all, I'm trying to use VBA for word to give an outcome based on a varying value in a form field. My problem is that I get an error every time I try to run the macro. I'm trying to use if/elseif functions but if there is a better way I'd be happy to use it,
    a sumple of my existing code is included below. Any help would be appreciated, I hate Word VBA it's so hard compaired to Excel.
    "Text86" is populated from a VBA calculation which outputs a sum total of several other fields so it should be recognisable as a number, I'm sure this isn't the problem.


    Public Sub MAIN()
    If ActiveDocument.Bookmarks("Text86") > 22 Then
    ActiveDocument.Bookmarks("Text94").Value = "Poor"
    ElseIf ActiveDocument.Bookmarks("Text86") > 36 Then
    ActiveDocument.Bookmarks("Text94") = "Average"
    ElseIf ActiveDocument.Bookmarks("Text86") > 48 Then
    ActiveDocument.Bookmarks("Text94") = "Good"
    ElseIf ActiveDocument.Bookmarks("Text86") > 56 Then
    ActiveDocument.Bookmarks("Text94") = "Very Good"
    End If


    End Sub


    Thanks


    Nathan

  • Re: Word VBA - if function and form fields.


    Anyone? please this is driving me nuts and I'm sure I should be able to get this to work, possibly its a simple argument or function error?

  • Re: Word VBA - if function and form fields.


    See pic if I delete the .value I get another error but I can't remember what it is, try creating two text boxes then running the macro you'll see what I mean. I'm totally stumped but then I don't understand lots of the basic stuff for macros but can do some of the more complex stuff (in excel)

  • Re: Word VBA - if function and form fields.


    The file that you attached doesn't seem to have come through properly.


    When I downloaded and unzipped it all I got was a blank jpeg.

    Boo!:yikes:

  • Re: Word VBA - if function and form fields.


    Ok dunno why cause it works for me.
    the error occurs highlighting the".value =" on the second line of code.
    error reads: "Compile Error.
    Meathod or or data member not found." but I know that bookmark exists.

  • Re: Word VBA - if function and form fields.


    Anyone? I suspect for someone who is decent with VBA they could past my code into a module then create the two text boxes and rename them to the bookmark names and would then very quickly be able to figure out the problem but I might be wrong.
    I just don't know enough about the protocols to figure this out.

  • Re: Word VBA - if function and form fields.


    As far as I know Value is not a property of or method of the Bookmark object.


    Could you actually attach the Word document, or perhaps just a sample of it?


    BTW I got your attachment to work.

    Boo!:yikes:

  • Re: Word VBA - if function and form fields.


    Hi Nathan,


    Don't get fired on Word (just kidding).


    There is a way to do all your stuff. Just use the FormFields collection instead of Bookmarks collection. That's all, Your work will be done.


    Replace the code (ActiveDocument.Bookmarks("Text94").Value) with ActiveDocument.FormFields("Text94").Result.


    If you have any problem reply back.


    Regards...
    Raghava

    :thanx:
    [SIZE=1]Raghava[/SIZE] :cool:

  • Re: Word VBA - if function and form fields.


    That's Great Raghava, that's fixed the existing problem now I get another error relating to the IF line, I get "Runtime error 13, Type mismatch" on the first IF line. Any ideas??

  • Re: Word VBA - if function and form fields.


    Ah it's ok I changed the line from refering to Bookmarks to refering to FormFields, that's got the whole thing working. Thanks very much for all your help everyone I really appreciate it.

  • Re: Word VBA - if function and form fields.


    I have forgot writing other replacement. Here you go....


    Replace ActiveDocument.Bookmarks("Text86") with ActiveDocument.FormFields("Text86").Result

    :thanx:
    [SIZE=1]Raghava[/SIZE] :cool:

  • Re: Word VBA - if function and form fields.


    Yeah I got it all now, thanks very much I really appreciate it. New it would be something rediculously simple.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!