我正在处理一组html文件(用一些python代码保存在本地驱动器上),并在这些文件中查找关键字。但是,它们有几页长,我正在努力寻找一种方法,用vba自动执行以下序列:打开文件>查找关键字1>;以关键字1为中心拍摄一张屏幕截图,以x宽y高保存在最佳格式(jpeg?)在本地驱动器上>;转到下一个关键字>;转到下一个文件。在
最终目标是能够在上下文中快速查看这些关键字。如果我设法得到这些文件,我会在我的Excel电子表格中用超链接链接到它们。在
这是我现在的代码-显然它不起作用:
'Get list of files in folder
Dim xRow As Long
Dim xDirect$, xFname$, InitialFoldr$
InitialFoldr$ = "blablabla"
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = Application.DefaultFilePath & "\"
.Title = "Please select a folder to list Files from"
.InitialFileName = InitialFoldr$
If .SelectedItems.Count <> 0 Then
xDirect$ = .SelectedItems(1) & "\"
xFname$ = Dir(xDirect$, 7)
Do While xFname$ <> ""
Sheets("List of files folder").Cells(3, 2).Offset(xRow) = xFname$
xRow = xRow + 1
xFname$ = Dir
Loop
End If
End With
'Get # rows in list of files in folder
Dim myrng4 As Range
Dim lastlinelist As Integer
Dim htmlpath As String
Dim objWord
Dim objDoc
Set objWord = CreateObject("Word.Application")
Set myrng4 = Sheets("List of files folder").Range("B3:B50000")
lastlinelist = myrng4.Find(What:="*", LookIn:=xlValues, Lookat:=xlWhole, SearchOrder:=xlRows, _
SearchDirection:=xlPrevious).Row
For Each cn In Range(wb.Sheets("Results conso").Cells(3, 11), wb.Sheets("Results conso").Cells(3, Lastcolumn))
For Each fileref In Range(Sheets("List of files folder").Cells(2, 3), Sheets("List of files folder").Cells(2, lastlinelist))
With Sheets("results conso")
htmlpath = InitialFoldr$ & fileref
If Dir(htmlpath) = "" Then
Else
If LCase(Right(pdfpath, 4)) <> "html" Then
Else
Set objDoc = objWord.Documents.Open(htmlpath)
objWord.Visible = True
objDoc.BringToFront
If objDoc.findText(cn.Value, True, True, False) = False Then
objDoc.Close True
Set objDoc = Nothing
Else
Call keybd_event(VK_SNAPSHOT, 0, 0, 0)
Set objDoc = wordobj.Documents.Add
wordobj.Visible = True
Set objselection = wordobj.Selection
objselection.Paste
End If
End If
End If
End With
Next fileref
Next cn
另外,我想知道用Python而不是VBA做这件事是否更好。在
非常感谢, 哈德良
找到这个词应该很容易。我写了一个函数来捕捉截图。它可能比你需要的代码更多。它正在查找程序和窗口等
这是我模块modWindowsScreenShot中的代码
^{pr2}$相关问题 更多 >
编程相关推荐