下面的代码是一个流程的一部分。该流程需要用户执行两个操作,即操作1和操作3。操作2中的所有操作都是自动完成的。操作3中的所有操作也都是自动完成的,除了CommandButton。
操作1) 允许用户选择PDF文件
操作2) 然后在Acrobat Reader中打开PDF,从文件名中删除错误字符并重命名,复制新的文件路径,用于将条目与原始PDF进行超链接,将PDF数据复制到隐藏工作表中,然后另一个隐藏工作表使用Offset(Index(VLookUp(按照这个顺序的公式从粘贴PDF数据的工作表中提取我的信息
操作3) 然后一个UserForm允许用户在将数据添加到文档之前查看数据,然后通过一个CommandButton将数据添加到文档,将文档名称与原始文件进行超链接,并允许用户重复该过程或关闭UserForm。
操作1) 允许用户选择PDF文件
操作2) 然后在Acrobat Reader中打开PDF,从文件名中删除错误字符并重命名,复制新的文件路径,用于将条目与原始PDF进行超链接,将PDF数据复制到隐藏工作表中,然后另一个隐藏工作表使用Offset(Index(VLookUp(按照这个顺序的公式从粘贴PDF数据的工作表中提取我的信息
操作3) 然后一个UserForm允许用户在将数据添加到文档之前查看数据,然后通过一个CommandButton将数据添加到文档,将文档名称与原始文件进行超链接,并允许用户重复该过程或关闭UserForm。
Sub GetData()
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker) 'Create a FileDialog object as a File Picker dialog box
Dim vrtSelectedItem As Variant
Application.ScreenUpdating = False 'speed up macro execution
Application.DisplayAlerts = False ‘Disables error messages
'Sub OPENFILE()
With fd
'Use a With...End With block to reference the FileDialog object.
'Use the Show method to display the File Picker dialog box and return the user's action.
'The user pressed the action button.
'On Error GoTo ErrMsg
If .Show = -1 Then
For Each vrtSelectedItem In .SelectedItems
rc = ShellExecute(0, "open", vrtSelectedItem, vbNullChar, _
vbNullChar, 0)
Application.CutCopyMode = True
'Wait some time
Application.Wait Now + TimeValue("00:00:03") ' wait 3 seconds
DoEvents
'IN ACROBAT :
'SELECT ALL
DoEvents
SendKeys "^a"
'COPY
DoEvents
SendKeys "^c"
'EXIT (Close & Exit)
Application.Wait Now + TimeValue("00:00:02") ' wait 3 seconds
DoEvents
SendKeys "^q"
'Wait some time
Application.Wait Now + TimeValue("00:00:06") ' wait 3 seconds
'Paste
DoEvents
Sheets("Raw WAM Data").Paste Destination:=Sheets("Raw WAM Data").Range("A1")
Sheet8.Range("a50").Value = vrtSelectedItem
Application.Wait Now + TimeValue("00:00:03") ' wait 3 seconds
'Replace bad characters in the file name and Rename the file
Dim FPath As String
Dim Ndx As Integer
Dim FName As String, strPath As String
Dim strFileName As String, strExt As String
Dim NewFileName As String
Const BadChars = "@!$/'<|>*-—" ' put your illegal characters here
If Right$(vrtSelectedItem, 1) <> "\" And Len(vrtSelectedItem) > 0 Then
FilenameFromPath = GetFilenameFromPath(Left$(vrtSelectedItem, Len(vrtSelectedItem) - 1)) + Right$(vrtSelectedItem, 1)
End If
FName = FilenameFromPath
For Ndx = 1 To Len(BadChars)
FName = Replace$(FName, Mid$(BadChars, Ndx, 1), "_")
Next Ndx
GivenLocation = _
SRV006\Am\Master Documents\PC 2.2.11 Document For Work(DFWs)\DFWS added to DFW Track\" 'note the trailing backslash
OldFileName = vrtSelectedItem
strExt = ".pdf"
NewFileName = GivenLocation & FName & strExt
Name vrtSelectedItem As NewFileName
'The next three lines are not used but can be if you do not want to rename the file
'FPath = vrtSelectedItem 'Fixing the File Path
'FPath = (Right(FPath, Len(FPath) - InStr(FPath, "#")))
'FPath = "\\" & FPath
'pastes new file name into cell to be used with the UserForm
Sheet8.Range("a50") = NewFileName
Next vrtSelectedItem
Else
End
End With
On Error GoTo ErrMsg:
ErrMsg:
If Err.Number = 1004 Then
MsgBox "You Cancelled the Operation" ‘The User pressed cancel
Exit Sub
End If
‘This delimits my data so I can use the Offset(Index(VLookUp formulas to locate the information on the RAW sheet
Sheet7.Activate
Sheet7.Range("A1:A1000").TextToColumns _
Destination:=Sheet7.Range("A1:A1000").Offset(0, 0), _
DataType:=xlDelimited, _
Tab:=False, _
Semicolon:=False, _
Comma:=False, _
Space:=False, _
OTHER:=True, _
OtherChar:=":"
‘Now the UserForm launches with the desired data already in the TextBoxes
With UserForm2
Dim h As String
h = Sheet8.Range("A50").Value ‘This is my Hyperlink to the file
UserForm2.Show
Set UserForm4 = UserForm2
On Error Resume Next
StartUpPosition = 0
.Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
.Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
UserForm4.TextBox1.Value = Sheet8.Range("A20")
UserForm4.TextBox2.Value = Sheet8.Range("A22")
UserForm4.TextBox3.Value = Sheet8.Range("A7")
UserForm4.TextBox5.Value = Sheet8.Range("A23")
UserForm4.TextBox6.Value = Sheet8.Range("A24")
UserForm4.TextBox7.Value = Sheet8.Range("A10")
UserForm4.TextBox10.Value = Date
UserForm4.TextBox12.Value = Sheet8.Range("A34")
UserForm4.TextBox13.Value = Sheet8.Range("A28")
UserForm4.TextBox14.Value = Sheet8.Range("A26")
UserForm4.TextBox17.Value = Sheet8.Range("A12")
UserForm4.TextBox19.Value = h
UserForm4.TextBox16.Value = Sheet8.Range("A18")
End With
Application.ScreenUpdating = True 'refreshes the screen
End Sub