Link to home
Create AccountLog in
Avatar of Bevos
Bevos

asked on

Word 2010 Macro Help, save as PDF with specific file name

Good afternoon, I have a macro that I would like to edit but I'm not sure how to proceed.  The macro is as follows
Private Sub SaveAsPDF()
  With Dialogs(wdDialogFileSaveAs)
         .Name = "Draft_[Topic]_[Date]"
         .Format = wdFormatPDF
         .Show
     End With
End Sub

Open in new window


Currently, the macro prompts the user to save a file with the format Draft_[Number]_[Date].  I would like the macro to be able to identify these elements given a standard filename in the format [number]_[additional text].docx.

So here is an example, I perform the SaveAsPDF macro in a file named 010902_Topic_Name.docx.  I would like it to save as a pdf titled Draft_010902_11142012.pdf where 11142012 is derived from today's date (MM/DD/YYYY without the slash).  

Could someone teach me how to do this?

Thanks,
Bev
Avatar of WebDevEM
WebDevEM
Flag of United States of America image

Something like this should do the trick...  
Sub SaveAsPDF()
'
' SaveAsPDF Macro
'
'
Dim sDay, sMonth, sYear, sFullDate, fName, Topic

sDay = DatePart("d", today)
sMonth = DatePart("m", today)
sYear = DatePart("yyyy", today)
sFullDate = sMonth & sDay & sYear
fName = ActiveDocument.Name

Topic = Split(fName, "_")

With Dialogs(wdDialogFileSaveAs)
         .Name = "Draft_" & Topic & "_" & sFullDate
         .Format = wdFormatPDF
         .Show
     End With
End Sub

Open in new window

Avatar of Ken Butters
Private Sub SaveAsPDF()
  With Dialogs(wdDialogFileSaveAs)
         .Name = "Draft_[Topic]_" & Format(Now(), "MMDDYYYY") & ".PDF"
         .Format = wdFormatPDF
         .Show
     End With
End Sub

Open in new window


The above code should name it according to your description.

The built in function Now() returns current date and time.   The format function reformats the date/time according to the format string "MMDDYYYY".

The following page shows some of the format specifiers that can be used for date/time manipulation.

http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
Avatar of Bevos
Bevos

ASKER

Thank you both for these replies! I believe WebDevEM understood everything that I was trying to accomplish, but I'm getting a snag on the code line, ".Name = "Draft_" & Topic & "_" & sFullDate".  The code from buttersk works, but it isn't attempting to name the file according to the current file name (to pull the value for 'topic').  I'll keep trying to tinker with what you both have provided to come to a solution, but any help would be welcomed as always.

Thanks again for the responses,
Bev
I see... I guess I didn't quite understand what you wanted with topic.

Try this:

Private Sub SaveAsPDF()

  Dim Topic As String
  Topic = CreateObject("scripting.filesystemobject").getbasename(ActiveDocument.Path)

  With Dialogs(wdDialogFileSaveAs)
         .Name = "Draft_" & Topic & "_" & Format(Now(), "MMDDYYYY") & ".PDF"
         .Format = wdFormatPDF
         .Show
     End With
End Sub

Open in new window

Avatar of Bevos

ASKER

Thanks again ButterSK!  This one is still having a small issue but I think you understand my question now.  For example I have a file on my desktop now called 010203_ProjectA_Bevos.docx.  When I run the SaveAsPDF macro it uses returns this filename currently, "Draft_Desktop_11152012.pdf".  So it is very close to correct except it should be "Draft_ProjectA_11152012.pdf".  Do you know why it is calling 'Desktop' instead of 'ProjectA'?  I'm not familiar with the .getbasename command.

Sorry for any confusion,
Bev
Its just a quirk it worked that way.. but I thought that was what you wanted.

In your first example you mentioned:

Filename = 010902_Topic_Name.docx
PDF name = Draft_010902_11142012.pdf

based on your latest example I am assuming now that you really want:
PDF name = Draft_010902_Topic_Name_11142012.pdf

In the original example... you don't have "_Topic_Name" carried forward... so I thought you only wanted info up to the first underscore.

If I understand it correctly now... Here is an update.
Private Sub SaveAsPDF()

  Dim Topic As String
  Topic = CreateObject("scripting.filesystemobject").getbasename(ActiveDocument.Name)

  With Dialogs(wdDialogFileSaveAs)
         .Name = "Draft_" & Topic & "_" & Format(Now(), "MMDDYYYY") & ".PDF"
         .Format = wdFormatPDF
         .Show
     End With
End Sub

Open in new window

Avatar of Bevos

ASKER

Hi again ButterSK, I'm not sure if I'm explaining well so I'll try again.  

Original filename:
010203_ProjectA_Bevos.docx

Currently retrieved filename from macro:
Draft_010203_ProjectA_Bevos_11152012.pdf

Desired filename:
Draft_ProjectA_11152012.pdf

So essentially your code is transposing the whole name of the file, whereas I had wanted just the topic information contained in that name (in this example the 'ProjectA' text enclosed within the first two '_' characters).  

Thanks,
Bev
ASKER CERTIFIED SOLUTION
Avatar of Ken Butters
Ken Butters
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Hello again...

Merging my code and ButterSK's gives this:
Sub SaveAsPDF()

Dim fName, Topic

fName = ActiveDocument.Name

Topic = Split(fName, "_")

With Dialogs(wdDialogFileSaveAs)
         .Name = "Draft_" & Topic(1) & "_" & Format(Now(), "MMDDYYYY") & ".pdf"
         .Format = wdFormatPDF
         .Show
     End With
End Sub

' Original document 010203_ProjectA_Bevos.docx
' Results in Draft_ProjectA_11162012.pdf

Open in new window

I have to say, I like the way ButterSK got the date much better than my original method.  If you end up using this version, please be sure to split the points since my latest version is based on ButterSK's with revisions to it.   The key to getting the ProjectA is splitting the filename into an array and taking the 2nd item

UPDATE:  Ahh, looks like I took too long to post mine, and ended up at the same result.
Avatar of Bevos

ASKER

Great work! Thanks so much for this awesome solution