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

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Is this an Excel question or a Word question?

Where are you actually running this code?
 
Upvote 0
I apologize for not specifying the program in my first post; I am using Word 2003.
 
Upvote 0
So is this code being run in Word?

Do you have a control called txtDate?
 
Upvote 0
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
 
Upvote 0
What's the name of the control on the userform?

Is is txtDate?
 
Upvote 0
No it is simply "Date". I was under the assumption that "txt" was needed because that is how the original code was.

Smoo
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,213,551
Messages
6,114,266
Members
448,558
Latest member
aivin

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
Back
Top