如何使用vba宏在循环中打开pdf文件

问题描述 投票:0回答:1

我在lbound和uboud上的错误超出了范围。请帮忙。这是我的代码。它在我的其他宏上工作。错误从ubound和lbound开始。当我运行它时,它导致下标错误。我只是复制我过去的代码。

Option Explicit
Public pdfpath, excelpath As String
Public pdfname, fname, issuename, excelname As String
Public wb As Workbook
Public ws, wspdf As Worksheet
Public STRinvoice, STRamnt, STRissue, STRcus As String
Public SAPinvoice, SAPamnt, SAPissue, SAPcus As String
Public i, ctr As Long
Public rfile() As Variant

Sub DO_ALL()

pdfpath = "C:\Users\" & Environ$("Username") & "\Desktop\Wiley Checker\PDF IN\"
excelpath = "C:\Users\" & Environ$("Username") & "\Desktop\Wiley Checker\EXCEL IN\"
pdfname = Dir(pdfpath & "*.pdf*")
ctr = 1


Set wb = ActiveWorkbook
Set ws = wb.Sheets("Checker")
Set wspdf = wb.Sheets("PDF")


Do Until pdfname = ""
    If InStr(pdfname, "pdf") > 0 Then
        ReDim Preserve rfile(1 To ctr)
        rfile(ctr) = pdfname
        ctr = ctr + 1
    End If
pdfname = Dir
Loop

For i = LBound(rfile) To UBound(rfile)

ActiveWorkbook.FollowHyperlink pdfpath & rfile(i)
'continous codes.....
Next i

Exit Sub

End Sub

我只是想循环打开pdf文件。

excel vba subscript
1个回答
1
投票

很可能Array rfile未初始化。请检查您是否有pdfpath中的文件,并且至少有一个文件通过InStr(pdfname,“pdf”)检查。

为安全起见,请使用:

If ctr > 1 Then
    For i = LBound(rfile) To UBound(rfile)

    ActiveWorkbook.FollowHyperlink pdfpath & rfile(i)
    'continous codes.....
    Next i
End If
© www.soinside.com 2019 - 2024. All rights reserved.