#1
|
|||
|
|||
Puzzling Behavior
Given that my current position is in the space just to the left of any of the Z-0 entries,
X-0.4679 Y0.1841 Z-0.259 A-15.145 C12.53 X-0.6704 Y0.1719 Z-0.2771 A-15.395 C16.017 X-0.7994 Y0.1623 Z-0.2923 A-15.582 C18.188 X-1.0048 Y0.1442 Z-0.3225 A-15.924 C21.547 X-1.0627 Y0.1385 Z-0.3323 A-16.03 C22.473 X-1.2737 Y0.1152 Z-0.3725 A-16.452 C25.755 X-1.3232 Y0.1092 Z-0.383 A-16.559 C26.505 X-1.5384 Y0.0808 Z-0.4329 A-17.055 C29.67 X-1.7583 Y0.0477 Z-0.491 A-17.615 C32.744 And with the following, With Selection.Find .Text = "X-" .Forward = False .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute why does Selection.Find.Found Return False? |
#2
|
|||
|
|||
You don't have Selection.Find.Found in your code. I don't see how it would return anything.
With this code and your example and instructions, it returns "True" Sub ScratchMacro() 'A basic Word Macro coded by Gregory K. Maxey With Selection.Find .Text = "X-" .Forward = False .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False MsgBox .Found End With lbl_Exit: Exit Sub End Sub |
#3
|
|||
|
|||
Selection.Find.Execute sets all the members of Selection.Find from what I can see. I set a watch on Selection.Find and literally watched "Found" change from True (It successfully found another string) to False.
|
#4
|
|||
|
|||
Attached is my problem in real time
|
#5
|
|||
|
|||
Find.Found has often been reported as unreliable, although I have yet to experience that. However, as Find.Execute returns a boolean it is unnecessary to use Found.
|
#6
|
|||
|
|||
I just tried doing the same logic with Selection.Find.Execute. It changed to False right there along Selection.Find.Found. Yet when I do a manual find exactly as I specified, Word finds it. I even recorded a Macro of the manual steps and replayed it. Same thing.
|
#7
|
|||
|
|||
Just tested your code by printing result to immediate window. It returns True every time.
Code:
Sub Find() With Selection.Find .Text = "X-" .Forward = False .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Debug.Print Selection.Find.Execute End Sub |
#8
|
|||
|
|||
Why don't you post your document and the code you are running instead of pictures of it?
|
#9
|
|||
|
|||
If I cannot find a solution soon I will. The fact that I can do the Find using the Word UI and then it works for you tells me there is something wrong with my environment (AV, OS corruption, RAM corruption, etc.). So I am going back to basics and disabling stuff and doing updates and see if anything changes. Failing that, I will come post something more substantial. Either way I will let you know. Thanks for the ongoing help.
|
#10
|
|||
|
|||
OK here we go. The updates did not improve things.
Here is a shortened but relevant snippet of what I am working with. ;FILENAME: ;SCPRT : ;RUNDATE : ;POSTNAME: ;POSTREV : G17 G40 G70 G80 G90 G500 T20 F32 FCT=0.005 G00 G79 Z(@ZPARKP1) G00 G79 A0. C0. ;--- BEGIN TOOL LIST --- ;T 1 - Q008 ;T 3 - 0.2500 DIA - 2 FLUTE - ENDMILL ;--- ENDOF TOOL LIST --- ;PATH:1 - 5AX TRIM ;VIEW:9 - ORIENT - ANGLES: 0,90 ;TOOL:1 - DIAM:.5 CRAD:0 REF GL:0 ;DESC: Q008 ;MATL: HSS M06 T1.1 (UAO,1) (UIO,Z(-1*L370)) (TCP,5) S18000 M03 G00 X12.7226 Y-7.0348 Z4. A0. C0. A-56.441 C-76.373 Z-5.6689 G01 Z-6.6689 F50. X2.5074 Y-.0286 Z-.8416 A-22.097 C-17.003 X2.2894 Y.0125 Z-.7499 A-21.388 C-15.526 X2.0727 Y.0485 Z-.6656 A-20.705 C-13.983 X2.045 Y.0528 Z-.6553 A-20.62 C-13.78 X1.8306 Y.0835 Z-.5797 A-19.97 C-12.165 X1.6188 Y.1099 Z-.5121 A-19.349 C-10.488 X1.4098 Y.1324 Z-.4526 A-18.759 C-8.747 X1.31 Y.142 Z-.4268 A-18.485 C-7.884 X1.1016 Y.1595 Z-.3784 A-17.93 C-6.017 X.8934 Y.1737 Z-.3376 A-17.399 C-4.056 X.6863 Y.1847 Z-.3043 A-16.894 C-2.004 X.481 Y.1924 Z-.2786 A-16.418 C0. X.2782 Y.1969 Z-.2604 A-15.973 C2.354 X.2667 Y.197 Z-.2596 A-15.948 C2.484 X.0642 Y.1982 Z-.2492 A-15.53 C4.819 X-.1375 Y.1959 Z-.2462 A-15.14 C7.259 X-.2483 Y.1932 Z-.2475 A-14.938 C8.648 X-.2674 Y.1926 Z-.2482 A-14.953 C8.99 X-.4679 Y.1841 Z-.259 A-15.145 C12.53 X-.6704 Y.1719 Z-.2771 A-15.395 C16.017 X-.7994 Y.1623 Z-.2923 A-15.582 C18.188 X-1.0048 Y.1442 Z-.3225 A-15.924 C21.547 X-1.0627 Y.1385 Z-.3323 A-16.03 C22.473 X-1.2737 Y.1152 Z-.3725 A-16.452 C25.755 X-1.3232 Y.1092 Z-.383 A-16.559 C26.505 X-1.5384 Y.0808 Z-.4329 A-17.055 C29.67 X-1.7583 Y.0477 Z-.491 A-17.615 C32.744 X-1.8332 Y.0354 Z-.5123 A-17.817 C33.753 X-2.0586 Y-.0044 Z-.5813 A-18.461 C36.678 X-2.1625 Y-.0243 Z-.6153 A-18.775 C37.97 X-2.3941 Y-.0724 Z-.696 A-19.511 C40.722 1. We want (for now) to swap the positions of the X & Y values only Example: X1.8306 Y.0835 Z-.5797 A-19.97 C-12.165 Becomes Y.0835 X1.8306 Z-.5797 A-19.97 C-12.165 2. The values are cleaned up so that every value has at least a leading zero. There is a value “5AX TRIM” that I’m changing temporarily to “BAD TRIM” 3. I’m finding my X & Y values by searching for “X*Y” 4. As long as the first X value on a line does not start with “X-“, I can do a CTRL- <Right Arrow> in the space just left of the “Y” three times and get to the beginning of the line. If the line starts out with “X-“, it takes 5 for some reason. So I want to set a variable to how many CTRL-RIGHTs I need. 5. Using VBA, I am unable to detect a “X-“ above the current cursor position. This is my problem. p.s. I’m getting back into VBA after a long hiatus,m so I am still weak on things like ranges and how to just select to the beginning of a line so I am doing it this way. Here's my VBA code: Sub FindSwitchXandY() ' ' FindSwitchXandY ' ' Call PrepDocument Call SwitchXAndY End Sub Sub PrepDocument() ' ' Temporarily rename "5AX" ' ' Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting With Selection.Find .Text = "5AX TRIM" .Replacement.Text = "BAD TRIM" .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute Replace:=wdReplaceAll Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting With Selection.Find .Text = "-." .Replacement.Text = "-0." .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute Replace:=wdReplaceAll With Selection.Find .Text = "X." .Replacement.Text = "X0." .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute Replace:=wdReplaceAll With Selection.Find .Text = "Y." .Replacement.Text = "Y0." .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute Replace:=wdReplaceAll End Sub Sub SwitchXAndY() Dim NumWords As Integer On Error Resume Next ' Go to the string "X*Y" Selection.Find.ClearFormatting With Selection.Find .Text = "X*Y" .Forward = True .MatchWildcards = True .Execute End With Do While Selection.Find.Found = True 'Go to the next space With Selection.Find .Text = " " .Forward = True .MatchWildcards = True .Execute End With 'Selection.Find.Execute Selection.ClearFormatting With Selection.Find .Text = "X-" .Forward = False .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute If Selection.Find.Found = True Then NumWords = 5 Else NumWords = 3 Selection.MoveLeft Unit:=wdWord, Count:=NumWords, Extend:=wdExtend 'Cut it Selection.Cut 'Go to the next space With Selection.Find .Text = " " .Forward = True .MatchWildcards = True .Execute End With 'Selection.Find.Execute 'Paste the previously cut string Selection.PasteAndFormat (wdFormatOriginalFormatting) Selection.TypeText Text:=" " ' Go to the string "X*Y" Selection.Find.ClearFormatting With Selection.Find .Text = "X*Y" .Forward = True .MatchWildcards = True .Execute End With Loop End Sub Bottom line, "Find" doesn't work as expected in my context. |
#11
|
|||
|
|||
Find doesn't work simply because you are attempting to execute multiple find operations within a looped find.
Instead try the following: Code:
Sub PrepDocument() ' ' Temporarily rename "5AX" ' ' With ActiveDocument.Content.Find .ClearFormatting .Replacement.ClearFormatting .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False .Text = "5AX TRIM" .Replacement.Text = "BAD TRIM" .Execute Replace:=wdReplaceAll .Text = "-." .Replacement.Text = "-0." .Execute Replace:=wdReplaceAll .Text = "X." .Replacement.Text = "X0." .Execute Replace:=wdReplaceAll .Text = "Y." .Replacement.Text = "Y0." .Execute Replace:=wdReplaceAll End With End Sub Sub SwitchXAndY() Dim findRange As Range: Set findRange = ActiveDocument.Content Dim switchText() As String On Error Resume Next ' Go to the string "X*Y" With findRange With .Find .ClearFormatting .Forward = True .MatchWildcards = True .Wrap = wdFindStop .Text = "X*Z" End With Do While .Find.Execute = True .MoveEnd wdCharacter, -2 switchText = Split(.Text, " ") .Text = switchText(1) & " " & switchText(0) .Collapse wdCollapseEnd Loop End With End Sub |
#12
|
|||
|
|||
Thanks, I'll try it. Why can't I use Find in a loop? When/how do I "reset" it?
|
#13
|
|||
|
|||
Because you’re only using a single find object and overwrite its settings each time. For what you are attempting to achieve it is unnecessary to use Find anyway.
|
#14
|
|||
|
|||
OK that seems to work great! Thanks for your help!
|
#15
|
|||
|
|||
Taking a look at your code, it's possible the problem could be related to the search parameters you set in your query. For example, if you are looking for the text "X-" and there is no such string in your document, the Find method will return False. It might be worth checking that your text "X-" actually exists in your document.
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Puzzling behavior by Window shape in [Walls, Shell and Structure] | csdlman | Visio | 3 | 06-12-2018 03:50 AM |
Puzzling Read Only Issue | BertLady | PowerPoint | 0 | 10-15-2014 11:40 AM |
Odd cursor behavior | markg2 | Outlook | 0 | 06-15-2010 08:33 AM |
Bizarre behavior | Mozydan | Outlook | 0 | 11-19-2007 08:59 AM |
Odd Behavior from Outlook | stonkers | Outlook | 0 | 03-14-2006 12:50 PM |