通过宏定义文件名

时间:2016-10-12 15:27:16

标签: excel-vba vba excel

我有以下代码来保存现有excel文件中的pdf文件。

Dim FSO As Object
Dim s(1) As String
Dim sNewFilePath As String

Set FSO = CreateObject("Scripting.FileSystemObject")
s(0) = ThisWorkbook.FullName

If FSO.FileExists(s(0)) Then
    '//Change Excel Extension to PDF extension in FilePath
    s(1) = FSO.GetExtensionName(s(0))
    If s(1) <> "" Then
        s(1) = "." & s(1)
        sNewFilePath = Replace(s(0), s(1), ".pdf")

        '//Export to PDF with new File Path
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF,_
        _ Filename:=sNewFilePath, Quality:=xlQualityStandard,_
        _ IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    End If
Else
    '//Error: file path not found
    MsgBox "Error: this workbook may be unsaved.  Please save and try again."
End If

Set FSO = Nothing

由于代码必须以递归方式运行,我想在文件名中添加包含在工作表中给定单元格(B2)中的周数。

我试过替换

s(0) = ThisWorkbook.FullName & Cells(2,2)

但它不起作用。错误在哪里?

3 个答案:

答案 0 :(得分:1)

FullName属性返回完整路径&amp;文件名和延期。将Cells(2,2)附加到"c:\path\to\filename.xlsx" & Cells(2,2).Value将为您提供类似Cells(2,2)的值。

您需要在文件扩展名部分之前插入周编号(sNewFilePath = Replace(s(0), s(1), Cells(2,2).Value & ".pdf")

你可以这样做:

Dim fullName As String, weekNum As String
Dim sNewFilePath As String

weekNum = Cells(2,2).Value
fullName = ThisWorkbook.FullName

'If the file exists, the `Dir` function will return the filename, len != 0
If Len(Dir(fullName)) <> 0 Then
    'remove the extension using Mid/InstrRev functions, _
     build the new filename with weeknumber & pdf extension
     sNewFilePath = Mid(fullName, 1, InstrRev(fullName,".")-1) & weekNum & ".pdf" 
    'Export to PDF with new File Path
     ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF,_
        _ Filename:=sNewFilePath, Quality:=xlQualityStandard,_
        _ IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    End If
Else
    '//Error: file path not found
    MsgBox "Error: this workbook may be unsaved.  Please save and try again."
End If

或者,不使用FileSystemObject:

import matplotlib.pyplot

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-64-6f467123fe04> in <module>()
----> 1 import matplotlib.pyplot

ImportError: No module named pyplot
---------------------------------------------------------------------------

答案 1 :(得分:0)

FullName包含文件扩展名。也许这个(你最好还要给B2添加一个工作表参考)。

s(0)=split(ThisWorkbook.FullName, ".")(0) & Cells(2, 2) & ".pdf"

答案 2 :(得分:0)

像这样的东西会这样做(我把它清理了一下):

Dim FSO As Object
Dim s(1) As String
Dim sNewFilePath As String
Sub SavePDF()

Set FSO = CreateObject("Scripting.FileSystemObject")
s(0) = ThisWorkbook.FullName

If FSO.FileExists(s(0)) Then
    '//Change Excel Extension to PDF extension in FilePath
    s(1) = FSO.GetExtensionName(s(0))
    If s(1) <> "" Then
        s(1) = "." & s(1)
        sNewFilePath = Left(s(0), InStrRev(s(0), "\")) & ".pdf"

        '//Export to PDF with new File Path
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        sNewFilePath & Sheets("wsTakeOff").Range("AY2").Value & " - " & Sheets("wsTakeOff").Range("D1") & ".pdf", Quality:= _
        xlQualityStandard, includedocproperties:=False, ignoreprintareas:=False, _
        openafterpublish:=False
    End If
Else
    '//Error: file path not found
    MsgBox "Error: this workbook may be unsaved.  Please save and try again."
End If

Set FSO = Nothing

End Sub