Complie Error: Variable not defined

Smoo

New Member
Joined
Sep 15, 2006
Messages
9
I'm new to writting code and am getting an error add "Compile Error: Variable not defined" on the text "txtDate = ActiveDocument.Variables("Date")"
Code:
Option Explicit

Private Sub cmdCancel_Click()
  Unload Me
End Sub

Private Sub cmdOK_Click()
  ' Prevent variable from being deleted
If txtDate = "" Then txtDate = " "
  ActiveDocument.Variables("Date") = txtDate
  If txtClient_Name = "" Then txtClient_Name = " "
  ActiveDocument.Variables("Client Name") = txtClient_Name
  If txtClient_ID_# = "" Then txtClient_ID_# = " "
  ActiveDocument.Variables("Client ID #") = txtClient_ID_#
  ' Update fields in document
  ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Fields.Update
  Unload Me
End Sub

Private Sub UserForm_Initialize()
  ' Load variables into text boxes
  On Error Resume Next
  txtDate = ActiveDocument.Variables("Date")
  txtClientName = ActiveDocument.Variables("Client Name")
  txtClientID# = ActiveDocument.Variables("Client ID #")
End Sub

Any help would be great!

Thank you,
SMOO
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,300
Office Version
  1. 365
Platform
  1. Windows
Is this an Excel question or a Word question?

Where are you actually running this code?
 

Smoo

New Member
Joined
Sep 15, 2006
Messages
9
I apologize for not specifying the program in my first post; I am using Word 2003.
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,300
Office Version
  1. 365
Platform
  1. Windows
So is this code being run in Word?

Do you have a control called txtDate?
 

Smoo

New Member
Joined
Sep 15, 2006
Messages
9

ADVERTISEMENT

Thank you for your assistance Norie. I'm not sure if this is what you mean by control...but I do have a Userform thingy that I made and "Date" is one of the questions. The box is supposed to pop up when the form is opened and ask the user for information that is then automatically put into the form.
I used another persons code as a starting point and now I'm stuck.

Smoo
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,300
Office Version
  1. 365
Platform
  1. Windows
What's the name of the control on the userform?

Is is txtDate?
 

Smoo

New Member
Joined
Sep 15, 2006
Messages
9

ADVERTISEMENT

No it is simply "Date". I was under the assumption that "txt" was needed because that is how the original code was.

Smoo
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,300
Office Version
  1. 365
Platform
  1. Windows
To get the existing code to run change txtDate to Date.

But a better solution would be to change the name of the control on the form to txtDate.
 

Smoo

New Member
Joined
Sep 15, 2006
Messages
9
Wow, it was that simple. Boy, is my face red. Thank you so much for all of your help. Your suggestion worked great.

Thank you,
Smoo
 

Forum statistics

Threads
1,136,263
Messages
5,674,710
Members
419,521
Latest member
Jasonnie

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top