所以,我要做的是获取一个pdf文件,用Internet Explorer打开它,从中复制文本,将该文本粘贴到记事本中的文本文件中并保存该文件与pdf同名它是由...创建的。到目前为止,我完成了所有这一切,除了保存文本文件,一旦我有了我需要的数据。我发现自己现在陷入困境,任何想法都会有所帮助。
这是我到目前为止的代码:
Set b2 = ThisWorkbook
Set ie = CreateObject("InternetExplorer.Application")
strPath = "C:\Users\353281\Desktop\test 134.pdf"
ie.Visible = True
ie.navigate (strPath)
Do While ie.Busy And ie.ReadyState <> 4
DoEvents
Loop
Application.Wait Now + TimeSerial(0, 0, 2)
AppActivate strPath
SendKeys "^(a)", True
Application.Wait Now + TimeSerial(0, 0, 1)
Do
On Error Resume Next
SendKeys "^(c)", True
Loop While Err.Number <> 0
np = Shell("Notepad.exe", vbNormalFocus)
AppActivate np
SendKeys "^(v)", True
'tried the SendKeys method of saving my text file here, not to much avail
SendKeys "^(s)", True
'this bit here finds the file name of strPath
a = InStrRev(strPath, "\")
b = InStrRev(strPath, ".")
c = Mid(strPath, a + 1, b - a - 1)
'haven't got anything after this point but a few failed attempts
(我为代码混乱而道歉,我不会专注于结构,直到我有一些共同的东西起作用)
答案 0 :(得分:0)
我会做这样的事情:
Dim NP as object
set NP = CreateObject("Notepad.Application")
'copy the text from the .PDF as you do now
NP.paste
NP.SaveAs Filename:=<your file name>
注意:这是完全未经测试的,但应作为框架。我没有去查看Notepad对象模型以确认我的方法名称是正确的。
答案 1 :(得分:0)
所以,最终为我工作的是代码如下:
Dim b2 As Workbook
Dim ie As Object
Dim np As Object
Sub test4()
Set b2 = ThisWorkbook
Set ie = CreateObject("InternetExplorer.Application")
strPath = "C:\Users\353281\Desktop\test 134.pdf"
ie.Visible = False
ie.navigate (strPath)
Do While ie.Busy And ie.ReadyState <> 4
DoEvents
Loop
Application.Wait Now + TimeSerial(0, 0, 2)
AppActivate strPath
SendKeys "^(a)", True
Application.Wait Now + TimeSerial(0, 0, 1)
Do
On Error Resume Next
SendKeys "^(c)", True
Loop While Err.Number <> 0
ie.Quit
Set np = CreateObject("Word.Application")
AppActivate np
np.documents.Add
np.Visible = False
np.Selection.pasteandformat wdpastedefault
b = InStrRev(strPath, ".")
filenm = Mid(strPath, 1, b - 1)
np.activedocument.SaveAs2 Filename:=filenm, FileFormat:=2
np.activedocument.Close
np.Quit
np = Nothing
ie = Nothing
SendKeys "{NUMLock}", True
End Sub
我没有使用记事本来创建文本文档,而是在使用Word方面取得了更大的成功。此代码将获取pdf,在IE中打开它,选择所有文本,复制到剪贴板,然后将其粘贴到word文档,然后将其作为文本文件保存在与原始pdf相同的文件夹中。