【问题标题】:vba Word create a variable with value of text between 2 cursor pointsvba Word在2个光标点之间创建一个具有文本值的变量
【发布时间】:2018-08-28 01:57:10
【问题描述】:

我们正在尝试修改由 Molecular Device 软件创建的 rtf 文档。 以下是这些文档之一的部分示例:

协议“C:\ALL USERS\Params\Current\2017 Opto Params\0 VoltageClampContinuous.pro”已打开。 C:\ALL USERS\Alan\2018_07_11\2018_07_11_0000.abf 开始于秒表时间 00:19:48。

所以现在 - 我要做的就是自动找到实验日期(在这种情况下 =“2018_07_11_”) 到目前为止,我的 sub 可以找到正确的光标位置,但是如何在 2 个光标位置之间选择文本?

以下是我所拥有的 CursorPosition 语句当然是错误的 - 这是我想要更正的内容。

Sub FindfilenameDate()

    txt_prior_to_expDate = "\"
    txt_after_expDate = "0000"
    With ActiveDocument.Content.Find
            .Text = txt_after_expDate
            .Forward = True
            .Execute
            If .Found = True Then
                .Parent.Select
                Set after_rng = Selection.Range
                expDateEnd_cursorPos = after_rng.Start - 1
                Selection.HomeKey Unit:=wdStory, Extend:=wdExtend
                With Selection.Find
                    .Text = txt_prior_to_expDate
                    .Forward = False
                    .Execute
                    If .Found = True Then
                        .Parent.Select
                        Set charBefore_expDate = Selection.Range
                        expDateStart_cursorPos = charBefore_expDate.Start + 1
                    End If
                End With
            End If
    End With
   'expDate = CursorPosition(expDateStart_cursorPos, expDateEnd_cursorPos)
'MsgBox ("expDate = " & expDate) 'DELETEMSGBOX
End Sub

【问题讨论】:

    标签: vba ms-word cursor-position


    【解决方案1】:

    这样的技巧是使用多个Range 对象。我个人的偏好是为每个要使用的单独事物声明一个 Range,而不是试图找出最小值并重新使用 Range - 至少用于初始代码和测试目的。

    然后,对于这个任务,我使用了四个范围:1) 用于原始搜索,2) 用于所需的“光标位置”的结尾,3) 用于第二次搜索,4) 用于最终的“光标位置” ”。

    其他重要的概念是如何“折叠”范围以及如何“复制”范围。

    折叠范围就像在选择时按下右箭头或左箭头键,因此它是一个“点”并且不包含任何内容。 Range 可以折叠到它的开始或结束位置。

    需要使用Duplicate 属性来复制范围(将一个范围设置为另一个范围),以便副本独立于原始范围。否则,当一个改变时,另一个也会改变。

    Sub FindfilenameDate()
        Dim rngFind As Word.Range, rngBefore As Word.Range
        Dim rngAfter As Word.Range, rngFound As Word.Range
    
        txt_prior_to_expDate = "\"
        txt_after_expDate = "0000"
        Set rngFind = ActiveDocument.content
        With rngFind.Find
                .Text = txt_after_expDate
                .Forward = True
                .Execute
                If .found = True Then
    
                    Set rngAfter = rngFind.Duplicate
                    rngAfter.Collapse wdCollapseStart
                    Set rngBefore = rngFind.Duplicate
                    rngBefore.Collapse wdCollapseStart
                    With rngBefore.Find
                        .Text = txt_prior_to_expDate
                        .Forward = False
                        .Execute
                        If .found = True Then
                            Set rngFound = rngBefore.Duplicate
                            rngFound.Collapse wdCollapseEnd
                            rngFound.End = rngAfter.Start
                            'rngFound.Select
                        End If
                    End With
                End If
        End With
       'expDate = CursorPosition(expDateStart_cursorPos, expDateEnd_cursorPos)
        MsgBox ("expDate = " & rngFound.Text) 'DELETEMSGBOX
    End Sub
    

    【讨论】:

      【解决方案2】:

      虽然不清楚为什么您要在以 _0000 结尾的日期字符串而不是作为父文件夹名称的日期之后,但对于单个日期来说,更简单的方法是:

      Sub Demo()
      Application.ScreenUpdating = False
      With ActiveDocument.Range
        With .Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Text = "[0-9]{4}_[0-9]{2}_[0-9]{2}_0000"
          .Replacement.Text = ""
          .Forward = True
          .Wrap = wdFindStop
          .Format = False
          .MatchWildcards = True
          .Execute
        End With
        If .Find.Found = True Then MsgBox "expDate = " & Split(.Text, "_0000")(0)
      End With
      Application.ScreenUpdating = True
      End Sub
      

      并且,对于文档中的所有此类日期:

      Sub Demo()
      Application.ScreenUpdating = False
      With ActiveDocument.Range
        With .Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Text = "[0-9]{4}_[0-9]{2}_[0-9]{2}_0000"
          .Replacement.Text = ""
          .Forward = True
          .Wrap = wdFindStop
          .Format = False
          .MatchWildcards = True
          .Execute
        End With
        Do While .Find.Found = True
          MsgBox "expDate = " & Split(.Text, "_0000")(0)
          .Collapse wdCollapseEnd
          .Find.Execute
        Loop
      End With
      Application.ScreenUpdating = True
      End Sub
      

      【讨论】:

        猜你喜欢
        • 2013-10-29
        • 2016-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-18
        • 1970-01-01
        相关资源
        最近更新 更多