Solved

Excel Save As autofill macro

Posted on 2011-09-19
6
328 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:WTC_Services
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for the help guys, both of your solutions helped with my problem!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

762 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

8 Experts available now in Live!

Get 1:1 Help Now