自动记录包含相关信息的PDF文件的过程

时间:2016-07-25 19:03:14

标签: excel vba excel-vba

我正在创建一个具有userform的数据库,以帮助自动化记录包含相关信息的PDF文件的过程。

为此,我想让用户输入图纸信息,浏览图纸所在的文件夹,然后在Excel中列出图纸文件,并为每个图纸/ PDF条目重复信息。

我该如何编码呢?

这就是我现在所拥有的(并且它不起作用):

Sub Button2_Click()
    UserForm1.Show
End Sub

Sub startIt()
    Dim FileSystem As Object
    Dim HostFolder As String
    Dim xDirect$, xFname$, InitialFoldr$

    InitialFoldr$ = "G:\ScannedDwgArchive\Transportation and BuildingsDrawings\Rocky Harbour\"
    With Application.FileDialog(msoFileDialogFolderPicker)
        .InitialFileName = Application.DefaultFilePath & "\"
        .Title = "Please select a folder to list Files from"
        .InitialFileName = InitialFoldr$
        .Show
        If .SelectedItems.Count <> 0 Then
            xDirect$ = .SelectedItems(1) & "\"
        End If
    End With

    Set FileSystem = CreateObject("Scripting.FileSystemObject")

    DoFolder FileSystem.GetFolder(InitialFoldr$)
End Sub

Sub DoFolder(Folder)
    Dim SubFolder
    For Each SubFolder In Folder.SubFolders
        DoFolder SubFolder
    Next

    i = Cells(Rows.Count, 21).End(xlUp).Row + 1
    Dim File
    For Each File In Folder.Files
        ActiveSheet.Hyperlinks.Add Anchor:=Cells(i, 21), Address:= _
        File.Path, TextToDisplay:=File.Name
        i = i + 1
        Call TextboxWrite
    Next
End Sub

**Sheet 5 is the page in my workbook I am working on. This part of the code never works**
Sub TextboxWrite()
    Sheet5.Range(i, 12) = jobNumTextBox.value
    Sheet5.Range(i, 15) = TownTextBox.value
    Sheet5.Range(i, 16) = YearTextBox.value
    Sheet5.Range(i, 18) = StreetTextBox.value
    Sheet5.Range(i, 19) = PhaseTextBox.value
    Sheet5.Range(i, 20) = cdTextBox.value
End Sub

我可以打开文件夹,并将文件写入Excel。这就是它。我怎样才能使这个工作?

更新:我在你的一些指导和一些新发现的教程之后开始工作了。这是(没有完成,但它的功能)

Sub Button2_Click()
UserForm1.Show
End Sub

Sub startIt()

  Dim FileSystem As Object
  Dim HostFolder As String
Dim xDirect$, xFname$, InitialFoldr$

  InitialFoldr$ = "G:\ScannedDwgArchive\Transportation and Buildings Drawings\Rocky Harbour\"
With Application.FileDialog(msoFileDialogFolderPicker)
    .InitialFileName = Application.DefaultFilePath & "\"
    .Title = "Please select a folder to list Files from"
    .InitialFileName = InitialFoldr$
    .Show
        If .SelectedItems.Count <> 0 Then
        xDirect$ = .SelectedItems(1) & "\"
        End If
    End With

  Set FileSystem = CreateObject("Scripting.FileSystemObject")

  DoFolder FileSystem.GetFolder(InitialFoldr$)

End Sub

Sub DoFolder(Folder)
    Dim SubFolder
    For Each SubFolder In Folder.SubFolders
        DoFolder SubFolder
    Next

    Dim i As Integer                             ' DECLARE i VARIABLE
    i = Cells(Rows.Count, 21).End(xlUp).row + 1
    Dim File
    For Each File In Folder.Files
        ActiveSheet.Hyperlinks.Add Anchor:=Cells(i, 21), Address:= _
        File.Path, TextToDisplay:=File.Name
        i = i + 1
        Call TextboxWrite(i)                     ' PASS ARGUMENT

    Next
End Sub

Sub TextboxWrite(i As Integer)                   ' RECEIVE ARGUMENT
    Worksheets("CIVIL DATABASE").Activate
    Dim row As Integer
    row = i - 1
    ActiveSheet.Cells(row, 12) = UserForm1.jobNumTextBox.value
    ActiveSheet.Cells(row, 15) = UserForm1.TownTextBox.value
    ActiveSheet.Cells(row, 16) = UserForm1.YearTextBox.value
    ActiveSheet.Cells(row, 17) = UserForm1.descTextBox.value
    ActiveSheet.Cells(row, 18) = UserForm1.StreetTextBox.value
    ActiveSheet.Cells(row, 19) = UserForm1.PhaseTextBox.value
    ActiveSheet.Cells(row, 20) = UserForm1.cdTextBox.value

End Sub

非常感谢你帮助我!

1 个答案:

答案 0 :(得分:0)

您需要将i值传递给被调用的子例程TextboxWrite()。现在,i在其范围内没有任何价值,因为它不是全局变量而是本地变量:

...
Sub DoFolder(Folder)
    Dim SubFolder
    For Each SubFolder In Folder.SubFolders
        DoFolder SubFolder
    Next

    Dim i As Integer                             ' DECLARE i VARIABLE
    i = Cells(Rows.Count, 21).End(xlUp).Row + 1
    Dim File
    For Each File In Folder.Files
        ActiveSheet.Hyperlinks.Add Anchor:=Cells(i, 21), Address:= _
        File.Path, TextToDisplay:=File.Name
        i = i + 1
        Call TextboxWrite(i)                     ' PASS ARGUMENT
    Next
End Sub

Sub TextboxWrite(i As Integer)                   ' RECEIVE ARGUMENT
    Sheet5.Range(i, 12) = jobNumTextBox.value
    Sheet5.Range(i, 15) = TownTextBox.value
    Sheet5.Range(i, 16) = YearTextBox.value
    Sheet5.Range(i, 18) = StreetTextBox.value
    Sheet5.Range(i, 19) = PhaseTextBox.value
    Sheet5.Range(i, 20) = cdTextBox.value
End Sub

此外,对于VBA中的最佳做法,请在任何子例程或函数之外的顶行使用Option Explicit。这会强制您在宏执行之前声明编译代码(Debug \ Compile)时使用的每个变量,这有望成为常规做法。此外,通过On Error处理预测运行时错误。如果您合并Err.NumberErr.Description,您将收到有关此问题的更多详细信息。