在较大查询中提取两个单词之间的文本

时间:2014-06-18 17:24:03

标签: excel-vba pdf word-vba vba excel

感谢您抽出宝贵时间阅读我的请求。我尝试在这个网站上使用一些答案,但我没有得到我想要的东西。 (我试过这个:Word VBA how to select text between two substrings and assign to variable?

我正在尝试选择一个始终在两个相同单词之间的数字。它位于“账号:”和“重要”之间(是全部上限,不确定上限/无上限是否与表示相关)。

我正在创建一个宏,我打开一个200页的word文档。我想打开并保存EACH PAGE,因为它是自己的具有特定名称的pdf。我已经将代码运行到我打开的位置并保存为PDF。我想要做的是,在该代码中,有一些东西可以找到“帐号:”和“重要”之间的文本,选择它并复制它。此文本是一个帐号。

然后,当我去保存文件时,我希望它将帐号粘贴为文件名。或者有一个参考,当它找到帐号时,它会将它分配给变量。我是VBA的新手,所以如果你可以请描述,并用外行术语说明。谢谢!

我的宏:

Sub CutePDFWriter()

Dim FName, FPath, username, LoanNo As String
Dim wordapp As Word.Application
Dim wordDoc As Word.Document
Dim i As Integer
Dim rngParagraphs As Range

'open doc and export as a pdf
Set wordapp = CreateObject("word.Application")
Set wordDoc = wordapp.Documents.Open("G:\test.doc")

For i = 1 To wordDoc.BuiltinDocumentProperties("Number of Pages")

**Here is where I want to add the “Find and Select” code**

'set variable strings
FPath = "G:\Excel Doc Tests\"
FName = "___**Here is where I want the acct nbr to go_______"** & i & ""

wordDoc.ExportAsFixedFormat FPath & FName & "-escrtax", ExportFormat:=wdExportFormatPDF, Range:=wdExportFromTo, From:=i, To:=i

Next i

'empty word doc objects
wordDoc.Close (False)
wordapp.Quit

End Sub

1 个答案:

答案 0 :(得分:0)

我在该链接上添加了对该问题的评论,这使他的代码工作。但是我花了很多时间在这上面:(用#34测试;等等等等等等号码账号:123-456重要的等等等等等等等等):

Option Explicit

Sub Sub1()
  Dim i&, FName$ ' I presume
  Dim i1&, i2&, s1$, rngDoc As Range
  Selection.HomeKey wdStory ' ?
  i1 = getPoint("Account No.:", 1) ' get start
  i2 = getPoint("IMPORTANT", 2) ' get end
  Set rngDoc = ActiveDocument.Range(i1, i2)
  s1 = rngDoc.Text
  FName = "Prefix" & s1 & "Postfix" & Str$(i)
  Stop ' and hover over FName
End Sub

Function getPoint&(sText$, iStart&) ' 1 for start, 2 for end
  With Selection.Find
    .Text = sText
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = True
    .MatchWholeWord = False
    .MatchAllWordForms = False
    .MatchSoundsLike = False
    .MatchWildcards = False
    .Execute
  End With
  If iStart = 1 Then
    getPoint = Selection.End
  Else
    getPoint = Selection.Start
  End If
End Function