【问题标题】:Code to make selection of text and delete it选择文本并将其删除的代码
【发布时间】:2015-09-11 10:19:59
【问题描述】:

我正在尝试创建一个宏来查找特定表达式并删除该内容。 例如,我需要删除从单词“NOTIFICATION”到“早安”的所有内容(但保留“早安,如果可能的话)。

我有一个删除一行的代码,但无法弄清楚如何通过选择来执行此操作,因为我每次都没有相同数量的行。可以是 3 或最多 9,或多或少。

我的代码是这样的(我已经删除了与我有的问题无关的其他事情的代码部分):

    Private Sub ProcessMsg(msg As MailItem)
    On Error GoTo ErrorHandlerProcessMsg

    Dim msg2 As Outlook.MailItem
    Dim msgDoc As Word.Document
    Dim msgDoc2 As Word.Document
    Dim objSel As Word.Selection

    Set msg2 = Application.CreateItem(olMailItem)
    Set msgDoc = msg.GetInspector.WordEditor
    Set msgDoc2 = msg2.GetInspector.WordEditor

    msgDoc.Select
    msgDoc.Windows(1).Selection.Copy
    msgDoc2.Windows(1).Selection.PasteAndFormat wdPasteDefault

    Set objSel = msgDoc2.Windows(1).Selection
    With objSel
        .Find.Execute "NOTIFICATION"
        .Collapse wdCollapseStart
        .MoveEnd WdUnits.wdStory, 1
        .Delete
    End With

    Set objSel = msgDoc2.Windows(1).Selection
    With objSel
        .MoveStart WdUnits.wdStory, -1
        .Collapse wdCollapseStart
        .MoveEnd WdUnits.wdParagraph, 1
        .Delete
    End With


    Set msgDoc = Nothing
    Set msgDoc2 = Nothing
    Set objSel = Nothing
    Set msg2 = Nothing

    Exit Sub
ErrorHandlerProcessMsg:
    Set msgDoc = Nothing
    Set msgDoc2 = Nothing
    Set objSel = Nothing
    Set msg2 = Nothing
    Err.Raise Err.Number, Err.Source, Err.Description
End Sub

有人能告诉我吗?

【问题讨论】:

    标签: vba excel selection


    【解决方案1】:

    这可以满足您的需求(据我了解)。只需将此函数粘贴到 word 文档的代码对象之一中并运行它(已在 Word 上尝试过,而不是在 Excel 上)。主要是为了告诉你如何处理这样的问题。

    Sub LineRemover()
    
        Dim doc As Document
        Set doc = ActiveDocument
    
        Dim myParagraph As Paragraph
        Dim mySentences As Sentences
        Dim mySentence As Range
    
        Dim deleted As Boolean
        deleted = False
    
        For Each myParagraph In doc.Paragraphs
    
            Set mySentences = myParagraph.Range.Sentences
            For Each mySentence In mySentences
    
                If InStr(mySentence, "Good morning") <> 0 Then
                        '"Good morning" is present inside this line; exit the loop!
    
                        Exit For
    
                ElseIf deleted Then
                        '"NOTIFICATION" was already deleted, need to remove all subsequent lines!
    
                    mySentence.Delete
    
                ElseIf InStr(mySentence, "NOTIFICATION") <> 0 Then
                        '"NOTIFICATION" is present inside this line; delete it!
    
                    mySentence.Delete
                    deleted = True 'Tell the program you've deleted it!
    
                End If
    
            Next
    
            If deleted Then
                Exit For
            End If
    
        Next
    
    End Sub
    

    更多细节: InStr(String1, String2) 将返回String2String1 中的位置

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-08
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-21
      相关资源
      最近更新 更多