VBA code help (saving to active workbook file location

kayne93

New Member
Joined
Feb 28, 2023
Messages
3
Office Version
  1. 365
  2. 2021
  3. 2019
Platform
  1. Windows
Hi,
I am wanting to save my active sheet (with print set up) as a PDF with variable & set name which does work.
but i also want it to save the PDF in the active workbook file location. Currently it just saves to my documents. help would be greatly appreciated!

Sub CLEARANCETEMPLATE_Button1_Click()
File_name = ActiveWorkbook.Name
If InStr(Filename, ".") > 0 Then
Filename = Left(Filename, InStrRev(Filename, ".") - 1)
End If

File_name = Range("C7") & " - CLEARANCE "
Destination = ThisWorkbook.Path
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=File_name, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
ActiveWorkbook.Path & "\"

returns the path in which the active workbook is saved.

ThisWorkbook.Path & "\"

returns the path in which the workbook in which the code resides is saved.

So ActiveWorkbook.Path & "\" & File_name
or
ThisWorkbook.Path & "\" & File_name

as appropriate can be used.
 
Upvote 0
1677590075539.png


i tried both and its still saves to document and not the file location?
(im completely new to VBA so apologies)
 
Upvote 0
You had File_name and File_name. Declare all variables before use them.

Have Option Explict at the top to enforce declaration.

Use Quick-wrap selection as VBA code. It is on the Mr Excel post editor menu.

VBA Code:
Option Explicit

Sub CLEARANCETEMPLATE_Button1_Click()
Dim File_name  As String
Dim Destination As String

    File_name = ActiveWorkbook.Name
    
    If InStr(File_name, ".") > 0 Then
        File_name = Left(File_name, InStrRev(File_name, ".") - 1)
    End If

    File_name = Range("C7") & " - CLEARANCE "

    Destination = ThisWorkbook.Path
    
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=ActiveWorkbook.Path & "\" & File_name, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
        
End Sub
 
Upvote 0
Solution
You had File_name and File_name. Declare all variables before use them.

Have Option Explict at the top to enforce declaration.

Use Quick-wrap selection as VBA code. It is on the Mr Excel post editor menu.

VBA Code:
Option Explicit

Sub CLEARANCETEMPLATE_Button1_Click()
Dim File_name  As String
Dim Destination As String

    File_name = ActiveWorkbook.Name
  
    If InStr(File_name, ".") > 0 Then
        File_name = Left(File_name, InStrRev(File_name, ".") - 1)
    End If

    File_name = Range("C7") & " - CLEARANCE "

    Destination = ThisWorkbook.Path
  
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=ActiveWorkbook.Path & "\" & File_name, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
      
End Sub

You had File_name and File_name. Declare all variables before use them.

Have Option Explict at the top to enforce declaration.

Use Quick-wrap selection as VBA code. It is on the Mr Excel post editor menu.

VBA Code:
Option Explicit

Sub CLEARANCETEMPLATE_Button1_Click()
Dim File_name  As String
Dim Destination As String

    File_name = ActiveWorkbook.Name
   
    If InStr(File_name, ".") > 0 Then
        File_name = Left(File_name, InStrRev(File_name, ".") - 1)
    End If

    File_name = Range("C7") & " - CLEARANCE "

    Destination = ThisWorkbook.Path
   
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=ActiveWorkbook.Path & "\" & File_name, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
       
End Sub
thank you, this works perfectly!
 
Upvote 0

Forum statistics

Threads
1,214,925
Messages
6,122,298
Members
449,077
Latest member
Rkmenon

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