Find [*\] macro debug

J

Joacim

Hi,

Runtime error 4605 this method or property is not available

I recorded this macro (keystrokes). Debug goes to Selection.Copy

Then I did CTRL C CTRL END CTRL Enter CTRL V
To insert the highlighted select variables in [ ] and place at end of
document.

What do I need to do to continue after the End With to make Selection.Copy
function?


Option Explicit

Sub SelectVariables()
'
' SelectVariables Macro
' Macro recorded 10/09/2006
'
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Text = "\[*\]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With

**Selection.Copy Selection.EndKey Unit:=wdStory
Selection.InsertBreak Type:=wdPageBreak
Selection.PasteAndFormat (wdPasteDefault)
End Sub
 
T

Tony Jollans

The Copy will fail if the Find before it has failed - nothing is found so
there is nothing selected and, thus, no Selection to copy. In this case you
set up the Find but never actually execute it, but more generally ...

' As you have up to ....
.MatchWildcards = True
End With

' ... and then ....
If Selection.Find.Execute Then
Selection.Copy
Selection.EndKey Unit:=wdStory
Selection.InsertBreak Type:=wdPageBreak
Selection.PasteAndFormat (wdPasteDefault)
End If
 
H

Helmut Weber

Hi Joacim,

you have definded the properties of the find method
of the selection object.
Now there is only an .execute missing in your code.

With selection.Find
.Text = "\[*\]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute ' !!!
End With

There are other ways to do what you want.
Try to get used to ranges, if you like.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
R

Russ

Joacim,
Hi,

Runtime error 4605 this method or property is not available

I recorded this macro (keystrokes). Debug goes to Selection.Copy

Then I did CTRL C CTRL END CTRL Enter CTRL V
To insert the highlighted select variables in [ ] and place at end of
document.

What do I need to do to continue after the End With to make Selection.Copy
function?


Option Explicit

Sub SelectVariables()
'
' SelectVariables Macro
' Macro recorded 10/09/2006
'
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Text = "\[*\]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With

**Selection.Copy Selection.EndKey Unit:=wdStory
You somehow ended up with two program statements on one line above.
Selection.Copy
Selection.EndKey Unit:=wdStory
Each need to be on their on line.
 
J

Joacim

Thank you everyone for the advice.


Russ said:
Joacim,
Hi,

Runtime error 4605 this method or property is not available

I recorded this macro (keystrokes). Debug goes to Selection.Copy

Then I did CTRL C CTRL END CTRL Enter CTRL V
To insert the highlighted select variables in [ ] and place at end of
document.

What do I need to do to continue after the End With to make
Selection.Copy
function?


Option Explicit

Sub SelectVariables()
'
' SelectVariables Macro
' Macro recorded 10/09/2006
'
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Text = "\[*\]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With

**Selection.Copy Selection.EndKey Unit:=wdStory
You somehow ended up with two program statements on one line above.
Selection.Copy
Selection.EndKey Unit:=wdStory
Each need to be on their on line.
Selection.InsertBreak Type:=wdPageBreak
Selection.PasteAndFormat (wdPasteDefault)
End Sub
 
J

Joacim

I am trying to get all text in square brackets [ ] highlighted and copy the
find using highlight all found and then build on that to create a variable
list to be entered into left side of a 2 column table.

It is the beginners group - how do I range a Find instruction for the
document

I use range to fill bookmarks in user forms.

Thanks again.
 
H

Helmut Weber

Hi "Joacim",

I'd first make sure that there is a table large enough,
to hold all your so called variables, and not larger.
For that you have to count the occurences of "\[*\]".

Let's say a table is already there and it is the table(1), then

Sub Test601()
Dim rDcm As Range
Dim lCnt As Long
Dim oTbl As Table
Set rDcm = ActiveDocument.Range
Set oTbl = ActiveDocument.Tables(1)
With rDcm.Find
.Text = "\[*\]"
.MatchWildcards = True
While .Execute
lCnt = lCnt + 1
Wend
End With
MsgBox lCnt ' for testing using [F8], remove later
' adjust the table
With oTbl
While .Rows.Count < lCnt
.Rows.Add
Wend
While .Rows.Count > lCnt
.Rows(.Rows.Count).Delete
Wend
End With
lCnt = 0
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "\[*\]"
.MatchWildcards = True
While .Execute And _
rDcm.Information(wdWithInTable) = False
' to prevent an enless loop
lCnt = lCnt + 1
rDcm.Copy
oTbl.Cell(lCnt, 1).Range.Paste
Wend
End With
End Sub

Of course there are other ways to do that.

Quite something for a beginner.

HTH
--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
J

Joacim

That was great Helmut - very impressive range count insert into Table (1) .

A great way to do it. I understand a little more about ranges now. BIG thank
you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top