简体   繁体   English

Excel 2010 VBA:使用单元格中的值保存文件以确定路径和文件名

[英]Excel 2010 VBA: Save file using value from cell to determine path and filename

I am trying to write some code that will save several tabs as a pdf document in folder specified by files within excell. 我正在尝试编写一些代码,将几个选项卡保存为在excell中的文件指定的文件夹中的pdf文档。 I would like for cells within the document to dictate where this file is saved. 我希望文档中的单元格指示保存此文件的位置。 I am not sure if this is possibly, but if it is any help would be good! 我不确定这是否可能,但如果有任何帮助会很好! I am currently getting a Run-time error '1004' during the save process of my code. 我目前在代码的保存过程中收到运行时错误“1004”。

And yes, I do have the folders created that are being referenced. 是的,我确实创建了正在引用的文件夹。

Sub asdf()

Dim Fname As String
Dim Fpath As String
Dim YrMth As String

Fname = Sheets("Sheet1").Range("A1").Text

YrMth = Sheets("Sheet1").Range("A2").Text & "\" & Sheets("Sheet1").Range("A3").Text

Fpath = "C:\Documents and Settings\My Documents\" & YrMth & "\Group\" & Fname & ".pdf"

ThisWorkbook.Sheets(Array("Sheet1", "Sheet2", "Sheet4")).Select

Application.DisplayAlerts = False

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
        Filename:=Fpath, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=True

End Sub 结束子

Your code works for me, but not with the path you've specified. 您的代码适用于我,但不适用于您指定的路径。

Declare a new string variable: 声明一个新的字符串变量:

dim myDocsPath as String

Get the path using: 获取路径使用:

myDocsPath = Environ$("USERPROFILE") & "\My Documents\"

and then change your definition for Fpath to: 然后将Fpath的定义更改为:

Fpath = myDocsPath & YrMth & "\Group\" & Fname & ".pdf"

If I change the end of myDocsPath to & "\\My foo Documents\\" I get the same 1004 error you are getting. 如果我将myDocsPath的结尾myDocsPath& "\\My foo Documents\\"我会得到相同的1004错误。

Try replace line in your code 尝试在代码中替换行

Fpath = "C:\Documents and Settings\My Documents\" & YrMth & "\Group\" & Fname & ".pdf"

with

Dim WshShell As Object
Dim MyDocsFolder As String
Set WshShell = CreateObject("WScript.Shell")
MyDocsFolder = WshShell.SpecialFolders("MyDocuments") & "\"

Fpath = MyDocsFolder & YrMth & "\Group\" & Fname & ".pdf"

Edit: The core of this solution is in line: 编辑:此解决方案的核心是:

MyDocsFolder = WshShell.SpecialFolders("MyDocuments") & "\"

which returns system path to My Documents, irrespectively from local system settings like language or nonstandard location of My Documents folders. 它返回“我的文档”的系统路径,与本地系统设置(如“我的文档”文件夹的语言或非标准位置)无关。 Then it adds a backslash at the end. 然后它在最后添加一个反斜杠。

It is more elegant (and the code becomes more portable) if you ask system about special folders than hardcode such data in your script. 如果您向系统询问特殊文件夹而不是在脚本中对此类数据进行硬编码,则它更优雅(并且代码变得更加便携)。

More on Windows special folders in VBA you can find https://www.rondebruin.nl/win/s3/win027.htm 有关VBA中Windows特殊文件夹的更多信息, 请访问https://www.rondebruin.nl/win/s3/win027.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM