Log in

View Full Version : Transpose specific words



Silence
05-23-2010, 11:02 AM
I need a macro that will replace the first word with the second one...

Example:

- John Johnson
- Patrick Something
- Annie Bunny

Convert to....

- Johnson John
- Something Patrick
- Bunny Annie

Can anyone help?

Thnx in advance...

Tinbendr
05-23-2010, 11:27 AM
Sub SwapNames()

With ActiveDocument.Content.Find
.ClearFormatting
.MatchWildcards = True
.Text = "(<*) ([! ]@)^13"
.Replacement.Text = "\2 \1^p"

.Execute Replace:=wdReplaceAll

End With


End Sub

Silence
05-23-2010, 03:20 PM
Thanks for replaying Tinbendr, this is the error I get after running the code:



Compile error:
Expected: identifier



Sub
'
' ClearHookedDialog Macro
'
'
Sub

With ActiveDocument.Content.Find
.ClearFormatting
.MatchWildcards = True
.Text = "(<*) ([! ]@)^13"
.Replacement.Text = "\2 \1^p"

.Execute Replace:=wdReplaceAll

End With


End Sub



Do you know what is the problem? Thnx

Tinbendr
05-23-2010, 05:46 PM
Thanks for replaying Tinbendr, this is the error I get after running the code:

Compile error:
Expected: identifier
Sub
'
' ClearHookedDialog Macro
'
'
Sub

Do you know what is the problem? ThnxYou have to subs with no sub end? Delete everything from the first Sub down to the second.

BTW, when you drop in Code, use the VBA tag. It makes it easier to read.

Thanks!

Silence
05-25-2010, 04:20 AM
Thnx Tinbendr problem solved. Thank you for the support :)

Silence
05-25-2010, 08:37 AM
Actually, there is somethings else...

Formula is made so that the last word comes in first place. That creates a problem because I have a whole sentence (Ana Maria goes to....). So I need to put second word in first place (Maria Ana goes to...) Currently this is what formula does... (to Ana Maria goes)

So second word in a line (not the last) goes in first place. I think you know what I mean.

Thanks

fumei
05-25-2010, 10:22 AM
Please always try to be fully detailed.

Your stated problem was fully solved by Tinbendr because you state it as two words.

Example:

- John Johnson
- Patrick Something
- Annie Bunny

Convert to....

- Johnson John
- Something Patrick
- Bunny Annie


In fact, the word count is not relevant, as your real problem is dealing with the first TWO words, regardless of the number of words of the original string.

This is significantly different.

Example:

- John Johnson
- Patrick Something
- Annie Bunny

is VERY different from:

- John Johnson has a little lamb
- Patrick Something ran the mile in four days
- Annie Bunny has a cottontail

Further, I do not want to start on this until I know if the dash in front is real, or not. Is it:

- Annie Bunny has a cottontail

OR

Annie Bunny has a cottontail

Further, are these terminated by a paragraph mark (are they whole paragraphs)? Are they possibly in a table?

Silence
05-25-2010, 11:34 AM
I made a confusion, for that I am sorry. This is exactly how my text looks like:

- Jake Johnson ("Dark Woods", season 1961/1962), production of MTM Theatre.
- Anna Rodrigez ("Mrs. Wake, season 1984/1985), production of KT 55.

et.

This is my text and there is no table. I need to replace last name with the first one. This is how it should look like in the end using the example above:
- Johnson Jake ("Dark Woods", season 1961/1962), production of MTM Theatre.

Thanks very much and once again sorry for confusion.

fumei
05-25-2010, 12:41 PM
No need to apologixe. I just want you to realize that it is in your own interest to give full details.

The use of the "- " is unfortunate. If they were not there you could use .Words of the paragraphs much easier. However, it can be done.
Sub ReverseSomeWords()
Dim aWord
Dim j As Long
Dim var
Dim ReplaceText As String

j = Selection.Words.Count

ReplaceText = Selection.Words(1) & _
Selection.Words(3) & _
Selection.Words(2)

For var = 4 To j
ReplaceText = ReplaceText & Selection.Words(var)
Next

Selection.Text = ReplaceText

End Sub
This works (unfortunately) with Selection. So select your paragraphs and execute.

Silence
05-25-2010, 01:05 PM
Fumei thanks very much for help! Formula works like a charm, there is only one mistake and it seems to be in this....

This is what I originaly posted:

- Anna Rodrigez ("Mrs. Wake, season 1984/1985), production of KT 55.

(fake dash)

But in my original document the dash is real:

- Anna Rodrigez ("Anna Mrs. Wake, season 1984/1985), production of KT 55.

(unfortunatly when I copy/paste here it looks the same)

So there is a bigger distance between dash and the first word. Everything else is perfect. Thnx

fumei
05-25-2010, 01:10 PM
"So there is a bigger distance between dash and the first word. "

Thus the reason to post accurately and precisely.

Silence
05-25-2010, 02:04 PM
I know... The distance is exactly 5 mm.


Can you please help me with this last part. Much appreciated :)

fumei
05-25-2010, 02:23 PM
HUH? I do not understand. I am not quite following. I thought you were meaning that "- " (dash, single space) was actually dash, two spaces.

So what, exactly, do you need help with?

Silence
05-25-2010, 02:49 PM
Actually it is dash, 5 spaces.

When I apply macro on exactly the same sentence I used here as an example it works perfectly (dash, one space)

But when I apply it in original word document (where is dash, 5 spaces) this is what i get:

- Rodrigez ("Anna Mrs. Wake, season 1984/1985), production of KT 55.

Don t know how to fix that...

fumei
05-25-2010, 02:57 PM
Ack. That is what you get with having 5 spaces. Good documents should never have extraneous spaces like that. I have to go, but will look at this later. Do you need the extra spaces? Will it always be five spaces?

The problem is that I was using .Words, and these are text + space (singular).

Silence
05-25-2010, 03:08 PM
It s always 5 spaces, in all the documents, and yes, I need them unfortunately.

Thanks very much :)

fumei
05-25-2010, 03:25 PM
Not tested....


ReplaceText = Selection.Words(1) & " " & _
Selection.Words(3) & _
Selection.Words(2)
which adds FOUR spaces after the first word ("- ")

Silence
05-25-2010, 03:57 PM
After I use it on the wrong version (dash, 1 space) it becomes dash, 5 spaces and it reversed the name as it should.

But when I apply it to the right version of the text (dash, 5 spaces) I still get the same result

- Anna ("Rodrigez Mrs. Wake, season 1984/1985), production of KT 55.

macropod
05-30-2010, 04:15 AM
Hi Silence,

Perhaps a simple wildcard Find/Replace with:
Find - (-[ ]{1,})([! ]{1,} )([! ]{1,} )([!^13]{1,}^13)
Replace - \1\3\2\4
With this, it really doesn't matter how many spaces follow the initial dash.

If you really want a macro solution, just change the corresponding expressions in Tinbendr's code.

Paul_Hossler
05-30-2010, 07:53 AM
macropod --

Cool - that would have taken me 7-8 (at least) tries to sort out the brackets and braces and inerds.

Paul

Silence
06-05-2010, 09:47 AM
Macropod, thnx for helping. Youre wildcard Find/Replace is interesting but it doesnt work for me. I ve been trying to change the expression but I m far from expert, keep hitting the wall unfortunately. Still have those same problems.

macropod
06-05-2010, 11:25 PM
Hi Silence,

The Find/Replace works for me with the strings you posted. Did you check the 'use wildcards' option?

Silence
06-06-2010, 04:38 AM
Yes I did, the following happens:

"The Find What text contains a pattern Match expression which is not valid."

macropod
06-07-2010, 01:39 AM
Hi Silence,

If you'd said earlier that the error related to the Find pattern, that would have helped.

The only thing in the Find expression that I can see that might produce such an error is the commas (ie ','), and that is only likely if your system settings are configured for a language other than English. Try changing the commas to semi-colons (ie ';').

Silence
06-09-2010, 09:52 AM
Yes, my native languange (and system settings) are not english. I did what you sad and it worked perfectly, but only on wrong example (dash, one space). In my document were is dash, 5 spaces the result is: "word has completed....and there are 0 replacements."

macropod
06-10-2010, 01:07 AM
Hi Silence,

the result is: "word has completed....and there are 0 replacements."
That's a very different issue to:

"The Find What text contains a pattern Match expression which is not valid."
The cause is most likely that your dashes aren't all simple hyphens (minus signs) and in which case you should get the result you're after by changing the '-' in the Find expression to '[\-^+^=^~]', ie:
Find - ([\-^+^=^~][ ]{1,})([! ]{1,} )([! ]{1,} )([!^13]{1,}^13)

Silence
06-10-2010, 05:30 AM
No my friend you didn t understand me. When I used the first version of you re code i got the error: " The Find What text contains a pattern Match.."

Then I changed the things you sad I should (because of my settings), changed the commas to semi-colons (ie ';'). After that the "word has completed....and there are 0 replacements." error happend.

This last version you sent me triggered the " The Find What text contains a pattern Match..:" error again unfortunately.

I uploaded a sample of my real document on rapid, here is the link:

http://rapidshare.com/files/397418883/Sample.doc

I should have done this in the beginning, now you have the actual document. I hope there will be no more confusion :)

Thnx

macropod
06-11-2010, 12:50 AM
Hi Silence,

If the pattern match error occurs with commas and not with semi-colons, then there is indeed a language/settings issue.

As for the sample document you posted, it has tabs after the hyphen, not multiple spaces. Please clarify what the document you're working with has (perhaps it's a mix of both).

Silence
06-11-2010, 03:47 AM
It s exactly like in the sample document. That is the actual text I m working with, I just cut some lines from the real thing. If its tabs after the hyphen in the sample, then that is what it is.

macropod
06-12-2010, 02:56 AM
Hi Silence,

Please go back through your posts in this thread and look carefully at how you've described your problem and what has occurred with each attempt to help you solve it. Then compare your sample document to your problem description and to what you've said has/hasn't worked with the solutions offered. What you'll find is that there is very little connection between what you've said the problem was, what did/didn't work and the sample document you've posted.

The sample document does NOT have paragraphs containing hyphens followed by spaces, then the 'problem' text. What is has are paragraphs formatted with hyphen-shaped bullets & tabs, and containing only the 'problem' text. In other words, while the paragraphs might look like they have hypens followed by tabs, then the 'problem' text, the hypens & tabs are not part of the paragraph content. However, since the Find/Replace operation that was predicated on your problem description apparently did work with one paragraph (after changing the commas to semi-colons), the document from which you extracted the sample cannot consist of only paragraphs formatted as per the sample – at least one paragraph must have contained a hyphen followed by one or more spaces, then the 'problem' text.

And, despite what you've claimed, your system settings cannot be correct for an english-language configuration, since the comma expression separators didn't work but the semi-colons did. In this context, I note that your document's text language is set to Croatian (not that the document language has any bearing on the Find/Replace operation).

Your failure to accurately describe the problem has resulted in a great waste of peoples' time – including your own. A Find/Replace operation to swap the first two words of each paragraph is really quite simple – but before investing any more time I'd want to be sure that you've fully described the problem(ie what all the paragraphs you want changed contain (which is not the same as how they're formatted). I don't want any more of this 'doesn't work' nonsense that results from your failure to adequately describe the problem.

