Link to home
Start Free TrialLog in
Avatar of john thornton
john thornton

asked on

Word 2013 VBA: refer to form variable in different form

Just a VBA beginner here... Word 2013; I have a template populated by a 3-page form. I currently gather and post/populate data to the template one form at a time. It works:
    Dim fname As Range
    Set fname = ActiveDocument.Bookmarks("firstname").Range
    fname.Text = Me.fnamebox.Value

But I would rather post/populate all of the fields just once, at the end, when the user clicks an update button. So I moved the code to the 3d page. But the macro does not recognize the last statement. I presume it does not recognize "fnamebox," which is a field on the first page of the form.

I have tried various other combinations (replacing "me" with the form name, and with "activedocument," but I am just fishing. What code do I use to get it to recognize "fnamebox"?

Thanks,
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

It is unclear what your situation is. By 'form', do you mean a UserForm?
Can you post your document and/or code, so that we can see and understand?
Avatar of john thornton
john thornton

ASKER

Thanks for the reply. Yes, it is a user form, generated with VBA. (Actually, there are 2 forms, all of which gather data from the user. Some of the code is as follows:

' the user completes the form and clicks the page2butt button. This is part of the code:

Private Sub page2butt_Click()
    Dim fname As Range
    Set fname = ActiveDocument.Bookmarks("fname").Range
    fname.Text = Me.fnamebox.Value
   
    Dim lname As Range
    Set lname = ActiveDocument.Bookmarks("lname").Range
    lname.Text = Me.lnamebox.Value
   

  ' a bunch of those; they update bookmarks in the document.
   

' last thing, I hide this form, and display the next one.
' In the 3d form, I want to refer to field names I used in the first form, i.e. "fname."  
   
' MsgBox ("done page 1")
    ResumeFormP1.hide
    ResumeFormP2.Show
   
End Sub


Thanks for your help.
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ah, that's the piece I was missing. Thanks much!
excellent and prompt. Thanks,
I am still having trouble with this. Wen I code Public Dim fname as Range, I get a compile error on the word DIM: "Expected identifier"
User generated image
When i remove the word "DIM," it accepts it, and it works in the function in which I declared it:
User generated image
(I also changed to using the variable as a string rather than a range.)

But it loses it in resumeformP2, resumeformP3, resumeformP4, resumeformP5, and resumeformP6.

The field (myfname) is empty in subsequent subroutines.

I have tried putting the declaration in the Declarations section of ThisDocument. The result is the same.

Obviously, there is still a piece of the puzzle that I am missing.

Can you help?

Thanks.
Sorry. That was careless of me. 'Public' is used instead of  'Dim', not as well as.

You see that the text of the line is coloured red, as I would have done if I had actually typed it into the VBA IDE. This means that the line contains a syntax error.
Thanks; I figured that it was just a "brain freeze." I understand a compile error. That fix was easy. But is till does not solve my current problem.

Thanks for your attention.
I will have to study your blocking problem
Sorry. I've been really busy today.

I can't see it, but you should be addressing the variable as 'ResumeForm1.myfname' in the code for the other forms. If you just use 'fname' then, by default, the  VBA compiler assumes that it is a new variable and creates a local one with that name, assigning it a default value, which is "" for string variables.

You can get the complier to warn you of this possibility by putting Option Explicit at the top of the module's code. It will then raise a compile-time error: 'Variable not defined' under such circumstances.

You can make your application automatically put 'Option Explicit' at the top of each module when it is created.  Open  the Options dialogue via Tools/Options. On the editor tab, check the box opposite 'Require Variable Declaration'. I strongly recommend that you do this in any application where you use VBA
wow... gives me a lot to chew on. I thought i might be going about it the wrong way. But I did not know which way to turn. And I could find nothing on the internet or in books that would point me in the right direction on this specific situation.

Thanks much for your attention and help.