Solved

Excel Save As autofill macro

Posted on 2011-09-19
6
339 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

749 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