首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >连接符号,在Word doc和Range.MoveEndUntil Cset:=段落签名中找到文本& "2“&”。

连接符号,在Word doc和Range.MoveEndUntil Cset:=段落签名中找到文本& "2“&”。
EN

Stack Overflow用户
提问于 2020-01-28 16:04:45
回答 1查看 341关注 0票数 0

我有一个从Excel编辑器运行的宏,它在Word文档中搜索字符串,复制该字符串并将其粘贴到.Activesheet.Range("E27")中的一个单元格中。

所以我有这段代码,我已经接近让它工作了,但是我需要连接四个字符来使它工作,段落符号(),& "2“&”。

代码语言:javascript
复制
Sub AdjusFindFirstName()
    Sub AdjFindFirstName()
    'Variables declaration
    Dim WordApp As Word.Application
    Dim WordDoc As Word.Document
    Dim ExcelApp As Excel.Application
    Dim Rng As Word.Range

    Dim ws As Worksheet
    Dim TextToFind As String
    Dim ApartmentPrice As String
    Dim StartPos As Long
    Dim EndPos As Long
    Dim FullName As String
    Application.ScreenUpdating = False

    'Assigning object variables
    Set WordApp = GetObject(, "Word.Application")
    Set WordDoc = WordApp.ActiveDocument
    Set Rng = WordApp.ActiveDocument.Content
    'Searching
    With Rng.Find
    '.Text = TextToFind
    .Text = "REGON 364061169, NIP 951-24-09-783,"
    .Execute    '?Rng = REGON 364061169, NIP 951-24-09-783,
        If .Found = True Then
            Rng.MoveEndUntil Cset:="2" & ". "  'expands Rng variable until first "2. "                  
            Rng.MoveEnd wdWord, 3          'expands Rng variable wdWord, 3 further
                                           'wdParagraph, wdLine, wdSentence, wdScreen
            If Rng.Words.Last.Next.Text = "-" Then    'I want the text of the word following the last word in my range
               Rng.MoveEnd wdWord, 2    'expands Rng variable wdWord, 2 further
            End If
            StartPos = InStr(1, Rng.Text, "2. ")
            FullName = Mid(Rng.Text, StartPos + 3)
        End If
    End With
    Debug.Print FullName
End Sub

事实上,我甚至不能执行Range.MoveEndUntil Cset:= "2. "

我甚至试过Range.MoveEndUntil Cset:= "2" & ". "

这些行不会用任何单词或字符展开我的Rng变量。

但我真正需要的是找到/移动终端直到Cset:= "¶" & "2" & ". "

这里是printscreen,我选择了需要.MoveEndUntil的字符串

也许您可以告诉我如何将我的Rng变量展开到当前段落的末尾?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-28 17:15:01

仔细阅读MoveEndUntil的帮助主题可以发现,它只查看单个字符,而不是字符组合:

将指定范围的结束位置移动到文档中找到指定字符的任何为止。

没有以这种方式工作的“移动”方法。然而,Word的Find功能可以定位字符组合。添加一个从第一个“发现”项中运行的附加Find将定位问题中指定的信息。例如

代码语言:javascript
复制
Dim WordDoc As Word.Document
Dim rngStart As Word.Range, rngEnd As Word.Range

Set WordDoc = WordApp.ActiveDocument
Set rngStart = WordDoc.Content
'Searching
With rngStart.Find
'.Text = TextToFind
    .Text = "REGON 364061169, NIP 951-24-09-783,"
    .Execute    '?Rng = REGON 364061169, NIP 951-24-09-783,
    If .found = True Then
        'Instantiate a Range for the next search, starting with the first "found"
        Set rngEnd = rngStart.Duplicate
        'Extend that range to the end of the document
        rngEnd.End = WordDoc.Content.End
        With rngEnd.Find
            .Text = vbCr & "2. "
            .Forward = True
            .Execute
            If .found Then
                rngEnd.MoveEnd wdWord, 3
                If rngEnd.Words.Last.Next.Text = "-" Then    'I want the text of the word following the last word in my range
                    rng.MoveEnd wdWord, 2    'expands Rng variable wdWord, 2 further
                End If
            End If
        End With

        startPos = InStr(1, rngEnd.Text, "2. ")
        FullName = Mid(rngEnd.Text, startPos + 3)
    End If
End With
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59952686

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档