Share via

Word "Sendkeys

Anonymous
2024-01-07T19:54:54+00:00

Hello from Steve

Objective please is to copy text and then find the second occurrence, using Sendkeys.

meaning  SendKeys "{Find}" or similar.

For example

GRACIE THE CAT

copy the above and now find the second occurrence

GRACIE THE CAT

I thank you

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points
2024-01-07T22:42:56+00:00

Sorry - change the name of the second macro to FindPreviousOccurrence

Was this answer helpful?

3 people found this answer helpful.
0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points
2024-01-07T20:25:59+00:00

Does this do what you want? Select the text you want to find, then run the macro.

Sub FindNextOccurrence()
    Dim s As String
    s = Selection.Text
    Selection.Collapse Direction:=wdCollapseEnd
    Selection.Find.Execute FindText:=s, Forward:=True
End Sub

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Anonymous
    2024-01-07T22:36:54+00:00

    Hello HansV

    Before I posted I changed Forward:=True to Forward:=False

    I am getting

    Compile error

    Ambiguous name detected: FindNextOccurence

    Sub FindNextOccurrence()
        Dim s As String
        s = Selection.Text
        Selection.Collapse Direction:=wdCollapseStart
        Selection.Find.Execute FindText:=s, Forward:=False
    End Sub
    

    Was this answer helpful?

    0 comments No comments
  2. HansV 462.6K Reputation points
    2024-01-07T22:05:14+00:00

    Like this:

    Sub FindNextOccurrence()
        Dim s As String
        s = Selection.Text
        Selection.Collapse Direction:=wdCollapseStart
        Selection.Find.Execute FindText:=s, Forward:=False
    End Sub
    

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2024-01-07T21:54:15+00:00

    Hello HansV

    Yes that is what I am looking for.

    Request please is it possible to search up.

    All the Best

    Steve

    Was this answer helpful?

    0 comments No comments