PDA

View Full Version : Solved: VBA to SAVE FILE



megha
12-07-2009, 10:58 AM
I am working on a macro for word document that save to specific drive on a click and stamp date and time. Its work fine.

I have included text boxes on document and reference them into code to MUST fill-in that information otherwise the message box appear to ask to fill-in the info. This part of my code is not working. When I click the command button to save the file with empty text boxes the message box does not appear to ask to fill-in the info.

I am using the following code. I would appreciate any help. Thanks!


Private Sub CommandButton1_Click()
Dim sFileName As String
Dim sPath As String
If txtDate.Value = " " Then
MsgBox "Please Enter Date", vbCritical
ElseIf txtShift.Value = " " Then
MsgBox "Please Enter Tank #", vbCritical
ElseIf txtShift1.Value = " " Then
MsgBox "Please Enter Calculated Tape", vbCritical
ElseIf txtShift2.Value = " " Then
MsgBox "Please Enter Local Varec", vbCritical
ElseIf txtShift3.Value = " " Then
MsgBox "Please Enter OMCC Gauge", vbCritical
ElseIf txtShift4.Value = " " Then
MsgBox "Please Enter Difference", vbCritical
Else
CommandButton1.Enabled = False

sFileName = Format(DateValue(Now()), "mmm_dd_yyyy") & "_" & _
Format(TimeSerial(Hour(Now()), Minute(Now()), Second(Now())), "hh_mm_ss_AM/PM") & "TANK" & txtShift.Value
sPath = "S:\OMCC\Tank Gauging/"
ThisDocument.SaveAs FileName:=sPath & sFileName, FileFormat:=xlNormal, ReadOnlyRecommended:=False
Application.Documents.Close
End If
End Sub

lucas
12-07-2009, 11:26 AM
When I remove the extra space it works for me. You don't say what kind of textboxes so I assumed activeX. See attached:

Change:
If txtDate.Value = " " Then
to

If txtDate.Value = "" Then

TonyJollans
12-07-2009, 11:26 AM
Always use Option Explicit in your code.

Unless you have defined and set txtShift(n) at module level, they will be treated as (empty) undeclared variables, not as document elements.

megha
12-07-2009, 12:30 PM
Thanks for your help! It does work when I removed the extra space.

fumei
12-07-2009, 12:33 PM
1. I will strongly agree with Tony: Always use Option Explicit in your code.

2. "I have included text boxes on document"

Please clarify what type of textbox. If they are text formfields, then to use them as document elements:
Private Sub CommandButton1_Click()
Dim sFileName As String
Dim sPath As String
Dim DocFF As FormFields()
Set DocFF = ActiveDocument.Formfields

If DocFF(txtDate).Result = " " Then
MsgBox "Please Enter Date", vbCritical
ElseIf DocFF(txtShift).Result = " " Then
MsgBox "Please Enter Tank #", vbCritical
ElseIf DocFF(txtShift1).Result = " " Then
MsgBox "Please Enter Calculated Tape", vbCritical
ElseIf DocFF(txtShift2).Result = " " Then
MsgBox "Please Enter Local Varec", vbCritical

' etc.
If they are ActiveX textboxes as Lucas assumed, then note that his use of txtDate.Value will only work if the procedure is in the ThisDocument code module. It will not work in a standard module - they have to be fully qualified.