Silence
06-12-2010, 06:55 PM
Trust me, I am well aware of wasted time and that I was responsible for mistakes. Franky, I m surprised people continue helping. (Thanks for that) Thing is, I am a lot more ignorant about the program then I originally thought. Second, even do I m good in english, this technical terminology is driving me insane.

Irony is, first solution almost solved my problem, so I though we should continue going that road and ultimately find a solution. Hell, if we are so close, than I couldn t have explained things that badly - that was my thinking. That didn t work out well :)

I have uploaded entire text now. It s one of many, but they are all the same. Nothing has been changed or modified. So what you are seeing I am seeing. Don t know what more description or information I can give you :)

http://rapidshare.com/files/398387912/Text.doc

And once more my friend, thanks for having the patience. And thanks to everyone else who helped and contributed on this long journey.

macropod
06-13-2010, 01:06 AM
Hi Silence,

Now that I've seen your entire document, it is clear that you don't simply want the first & second word of each paragraph swapped - what I believe you really want is for the given names and family names that each paragraph begins with swapped. But these given names and family names are not simply the first & second word of each paragraph.

For example, take a look at the first five entries in your file. The names appear to be:
. Aleksandrov Mihail Petrovič Staricki
. Amijel Deni
. Arden Džon
. Arman Gaston De Kajave
. B Stevan Sremac

Sure, we can swap the first two words around, so that you get:
. Mihail Aleksandrov Petrovič Staricki
. Deni Amijel
. Džon Arden
. Gaston Arman De Kajave
. Stevan B Sremac
but is that what you really want?

Or are you after something more like:
. Staricki Aleksandrov Mihail Petrovič
. Deni Amijel
. Džon Arden
. De Kajave Arman Gaston
. Sremac B Stevan

If, as I suspect, you want the latter, you're asking for something that really is beyond the abilities of Find/Replace or any generalised macro solution. That's because there is no reliable way to tell a Find/Replace operation or a macro which parts of each string are the given names and family names.

What I suggest you do is to break the file into multiple parts - one part for two-word names and at least one more part for names of more than two words, and leaving an empty paragraph at the end of the file with the two-word names. Assuming a correctly-set english language PC configuration, you can then use the following wildcard Find/Replace operation on the part with the two-word names:
Find - ([! ]{1,} )([! ]{1,} )([!^13]{1,}^13)
Replace - \2\1\3

The other file part(s) you'll probably need to do manually. If you look at the first and fourth entries the reason is clear - both have the same number of words, but the given name/family name split is quite different.