正如您所建议的,我做了一些更改,但仍然无法使其变得用户友好,或者您可以说每次我必须使用此代码时,我都必须在不同的计算机上打开脚本运行时。
请尽快提出建议并修正我的代码
Sub GetFileNamesandPageCount()
Option Explicit
Public fs As Object
Dim diaFolder As FileDialog
Set fs = CreateObject("Scripting.FileSystemObject")
Dim i As Integer
Dim fld As Object
Dim T_Str As String
Set diaFolder = Application.FileDialog(msoFileDialogFolderPicker)
diaFolder.AllowMultiSelect = False
diaFolder.Show
If diaFolder.Show = -1 Then
T_Str = diaFolder.SelectedItems(1)
Else
Set diaFolder = Nothing
End If
Set fld = fs.getfolder(T_Str)
Dim wdOBJ As Word.Application
Dim wdDoc As Object
Dim fd As Object
Sheet1.UsedRange.Clear
Sheet1.Range("A1") = "Document Name"
Sheet1.Range("B1") = "Page Count"
Sheet1.Range("A1:B1").Font.Bold = True
Sheet1.Columns("A:A").ColumnWidth = 70
Sheet1.Columns("B:B").AutoFit
Sheet1.Range("A1:B1").Interior.ColorIndex = 37
fs.GetSpecialFolder (1)
Set wdOBJ = CreateObject("Word.Application")
wdOBJ.Visible = True
i = 1
For Each fd In fld.Files
If (InStr(1, fd, ".doc") > 0) Or (InStr(1, fd, ".docx") > 0) Or (InStr(1, fd, ".docm") > 0) Or (InStr(1, fd, ".doct") > 0) Or (InStr(1, fd, ".pdf") > 0) Then
Sheet1.Range("A" & i + 1) = fd.Name
Set wdDoc = wdOBJ.Documents.Open(CStr(fd))
wdDoc.ComputeStatistics (wdStatisticPages)
Sheet1.Range("B" & i + 1) = wdDoc.BuiltinDocumentProperties(14)
wdDoc.Close False
End If
i = i + 1
Next fd
wdOBJ.Quit False
End Sub
答案1
当您将 wdOBJ 实例化为后期绑定时,您应该将其尺寸设置为对象,而不是 Word.Application。之后您应该能够删除该引用。