Solved

Excel Save As autofill macro

Posted on 2011-09-19
6
331 Views
Last Modified: 2012-05-12
Hi,

I'm trying to write a macro for my excel file that saves the document with a specific name.

E.g. for a document like this, i want to be able to push the macro button

experts-saveas.xlsx

and have a save as screen pop up with my specific data auto-filling the document name section

screen to pop up
thanks!
0
Comment
Question by:WTC_Services
  • 3
  • 2
6 Comments
 
LVL 33

Expert Comment

by:Norie
ID: 36565135
This will take the filename from the A3:C3, formatting the date to ddmmyyyy as the format in the cell isn't valid.
Option Explicit

Sub Button1_Click()
Dim wbThis As Workbook
Dim strInitialName As String
Dim strSaveName As Variant

    Set wbThis = ThisWorkbook
    strInitialName = Range("A3") & "_" & Format(Range("B3"), "ddmmyyyy") & "_" & Range("C3") & ".xlsm"
    
    
    strSaveName = Application.GetSaveAsFilename(strInitialName, "Excel Workbook (*.xlsm),*.xlsm)")
    
    If TypeName(strSaveName) = "String" Then
        wbThis.SaveAs strSaveName, xlWorkbookNormal
    Else
        MsgBox "File not saved"
    End If
End Sub

Open in new window

0
 

Author Comment

by:WTC_Services
ID: 36584723
Thanks imnorie,

This should help a lot with naming my file, my dilemma now is that i am trying to save to pdf, currently my macro is like this:

        ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "C:\Users\nickf\Desktop\Wilson.pdf", Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
        True

now i wish to incorporate your method with my current script, can anyone help?

WTC
0
 
LVL 2

Accepted Solution

by:
geoffkk earned 250 total points
ID: 36814954
Sounds simple:

Sub Button1_Click()
Dim wbThis As Workbook
Dim strInitialName As String
Dim strSaveName As Variant

    Set wbThis = ThisWorkbook
    strInitialName = Range("A3") & "_" & Format(Range("B3"), "ddmmyyyy") & "_" & Range("C3") & ".xlsm"
   
   
    strSaveName = Application.GetSaveAsFilename(strInitialName, "PSF file(*.pdf),*.pdf)")
   
    If TypeName(strSaveName) = "String" Then
       ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        strSaveName , Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
        True
    Else
        MsgBox "File not saved"
    End If
End Sub

Geoff
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:WTC_Services
ID: 36906470
Geoff,

Thank you, this brings up the save as pdf box but for some reason "Wilson_20092011_Melbourne" doesn't appear in the file name section. Maybe there is something wrong with strSaveName??

WTC
0
 
LVL 33

Assisted Solution

by:Norie
Norie earned 250 total points
ID: 36906721
If you want the initiall filename to appear then it needs to have the same extension, ie pdf, as the filter you are using.
Option Explicit

Sub Button1_Click()
Dim wbThis As Workbook
Dim strInitialName As String
Dim strSaveName As Variant
Dim strPath As String

    Set wbThis = ThisWorkbook

    strInitialName = Range("A3") & "_" & Format(Range("B3"), "ddmmyyyy") & "_" & Range("C3") & ".pdf"

    strPath = "C:\Users\nickf\Desktop\"

    strSaveName = Application.GetSaveAsFilename(strInitialName, "PDF Document (*.pdf),*.pdf")

    If TypeName(strSaveName) = "String" Then

        wbThis.ExportAsFixedFormat Type:=xlTypePDF, _
                                                Filename:=strPath & strSaveName, _
                                                Quality:=xlQualityStandard, _
                                                IncludeDocProperties:=True, _
                                                IgnorePrintAreas:=False, _
                                                OpenAfterPublish:=True

    Else
        MsgBox "File not saved"
    End If

End Sub

Open in new window


Note, this saves the workbook the code is in (ThisWorkbook).

If you want to save another workbook then you'll need to give details.
0
 

Author Closing Comment

by:WTC_Services
ID: 36907258
Thanks for the help guys, both of your solutions helped with my problem!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now