PDA

View Full Version : Word 2003 - VBA "FileName" need help!



megha
01-27-2012, 11:35 AM
Hello...

I am macro beginner and digging with this macro since last few days. I would really appreciate any help.

The macro is working absolutely fine but the Filename causing issue when it come to delete the old version.

Attached word file has three buttons:

1) First button labeled, “Create” simply create a checklist as version 1 with current date and time as file name and SaveAs the file to “Temp Folder”
2) Second button labeled, “Modify File” perform two jobs:
Ř SaveAs the same checklist from step one as version 2 with current date and time as file name to the same folder as in step 1 (i.e. “Temp Folder”). It works fine.
Ř Delete old file meaning delete version 1 which was created in step one. And this is causing a issue, error message pop up stating “File not found” because for example; if I perform 1st step now the file will SaveAs as “TempJan_27_2012_A.Team_Day01_00_12_PM Rev1” then I perform 2nd step the file will SaveAs as “TempJan_27_2012_A.Team_Day01_05_10_PM Rev2” and looking for “TempJan_27_2012_A.Team_Day01_05_10_PM Rev1” to delete instead of “TempJan_27_2012_A.Team_Day01_00_12_PM Rev1”
3) Third button labeled, “Final Save” (I have not work on it yet) will do the basically same jobs as second button but will SaveAs the file to “Completed folder” and will delete version 2 (which is created in step 2) from “temp folder.”

Can some one please help me? I know if I will keep something else as a file name instead of current date and time it will solve my problem but I can’t keep anything else than the current time and date. Any solution? Any alternate way to meet my goal? A waiting for someone reply!!!

Thank you in advance!!!!!!!!!

fumei
01-27-2012, 08:08 PM
There are so many things wrong with your code you need to really work things through.

Put Option Explicit at the top of your code module. To have it in all modules - and DO this! - in the VBA editor, go Tools, Options and check Require Variable Declaration.

RE yur code:Private Sub cmdCreate_Click()
Dim sFileName As String
Dim sPath As String
Dim sRev As Integer
If txtName.Value = " " Then
MsgBox "Please Enter your name on Page 1", vbCritical
Else
cmdModify.Enabled = True
cmdSave.Enabled = True
cmdCreate.Enabled = False
sRev = 1
txtRev.Text = sRev

sFileName = Format(DateValue(Now()), "mmm_dd_yyyy") & "_" & Team.Value & "_" & Shift.Value & _
Format(TimeSerial(Hour(Now()), Minute(Now()), Second(Now())), "hh_mm_ss_AM/PM")
sFileName = sFileName & " Rev " & sRev & ".doc"
sPath = "C:\Gerry Pic\New Folder"
ThisDocument.SaveAs FileName:=sPath & sFileName, FileFormat:=xlNormal, ReadOnlyRecommended:=False
MsgBox "Checklist has been saved to Temp Folder"
ThisDocument.Close SaveChanges:=False
End If
End Sub
do you realize that if txtName.Value = " " you get the message "Please Enter your name on Page 1"....and then NOTHING else happens?

No file is creat

You need to use basic debugging methods and tools.

Option Explicit and always step through your code to make sure what you want (think) is happening is actually happening.

Step through by pressing F8.

fumei
01-27-2012, 08:52 PM
The real problem is in your Modify code. You have:sFileName = Format(DateValue(Now()), "mmm_dd_yyyy") & "_" & Team.Value & "_" & Shift.Value & _
Format(TimeSerial(Hour(Now()), Minute(Now()), Second(Now())), "hh_mm_ss_AM/PM")

The previous file has a DIFFERENT timestamp. The code gets the CURRENT timestamp, not the one in the previous filename.

You need to actually use a Rev number, which you do not appear to be using. Aee you really going to put into the document?

fumei
01-27-2012, 08:54 PM
And I have to ask why you are doing this in the first place.

megha
01-30-2012, 08:48 AM
Is it possible without doing it at first place? How can i do it?

megha
02-01-2012, 08:28 AM
any suggestions????

fumei
02-03-2012, 08:23 PM
Please see your latest post asking the same question.