Save as PDF with workbook's name

Hatye

Board Regular
Joined
Jan 11, 2010
Messages
143
Hi,
I have the following code:

Code:
Dim fileSaveName As Variant


    wbName = ActiveWorkbook.Name



    fileSaveName = Application.GetSaveAsFilename(initialFilename:=wbName)
    If VarType(fileSaveName) = vbBoolean Then Exit Sub
    
    fileSaveName = Left(fileSaveName, Len(fileSaveName) - 1)
    
    'Hvis Gemini Terreng & Entprenrenør
        If Rows("63:63").Hidden = True Then
            Dim varMyArray() As Variant 'Declares a dynamic array variable
            Dim lngArrayCount As Long
            Dim varMySheet As Variant
                
            For Each varMySheet In Array("Rapport", "Rapport_Linjer", "Rapport_Punkt", "Rapport_Polygon", "Rapport_Tekst", "Rapport_Triangelnett")
                If Len(Sheets(varMySheet).Range("A7")) > 0 Or varMySheet = "Rapport" Then
                    lngArrayCount = lngArrayCount + 1
                    ReDim Preserve varMyArray(1 To lngArrayCount) 'Append the record to the existing array
                    varMyArray(lngArrayCount) = varMySheet
                End If
            Next varMySheet
            
            Sheets(varMyArray).Select
        End If
    
    ' Save as PDF
    Application.DisplayAlerts = True
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, fileName:=fileSaveName, _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, OpenAfterPublish:=True

The issue is regarding initialFilename!

When the workbook is an template (that is, the filename does not contain any file format extension), this works great!

But if I have an xlsm-file (have saved the template), the initialFilename isn't inserted at all. I have tried the following code (just to see what value the code is returning):
Code:
MsgBox (wbName)
and the value is the workbook's name including the file format extension (.xlsm).

What can I do the make the initialFilename to be the workbook name (without file format extension)?
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Assuming there are no periods in the name itself, you could use, for example:
wbName = Split(ActiveWorkbook.Name,".")(0)
 
Upvote 0

Forum statistics

Threads
1,215,947
Messages
6,127,867
Members
449,410
Latest member
adunn_23

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top