PDA

View Full Version : Another hapless filesaveas question :)



johnkubik
04-20-2010, 06:54 AM
Hey everyone,

I am trying to make a macro in vba that will automatically append the date and time to a word filename on save. For instance, I have a file named temp-serv.docx and I want the end result of the macro to, when the user hits the save button, to make the file temp-serv-20100420-0910.docm and when they resave it it updates the date and time so if they save it again it becomes temp-serv-20100420-0930.docm

There are a few problems I have solved and some that still need resolution, and I need your help!

Firstly, it was a pain to find out how to make a filesave or filesaveas event. I spent a while trying to find a Beforesave or beforesaveas event handler, but all the information on beforesaves was for excel and I couldn't find a SINGLE way to do it in office word vba. So, I thankfully found a reference to making a 'public sub filesave' routine that would proc when the user hit file->save, and conversely a public filesaveas sub for file->saveas. Hopefully this forum post helps others that google " office word 2007 beforesave " :)

Second, working with the filenames and saving procedure is proving to be a lot more difficult than I originally expected. I was able to effect the initial filename in the filesaveas dialog box to display a "suggested filename" that automatically has the date and time added to it making it easier for the user, but that is where I am running into a problem.
Here is my code thus far:

Sub FileSaveAs()
Dim dlgSaveAs As FileDialog

'filesaveas dialog box
Set dlgSaveAs = Application.FileDialog(2)
With dlgSaveAs
'sets the inital file name suggested to the user in the file dialog box that pops up
'appends the current date and time to the current file name

.InitialFileName = ThisDocument.Name & "-" & Format(Date, "yyyymmdd") & "-" & Format(Time, "hhmm")


If .Show = -1 Then
'opens the file save as dialog box and saves the document as the text in the box
Application.ActiveDocument.SaveAs .SelectedItems(1), FileFormat:=wdFormatXMLDocumentMacroEnabled

'fileformat sets the file format to a .docm

End If
End With
Set dlgSaveAs = Nothing
End Sub So, the .InitialFileName sets what is suggested to the user when the filesaveas dlg box opens. I would like the date-time to appear appended to the filename at this point, which is what I thought I was doing with the format()s.



It doesnt seem to pass the .initialfilename to the 'if' statement either, given what I have seen with the selecteditems(1) I have tried watching.



The problem comes in with getting the current filename 'ThisDocument.Name' to work correctly. I tried doing some replace() and instr()'s but I couldnt get it to work correctly when the .initialfilename was passed to the 'selecteditems(1).' Also, the fileformat is not correctly setting the document to a docm extension, which is causing the resulting .docx to be unreadable.



The .initialfilename is a problem using len() because when I added a watch on it it seemed to take into account the current save directory (eg. c:\~) which is why I think my replace() and instr()'s weren't working.


One last thing which is a PROBLEM is if it is a new document (which means it is a blank document with no fliename)...how should I use a Document_New() to solve a blank file name?


The filename problem may just be an error in my logic because I know the ThisDocument.name is just going to keep tacking on dates and times...I was hoping to be able to remove the old dates and times and append new ones when they click save, but it isn't apparent as to how.


Sorry for the long forum post, thanks for any help you can provide. If anything wasn't clear please let me know.


Microsoft Visual Basic 6.5.1040
Office 2007 12.0.6514

johnkubik
04-20-2010, 09:22 AM
So, I figure it out...kind of.

Sub FileSaveAs()
Dim dlgSaveAs As FileDialog
Dim ddr As String
Dim ddr1 As String
Dim ddr2 As String
Dim ddr3 As String
Dim ddr4 As String
Dim ddr5 As Integer
Dim ddr6 As Long



Set dlgSaveAs = Application.FileDialog(2)
With dlgSaveAs
ddr = ThisDocument.Name
ddr6 = Len(ddr)
ddr5 = InStrRev(ddr, "-", InStrRev(ddr, "-") - 1)
ddr3 = Left(ddr, ddr5)

.InitialFileName = ddr3 & Format(Date, "yyyymmdd") & "-" & Format(Time, "hhmm") & ".docm"



If .Show = -1 Then
Application.ActiveDocument.SaveAs .SelectedItems(1), FileFormat:=wdFormatXMLDocumentMacroEnabled
End If
End With
Set dlgSaveAs = Nothing



End Sub





It works okay, but the default filetype is the only thing I have yet to fix.

fumei
04-20-2010, 10:11 AM
If you want to have this automated (the user has no choice...the file IS saved with a new name), then why even bothe rwith a dialogue. Just rewrite the Save procedure to do exactly what you want.

johnkubik
04-20-2010, 10:46 AM
so they have the ability to change the file name if they want to. For instance, if their time zone is messed up, or they want to make it look like they did work before they actually did it. :rotlaugh:

fumei
04-20-2010, 12:57 PM
fair enough

douglas01
05-19-2010, 04:09 PM
Hi!This thread is very interesting I really like that I discovered this thread that Ihave been looking for I will keep checking out this post on to get any new ideas about this post healthlifesource com]Health Information