保存在现有文件夹中

问题描述 投票:0回答:1

我希望此代码始终将 pdf 保存在与文件相同的位置。 如果我在运行代码之前没有手动重新保存此文件,则 pdf 最终会出现在不同的位置。

    Sub Save_Mappe_As_pdf()
                       
    'turns off screen updating & display alerts & etc to go faster
    Application.Calculation = xlCalculationManual
    Application.DisplayAlerts = False
    Application.DisplayStatusBar = False
    Application.EnableEvents = False
    Application.ScreenUpdating = False
       
    'declares the variable mappe based upon text in a cell
    Dim mappe As String
    mappe = Worksheets("Erfassen").Range("g2")
       
    'declares the variable saveLocation the same as this excel document
    Dim saveLocation As String
    saveLocation = ActiveWorkbook.Path 'saves the pdf in the same folder as the recently saved mappe

    'declares & selects the variable sheetArray aka it selects which sheets will be exported as a pdf
    Dim sheetArray As Variant
    sheetArray = Array("Druck", "Infozettel", "Kontrolle", "Beschriftungszettel")
    Sheets(sheetArray).Select
    
    'exports the selected sheets as a pdf
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=mappe, OpenAfterPublish:=True

    'Worksheets("Erfassen").Range("b8").Activate  'moves to the erfassen sheet
    Dim eingabe_sheet As Worksheet
    Set eingabe_sheet = Sheets("Eingabe")
    eingabe_sheet.Activate

    Application.Calculation = xlCalculationAutomatic
    Application.DisplayAlerts = True
    Application.DisplayStatusBar = True
    Application.EnableEvents = True
    Application.ScreenUpdating = True

    End Sub

我在保存位置代码之前尝试过workbook.activate。

excel vba save
1个回答
0
投票

看来您首先通过设置mappe变量来定义文件夹。之后,您使用另一个变量来存储 saveLocation。导出为 PDF 时,请确保使用哪一个作为文件名参数。

在保存 PDF 文件之前,

saveLocation = ActiveWorkbook.Path
返回,例如C:\SomePath.现在发生的情况是,文件被保存到上一级文件夹,名称与文件夹 C:\SomePath.pdf 相同。

相反,导出时,您应该定义路径和有效文件名(例如,Excel 文件名本身不带扩展名 .xlsm)。像

Filename:=saveLocation & "\" & Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 4)

之类的东西

希望这有帮助。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.