【发布时间】:2017-03-21 14:45:47
【问题描述】:
我有一个文件夹,里面有我们基金经理月结单的所有 PDF 文件。我正在尝试创建一个循环遍历它们的子程序,将每个 pdf 报告转换为 excel 文档中的工作表。问题是,每家向我们发送声明的公司的命名约定都非常不同,所以我试图想出一种优雅的方式来命名每个工作表。这是打开新工作簿的第一个子程序的代码,设置路径并调用实际导入 pdf 的另一个子程序。
Sub newWkbk_callSub()
Dim PDF_File As String
Dim wb As Workbook
Set wb = Workbooks.Add
wb.SaveAs Filename:="H:\Performance Reports\SMU Quick Endowment Performance Summaries\2017\Supporting Docs\Monthly Manager Statements\Update wksht " + Format(Now(), "mm-dd-yyyy") + ".xlsx"
Dim rptName As Variant
Dim element As Variant
Dim asOf As Date
Dim path, sfx, Fund1, Fund2 As String
path = "H:\Performance Reports\SMU Quick Endowment Performance Summaries\2017\Supporting Docs\Monthly Manager Statements\"
sfx = ".pdf"
asOf = WorksheetFunction.EoMonth(Now(), -1)
fund1= path + Format(asOf, "yyyy-mm-dd") + " fund1" + sfx
'this is fund 1's naming convention: "2017-02-28 Fund1"
fund2 = "something similar to fund1"
rptName = Array(fund1, fund2)
'loop through the report names/paths in the array
For Each element in rptName
Call Imp_Into_XL(element)
Next
End Sub
理想情况下,这将遍历 rptName 数组,使用给定的路径打开每个文件,然后使用以下 sub 将 pdf 文本放入新的工作表中:
Sub ImportPDF(PDF_File As String)
Dim PDFfile As Acrobat.AcroPDDoc 'access pdf file
Dim wordCount As Acrobat.AcroHiliteList 'set selection word count
Dim PDFpage As Acrobat.AcroPDPage 'get the particular page
Dim PDFtext As Acrobat.AcroPDTextSelect 'get the text of selection area
Dim wb As Workbook
Dim ws As Worksheet
Dim tabName As String
Application.ScreenUpdating = False
Set PDFfile = New Acrobat.AcroPDDoc
Set wordCount = New Acrobat.AcroHiliteList
With PDFfile
.Open (PDF_File) 'open PDF file
'add workbook sheet
Set ws = Worksheets.Add(, Worksheets(Sheets.Count))
tabName = (PDF_File)
Debug.Print (tabName) ' Can't just name the tab the PDF_file, because it's too long - "H:\areallylongstringofdirectories"
tabName = Right(tabName, Len(tabName) - 113)
tabName = Left(tabName, Len(tabName) - 4)
Debug.Print (tabName) ' I thought about trying to shorten it, but then I run into the problem where the naming convention for each firm's report is different, and the name will be different for a firm reporting "02-2017 Fund1" vs. "Fund2 February 2017"
ws.Name = tabName
'and really the code for doing the pdf import is not relevant to my question, this is where I'm trying to get the naming convention right
End Sub
我的两个想法是(1)我可以在调用字符串的变量名称之后命名选项卡(即,fund1 是变量名称,即使它存储字符串“H:\etc”。 ),我查了一下,它看起来真的很复杂,或者 (2) 将另一个字符串变量作为附加参数传递给第二个子例程(即调用 Imp_Into_XL(element, tabName),但我不确定它是如何工作的进入我最初循环数组以打开文件的想法。我觉得如果我对设置一个新类了解很多(或任何东西,真的),这可能会有所帮助,但我对这种技术一无所知。
这是一个非常具体的问题,而且有点难以描述,所以我非常感谢任何关于如何解决它的见解,或者如果你有任何我没有想到的想法可以完成同样的事情,我全是耳朵。
【问题讨论】:
-
我投票决定将此问题作为题外话结束,因为这似乎是一个设计问题,而不是编码问题。
-
请注意这是不正确的变量声明:
Dim path, sfx, Fund1, Fund2 As String。只有Fund2明确是String类型。其余的是Variant。 -
选项 1(基于 变量名 命名它在 VBA 中基本上是不可能的,因为它不支持任何类型的内省)。不要使用路径数组,而是使用字典,其中 keys=paths$ 和 values=tabName$?否则,我会同意共产国际,这更像是一个设计问题,而不是特定代码问题。
-
再一次,如果你要在变量之后命名它(例如,“fund1”),变量名不会改变,所以为什么不直接硬编码一个常量字符串并使用总是作为标签名称?
-
数组看起来像(fund1,fund2等),因为元素变量循环通过,它被分配给这些变量名中的每一个,这些变量名又被分配给一些路径。所以你是对的,它可能行不通。
标签: arrays excel vba pdf worksheet