Vba 在word文档中搜索文本并粘贴到excel文件中
我很确定我已经非常接近这一点了,我使用了和的组合来实现我迄今为止所得到的 我试图在word文件中找到某些值,最容易识别的前一个文本是上面一行的“值日期”。我想要的值在“生效日期”下面的行中。我希望宏能够搜索word文档中所需的文本并将其粘贴到excel中,因为通常我们必须手动执行大约50次。非常乏味 以下是单词doc中的文本,以供参考Vba 在word文档中搜索文本并粘贴到excel文件中,vba,excel,ms-word,Vba,Excel,Ms Word,我很确定我已经非常接近这一点了,我使用了和的组合来实现我迄今为止所得到的 我试图在word文件中找到某些值,最容易识别的前一个文本是上面一行的“值日期”。我想要的值在“生效日期”下面的行中。我希望宏能够搜索word文档中所需的文本并将其粘贴到excel中,因为通常我们必须手动执行大约50次。非常乏味 以下是单词doc中的文本,以供参考 TRANSACTIONS VALUE DATE 31-08-15
TRANSACTIONS VALUE DATE
31-08-15 X,XXX.XX
我想拉取值X,XXX.XX并将其粘贴到excel中的目标中,为了简单起见,我们只使用A1
Sub wordscraper9000()
Dim oWordApp As Object, oWordDoc As Object
Dim FlName As String
'''''dim tbl as object --> make string
Dim TextToFind As String, TheContent As String
Dim rng1 As Word.Range
FlName = Application.InputBox("Enter filepath of .doc with desired information")
'establish word app object
On Error Resume Next
Set oWordApp = GetObject(, "Word.application")
If Err.Number <> 0 Then
Set oWordApp = CreateObject("Word.application")
End If
Err.Clear
On Error GoTo 0
oWordApp.Visible = True
'open word doc
Set oWordDoc = oWordApp.documents.Open(FlName)
'--> enter something that will skip if file already open
'''''set tbl = oworddoc.tables(1) --> set word string
'declare excel objects
Dim wb As Workbook, ws As Worksheet
'Adding New Workbook
Set wb = Workbooks.Add
'Saving the Workbook
ActiveWorkbook.SaveAs "C:\Users\iansch\Desktop\DummyWB.xlsx"
Set ws = wb.Sheets(1)
'what text to look for
TextToFind = "VALUE DATE"
'''''problems here below
Set rng1 = oWordApp.ActiveDocument.Content
rng.Find.Execute findtext:=TextToFind, Forward:=True
If rng1.Find.found Then
If rng1.Information(wdwithintable) Then
TheContent = rng.Cells(1).Next.Range.Text 'moves right on row
End If
Else
MsgBox "Text '" & TextToFind & "' was not found!"
End If
'copy text range and paste into cell A1
'tbl.range.copy
ws.Range("A1").Activate
ws.Paste
End Sub
我得到一个运行时8002801d错误-自动化错误,库未注册
我在这里找不到任何适合我的问题,但是我链接到的第二个问题非常非常接近我想要的,但是我正在尝试导入文本而不是表格。这将把“X,XXX.XX”值提取到一个新的Excel文件中,第1页,单元格A1:
Option Explicit
Public Sub wordscraper9000()
Const FIND_TXT As String = "VALUE DATE"
Const OUTPUT As String = "\DummyWB.xlsx"
Dim fName As Variant, wrdApp As Object, wrdTxt As Variant, sz As Long, wb As Workbook
fName = Application.GetOpenFilename("Word Files (*.Doc*),*.Doc*", , _
"Enter filepath of .doc with desired information")
If fName <> False Then
'get Word text --------------------------------------------------------------------
On Error Resume Next
Set wrdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set wrdApp = CreateObject("Word.Application")
Err.Clear
End If: wrdApp.Visible = False
wrdTxt = wrdApp.Documents.Open(fName).Content.Text: wrdApp.Quit
'get value ------------------------------------------------------------------------
sz = InStr(1, wrdTxt, FIND_TXT, 1)
If Len(sz) > 0 Then
wrdTxt = Trim(Right(wrdTxt, Len(wrdTxt) - sz - Len(FIND_TXT)))
wrdTxt = Split(Trim(Right(wrdTxt, InStr(wrdTxt, " "))))(0)
'save to Excel ----------------------------------------------------------------
Set wb = Workbooks.Add
wb.Sheets(1).Cells(1, 1) = wrdTxt
Application.DisplayAlerts = False
wb.Close True, CreateObject("WScript.Shell").SpecialFolders("Desktop") & OUTPUT
Application.DisplayAlerts = True
End If
End If
End Sub
选项显式
公共Sub-wordscraper9000()
Const FIND_TXT As String=“VALUE DATE”
常量输出为字符串=“\DummyWB.xlsx”
Dim fName作为变量,wrdApp作为对象,wrdTxt作为变量,sz作为长,wb作为工作簿
fName=Application.GetOpenFilename(“Word文件(*.Doc*),*.Doc*”_
“输入带有所需信息的.doc文件路径”)
如果fName为False,则
'获取Word文本--------------------------------------------------------------------
出错时继续下一步
Set wrdApp=GetObject(,“Word.Application”)
如果错误号为0,则
Set wrdApp=CreateObject(“Word.Application”)
呃,明白了
如果:wrdApp.Visible=False,则结束
wrdTxt=wrdApp.Documents.Open(fName).Content.Text:wrdApp.Quit
“获取价值------------------------------------------------------------------------
sz=InStr(1,wrdTxt,FIND_TXT,1)
如果Len(sz)>0,则
wrdTxt=修剪(右(wrdTxt,Len(wrdTxt)-sz-Len(FIND_TXT)))
wrdTxt=拆分(修剪(右(wrdTxt,仪表(wrdTxt,“”)))(0)
'保存到Excel----------------------------------------------------------------
设置wb=工作簿。添加
wb.表格(1).单元格(1,1)=wrdTxt
Application.DisplayAlerts=False
wb.Close True、CreateObject(“WScript.Shell”)、SpecialFolders(“桌面”)和输出
Application.DisplayAlerts=True
如果结束
如果结束
端接头
此代码特定于此模式:
“Reference”(任意空格)(任何没有空格的单词)(任意空格)“ExtractValue”
- 搜索引用(查找TXT)
- 在任意数量的空格或空行后查找并跳过下一个单词(文本中没有空格)
- 从跳过的第一个单词中提取第二个单词,以任意数量的空格或行分隔
Sub wordscraper90000()
Dim oWordApp As Object, oWordDoc As Object
Dim FlName As String
Dim TheContent As String
FlName = Application.GetOpenFilename("Word Files (*.Doc*),*.Doc*", , _
"Enter filepath of .doc with desired information")
'establish word app object
On Error Resume Next
Set oWordApp = GetObject(, "Word.application")
If Err.Number <> 0 Then
Set oWordApp = CreateObject("Word.application")
End If
Err.Clear
On Error GoTo 0
oWordApp.Visible = True
'open word doc
Set oWordDoc = oWordApp.Documents.Open(FlName)
'declare excel objects
Dim wb As Workbook, ws As Worksheet
'Adding New Workbook
Set wb = Workbooks.Add
'Saving the Workbook
ActiveWorkbook.SaveAs "C:\Users\iansch\Desktop\DummyWB.xlsx"
Set ws = wb.Sheets(1)
TheContent = oWordDoc.Tables.Item(1).Cell(2, 3).Range.Text
ws.Range("A1").Activate
ws.Range("A1").Value = Trim(Replace(TheContent, Chr(7), Chr(32))) 'Remove strange character at the end
End Sub
Sub-wordscraper90000()
将oWordApp作为对象,将oWordDoc作为对象
将名称设置为字符串
将内容设置为字符串
FlName=Application.GetOpenFilename(“Word文件(*.Doc*),*.Doc*”_
“输入带有所需信息的.doc文件路径”)
'建立word应用程序对象
出错时继续下一步
Set oWordApp=GetObject(,“Word.application”)
如果错误号为0,则
设置oWordApp=CreateObject(“Word.application”)
如果结束
呃,明白了
错误转到0
oWordApp.Visible=True
“开放式word文档
设置oWordDoc=oWordApp.Documents.Open(FlName)
'声明excel对象
将wb设置为工作簿,ws设置为工作表
'添加新工作簿
设置wb=工作簿。添加
'保存工作簿
ActiveWorkbook.SaveAs“C:\Users\iansch\Desktop\DummyWB.xlsx”
设置ws=wb.Sheets(1)
content=oWordDoc.Tables.Item(1).单元格(2,3).Range.Text
ws.范围(“A1”)。激活
ws.Range(“A1”).Value=Trim(Replace(内容,Chr(7),Chr(32)))'删除结尾处的奇怪字符
端接头
而要提取的数据位于第2行第3列:值不在表中,但这非常有用!非常感谢。基于文档类型,项目最终变得不可行,因为目标值在每个月的版本中没有一个静态的相对位置。我确实在一个医生身上试用过,效果很好,谢谢!
Sub wordscraper90000()
Dim oWordApp As Object, oWordDoc As Object
Dim FlName As String
Dim TheContent As String
FlName = Application.GetOpenFilename("Word Files (*.Doc*),*.Doc*", , _
"Enter filepath of .doc with desired information")
'establish word app object
On Error Resume Next
Set oWordApp = GetObject(, "Word.application")
If Err.Number <> 0 Then
Set oWordApp = CreateObject("Word.application")
End If
Err.Clear
On Error GoTo 0
oWordApp.Visible = True
'open word doc
Set oWordDoc = oWordApp.Documents.Open(FlName)
'declare excel objects
Dim wb As Workbook, ws As Worksheet
'Adding New Workbook
Set wb = Workbooks.Add
'Saving the Workbook
ActiveWorkbook.SaveAs "C:\Users\iansch\Desktop\DummyWB.xlsx"
Set ws = wb.Sheets(1)
TheContent = oWordDoc.Tables.Item(1).Cell(2, 3).Range.Text
ws.Range("A1").Activate
ws.Range("A1").Value = Trim(Replace(TheContent, Chr(7), Chr(32))) 'Remove strange character at the end
End Sub