我有一个文件夹,其中包含我们基金经理的所有PDF文件。月结单。我正在尝试创建一个循环遍历所有的子程序,将每个pdf报告转换为excel doc中的工作表。问题是,每个向我们发送声明的公司的命名惯例是非常不同的,所以我试图想出一种优雅的方法来命名每个工作表。下面是打开新工作簿的第一个子代码,设置路径并调用实际导入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数组,使用它的给定路径打开每个文件,然后使用以下子项将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:\等等#34;),我查了一下,看起来真的很复杂,或者(2)将另一个字符串变量作为附加参数传递给第二个子程序(即调用Imp_Into_XL(element,tabName),但是我' ;我不确定这会如何影响我最初通过数组循环打开文件的想法。我觉得如果我知道很多(或者其他什么,真的)关于建立一个新课程,这可能会有所帮助,但我&#39在那种技术上我很茫然。
这是一个非常具体的问题,并且很难描述,所以我真的很感激有关如何解决它的任何见解,或者如果你有任何想法,我没有想到这个会完成同样的事情,我全都耳朵。
答案 0 :(得分:0)
如果您从需要标准化的客户那里获得可变数据,则有IMO 3方法可以处理它:
1)执行一些规则。使您的客户端提供具有指定名称格式的文档。你控制他们的数据,他们知道,有一些谈判的空间,这样你的代码可以相当统一。
2)你硬编码一堆If的声明&其他逻辑 - 知道您收到的文件名 - 将文件名更改为您的代码可接受的内容,然后使用这些规则和文件名运行您的sub。
3)你对企业的疯狂感到不安,并且花费你余下的时间进行无聊的编码,以满足下一个新客户的命名惯例要求:)
我选择第一选。