在桌面上创建文件作为位置时,Excel VBA /另存为PDF文件名和日期 [英] Excel VBA / Save As PDF with file name and date when file created on desktop as location

查看:107
本文介绍了在桌面上创建文件作为位置时,Excel VBA /另存为PDF文件名和日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我是VBA的新手,需要帮助如何继续创建一个在桌面上保存为PDF的命令按钮。

I am new to VBA and need help how to proceed with creating a command button that saves as PDF on desktop.

我有一张带有1张纸的XLS文件。我需要将其保存为具有相同文件名的PDF以及创建日期作为后缀。 

I have an XLS file with 1 sheet. I need to save it as PDF with same file name together with date of creation as suffix. 

位置路径应该是通用的,因为超过1个用户将工作表保存为PDF,每个用户将位于不同的域中。

Location path should be generic as more than 1 user will save sheet as PDF, and each user will be on different domains.

示例: 

文件名为TEST.xls

File name is TEST.xls

命令按钮应将.xls文件转换为TEST DD.MM.YYYY.pdf,位置路径应为桌面,能够在所有域上运行。 

Command button should convert .xls file to TEST DD.MM.YYYY.pdf and location path should be desktop, able to work on all domains. 

请帮忙吗?

推荐答案

确保开发人员标签页功能区显示。

Make sure that the Developer tab of the ribbon is displayed.

按Alt + F11激活Visual Basic编辑器。

Press Alt+F11 to activate the Visual Basic Editor.

选择插入>模块。

Select Insert > Module.

将以下宏复制到模块中:

Copy the following macro into the module:

Sub SaveAsPDF()
    Dim strDesktop As String
    strDesktop = CreateObject("WScript.Shell").SpecialFolders("Desktop")
    ThisWorkbook.ExportAsFixedFormat Type:=xlTypePDF, _
        Filename:=strDesktop & "\TEST " & Format(Date, "dd.mm.yyyy") & ".pdf"
End Sub

按Alt + F11返回Excel。

Press Alt+F11 to return to Excel.

激活功能区的"开发人员"选项卡。

Activate the Developer tab of the ribbon.

在"控件"组中,单击"插入">按钮(表单控件)。

In the Controls group, click Insert > Button (Form Control).

单击您想要按钮的工作表。

Click in the sheet where you want the button.

Excel将显示"指定宏"对话框。

Excel will display the Assign Macro dialog.

选择SaveAsPDF并单击OK。

Select SaveAsPDF and click OK.

您可以编辑按钮的标题。

You can edit the caption of the button.

点击按钮外部取消选择它。

Click outside the button to deselect it.


这篇关于在桌面上创建文件作为位置时,Excel VBA /另存为PDF文件名和日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