Word vba saveas pdf错误的扩展名

时间:2019-10-02 10:54:29

标签: excel vba ms-word

enter image description here

尝试保存为pdf

Sub fill2()

            TemplateName = ThisWorkbook.path & "\DOC\test.docx"
            PdfPath = ThisWorkbook.path & "\OutPut"
            sSaveFolder = PdfPath & "\" & Format(Now(), "yyyy.mm.dd" & ".pdf")

        Set WA = CreateObject("Word.Application")
        Set WD = WA.Documents.Add(TemplateName)
            WA.Visible = False

            WD.SaveAs sSaveFolder, 17
            WD.Close False: Set WD = Nothing
        WA.Quit False: Set WA = Nothing
End Sub

但是文件保存的扩展名错误(将d字母更改为2个数字)

怎么了?

enter image description here

2 个答案:

答案 0 :(得分:4)

您要让Excel像这样格式化文件名:

Format(Now(), "yyyy.mm.dd" & ".pdf")

与Excel完全相同:

Format(Now(), "yyyy.mm.dd.pdf")

由于.pdf包含一个d,因此将其替换为日期数字(2)。

Format(Now(), "yyyy.mm.dd.pdf")
                       ^^  ^

将格式元素重新排列为yyyy.mm.dd,然后分别添加文件扩展名:

sSaveFolder = PdfPath & "\" & Format(Now(), "yyyy.mm.dd") & ".pdf"

答案 1 :(得分:2)

您在错误的位置加上了括号。试试这个:

Format(Now(), "yyyy.mm.dd") & ".pdf"