Jump to content
Macro Express Forums
koden

Copy From Notepad Problem

Recommended Posts

I have this problem that I can't find the best way to solve.

 

 

I get a notepad dokument with names.

Those names I have to copy and then fill in to another program.

This other program has fields for:

 

first name

middle name

last name

 

But in notepad I can get names in all kind of variants.

 

Ex.

 

Firstname middlename middlename lastname

or

firstname lastname

or

firstname middlename lastname

or

firstname-firstname middlename lastname

or

firstname lastname-lastname

 

and so on.......

 

In the program where I have to paste, I always have the 3 fields first,middle and lastname.

 

The problem is to get the right names into T1,T2 and T3 from the notepad.

 

My own surgestion is that I check for space and.

 

First copy firstname until a space and put this in T1.

 

Then copy the rest of the line into T10.

Check T10 for spaces and copy text after last spaces into T3

Copy all. between first space and last space into T2.

 

Then I always get firstname in T1 and all middlenames into T2 and last name into T3.

Am I right or have anyone a better idea?

 

If I'm right then my problem is how to do it the easy way.

 

I would use arrow to jump around and ctrl+shift+arrow to mark a name.

But then - does not come with.

How do i get the macro to identify the spaces???

Share this post


Link to post
Share on other sites

Hello!

 

I've got the solution right here!

 

You only have to make sure to copy the lines one by one to the clipboard so the macro can split them.

 

- This macro is Bulletproof! :P

 

<REM2:Hello there - Strathos><TVAR2:04:03:><TMVAR2:01:04:00:000:000:><REM2:Get First Name><IVAR2:04:13:4: ><NMVAR:09:04:0:0000001:0:0000000><IFVAR2:2:04:3:0><TBOX4:T:1:CenterCenter000278000200:000:Error:Only 1 Word Detected...

T4:
%T4%><MSTOP><ENDIF><TMVAR2:10:01:04:001:N04:><TMVAR2:11:04:00:001:N04:><TMVAR2:01:01:00:000:000:><TBOX4:T:1:CenterCenter000278000200:000:First Name:%T1%><REM2:Get Last Name><IVAR2:04:12:4><IVAR2:03:01:%N4%><REP3:08:000007:000001:0003:0:01: ><NMVAR:09:03:0:0000001:0:0000000><TMVAR2:10:03:04:N03:N04:><DIS:<TBOX4:T:1:CenterCenter000278000200:000:Working...Detecting:

%T3%

- %N3%><ENDREP><TMVAR2:11:04:00:N03:N04:><TMVAR2:01:03:00:000:000:><TBOX4:T:1:CenterCenter000278000200:000:Last Name:%T3%><REM2:Get Middle Name><TVAR2:02:01:%T4%><TMVAR2:01:02:00:000:000:><TBOX4:T:1:CenterCenter000278000200:000:Middle Name:%T2%>

Share this post


Link to post
Share on other sites

Its quite simple actually :ph34r:

 

Have you added the code with the Direct Editor?

 

In case you haven't:

First you need to create a new macro. Then, once you are editing it, click on "Direct Editor" (on the left side). Now you only have to copy the code I did.

Share this post


Link to post
Share on other sites

Thanks a lot...but I can't get it working.

I can see what your code is doing, but I would like to understand it :-)

If you have the time to explain what the code does with this:

 

firstname middlename1 middlename2 lastname

 

The copy should result in

 

T1 = firstname

T2 = middlename1 and 2

T3 = lastname

 

If there is only 1 middlename or none, then T2 should be 1 name or none.

Share this post


Link to post
Share on other sites

Sorry to bring this up again, but it's not working for me.

 

maybe there is a simpler solution (cant tell if strathos is simple, because I don't understand whats happening :-)

 

Is it possible to copy the whole line with first,last and all middle name into a varibale.

Then check for how many spaces.

If there is 5 spaces then it must be this :

 

firstname,middlename1,middlename2,middlename3,middlename4,lastname

(nomally no one have 4 middle names, but for the ex. :-)

 

Then the macro has to take firstname into variable 1

lastname into variable 2

And all ather names into variable 3 with spaces between each middlename.

 

names with - between has to be one name.

In my case they sometimes look like this george-schmidt.

george-schmidt is one name.

 

maybe that is what strathos is doing?

Share this post


Link to post
Share on other sites

I'll try to comment it. Note that you cannot copy this into Macro Express directly!

 

 

//Previously one line from the Notepad document was copied to the Clipboard.

//Don't use Ctrl+Shift+arrow but: Home Shift+End

 

//Store this line in temorary text variable T4

Variable Set String %T4% from Clipboard

//Get rid of leading and trailing spaces/tabs

Variable Modify String: Trim %T4%

 

 

 

// Get First Name

//Set temporary variable N4 to the position of the first space in T4.

//Double click the command in the Scripting Editor and click in 'Search Text' to see the space.

Variable Set Integer %N4% from Position of Text in Variable %T4%

 

//Decrease N4 (subtract 1) so that N4 is the position of the last character of First Name

//and also the length of First Name.

Variable Modify Integer: Dec (%N4%)

 

//In case no spaces were found, N4 is 0

If Variable %N4% < 0

Text Box Display: Error:

Macro Stop

End If

 

//Copy First Name to T1

//Copy N4 characters from T4 to T1 starting character 1.

Variable Modify String: Copy Part of %T4% to %T1%

 

//Delete First Name from T4

//Delete N4 characters from T4 starting character 1.

Variable Modify String: Delete Part of %T4%

 

//Get rid of leading and trailing spaces/tabs in First Name

Variable Modify String: Trim %T1%

//and display it

Text Box Display: First Name:

 

 

 

// Get Last Name

//Reuse temporary variable N4 and set it to the length of the remaining: maybe Middle Name(s) & Last Name

//which is also the position of the last character.

Variable Set Integer %N4% from Length of Variable %T4%

 

//Copy N4 to other temporary variable N3. We have to modify this value below and we also need N4 again.

//You could also use Variable Modify Integer: Copy %N4% to %N3%

Variable Set Integer %N3% to %N4%

 

//Repeat: look througth T4 (from last to first character one by one) until a space was found.

Repeat Until %T3% Contains " "

//N3 initially points to the last character of T4. At each repeat loop:

//Decrease N3 (subtract 1) so that it points to the previous character

Variable Modify Integer: Dec (%N3%)

 

//Copy the characters from position N3 to the end

//Copy N4 characters from T4 to T3 starting character N3

Variable Modify String: Copy Part of %T4% to %T3%

Repeat End

 

//When a space was found, the Repeat was aborted.

//This can be the space between Middle Name & Last Name or the space between First Name & Last Name.

//T3 now contains the Last Name.

 

//Delete Last Name from T4

//Delete N4 characters from T4 starting character N3

Variable Modify String: Delete Part of %T4%

 

//Get rid of leading and trailing spaces/tabs in Last Name

Variable Modify String: Trim %T3%

//and display it

Text Box Display: Last Name:

 

 

 

// Get Middle Name

//Copy everything that is left in T4 to Middle Name. If no Middle Name is present, T4 contains only

//the space between First Name & Last Name.

//You could also use Variable Modify String: Copy %T4% to %T2%

Variable Set String %T2% "%T4%"

 

//Get rid of leading and trailing spaces/tabs in Last Name

Variable Modify String: Trim %T2%

//and display it

Text Box Display: Middle Name:

 

 

 

 

 

 

 

//names with - between has to be one name.

//In my case they sometimes look like this george-schmidt.

//george-schmidt is one name.

 

As we only consider space as a separator character, that's no problem.

Share this post


Link to post
Share on other sites

Strathos approaches the problem as I would. He takes the clipboard into a string variable and gets the position of the firs space and saves that to an integer variable. He then decrements the integer once and grabs the text from character 1 to the integer value. And he simply plows on thru parsing the bits between spaces into text variables.

 

But I see a problem here. How does anyone know what is first middle and last? Your rule of 5 versus 4 is the key and your going the right way. First trim the variable from the clipboard and strip CRLF (Carriage Return Line Feed). Many times users accidentally grab spaces or an "Enter" on the end. Then count how many characters there are. Now repeat that many times and grab each subsequent character and increment a counter if it’s a space. I do this in a couple of macros counting tabs so I can tell how many fields there are. Here, let me hack on out quick.

 

Variable Set String %T1% from Clipboard

Variable Modify String: Trim %T1%

Variable Modify String: Strip CR/LF from %T1%

Variable Set Integer %N1% from Length of Variable %T1%

Repeat Start (Repeat %N1% times)

Variable Modify String: Copy Part of %T1% to %T2%

If Variable %T2% = " "

Variable Modify Integer: Inc (%N3%)

End If

Repeat End

 

<TVAR2:01:03:><TMVAR2:01:01:00:000:000:><TMVAR2:04:01:00:000:000:><IVAR2:01:12:1><REP3:01:000001:000001:%N1%:1:02:><TMVAR2:10:02:01:N02:001:><IFVAR2:1:02:1: ><NMVAR:08:03:0:0000001:0:0000000><ENDIF><ENDREP><TBOX4:T:1:CenterCenter000278000200:000:%N3%>

 

As you can see it's pretty simple. So now when you are parsing you can add a condition for the middle name part as weather you append the second middle name.

Share this post


Link to post
Share on other sites

I'm not sure I understand. I try....

This ex. has a name with first,2xmiddle and lastname

 

Variable Set String %T1% from Clipboard

//copy firstname middlename1 middlename2 lastname into T1

 

Variable Modify String: Trim %T1%

// trim the T1 so there is no spaces (but why that?)

 

Variable Modify String: Strip CR/LF from %T1%

// get rid of line feeds a.s.o.

 

// I don't quite understand what happens after here?

 

Variable Set Integer %N1% from Length of Variable %T1%

// set N1 from T1 ?? firstnamemiddlenamemiddlenamelastname ???

 

Repeat Start (Repeat %N1% times)

Variable Modify String: Copy Part of %T1% to %T2%

If Variable %T2% = " "

Variable Modify Integer: Inc (%N3%)

End If

Repeat End

 

I can't see how you can find out how many middlename there is and always get them into the middle name field in the other program.

When you trim T1 and remove spaces there is nothing to seperate firt,middle and lastname???

 

In my other program there is field for firstname, middlename and lastname.

 

If a person has 5 middlename (not seen yet :-) then all 5 has to be in the middlename field.

 

Do you understand what I meen ??

Share this post


Link to post
Share on other sites

SORRY SORRY.... I have tryed strathos script again and it's working.

I must have done something wrong first time

 

but I still don't understand how spaces are reconized...

And again how the names are seperated from each other.

 

It maybe does'nt matter but I would like to understand :-))

 

THANKS A LOT....

Share this post


Link to post
Share on other sites

My example counts the number of spaces in your text variable. The number of names is therefore N+1. I mentioned this because you can use this to determine up front if there are two middle names. If N1=3 then you know there are two middle names and you can put them in the right place. IOW if you had 4 string variables but the person only had one middle name then the last name would be put into the second middle name field. But let me answer some of your other questions:

 

Why trim? Because often users accidentally grab an extra space when copying with the windows clipboard. It’s not strictly necessary, just something I always do. This and the CRLF are just extra clean up steps. I think you’re confused by the Trim command. Trim does not remove spaces but rather it removed superfluous spaces from the ends of the text variable. It does not remove any spaces inside of that.

 

Setting N1? N1 is an integer variable that tells us how many characters are in the string variable. We will use this to repeat N1 times when counting spaces.

 

Let me describe this blow by blow. We clean the spaces off the end and remove any extra CRLF. We then count how many spaces are in the string variable and call it N1. We then repeat N1 times. When doing this repeat the repeat command sets N2 to the current iteration. IOW it counts up each time starting at 1. Inside this repeat we grab 1 character at position N2 and copy it to T2. We then test that character to see if T2 is a space. You need to look close since you can’t see it obviously but I have typed a space in there. Notice there’s a “ “ instead of a “” in the raw script and script text. If T2 is a space then we increment variable N3. When it’s all over N3 is the number of spaces. Does that make sense? From here one would use N3 in logic to determine if the last name is a last name or the second middle name.

 

Now you mentioned there could be more than two middle name? You did not mention this in your examples before. But you can use this same method to count the number of words in order to determine where to stick each string var.

Share this post


Link to post
Share on other sites

Just as a tip -

 

The "Variable Modify String: Trim" command trims all non-printable characters from a string, not just spaces. Therefore, stripping the CR/LF is not needed.

Share this post


Link to post
Share on other sites

Hey now, that is good to know. Thanks Joe! I was thinking I got it backwards anyway.

 

What would be nice is a trim that work like MS Excel's trim where it also reduces multiple spaces to a single space each.

Share this post


Link to post
Share on other sites
What would be nice is a trim that work like MS Excel's trim where it also reduces multiple spaces to a single space each.
Wouldn't this work?
Replace "  " with " " in %T1%

I didn't test it.

Share this post


Link to post
Share on other sites

It would work if placed inside a loop:

Variable Set String %T1% "qwerty                qwerty            qwerty"
Repeat Until %N1% = 0
  Replace "  " with " " in %T1%
  Variable Set Integer %N1% from Position of Text in Variable %T1%
Repeat End


<TVAR2:01:01:qwerty                qwerty><REP3:08:000001:000002:0001:0:01:0><TMVAR2:21:01:01:000:000:   ><IVAR2:01:13:1:  ><ENDREP>

Share this post


Link to post
Share on other sites

Yes it would but one would also need to replace three spaces and so on. There's always a way, I was just thinking it would be more elegant if the programmers at insight could simply anticipate my every whim and possible usage so that ME works exactly the way it think it should. :rolleyes:

 

In Excel I often concatenate cell contents to form quasi sentences that are easier for the user to read and understand and the Excel Trim come in very handy in these instances.

 

I was thinking of writing a subroutine that would clean text. Have it evaluate one character at a time and if the ASCII value falls out of range be it odd characters or whatever and also kill end and multiple spaces. One of these days…

Share this post


Link to post
Share on other sites
but one would also need to replace three spaces and so on

Yes, that's exactly what it does. It replaces two or more spaces with a single space. And it keeps doing it until only single spaces are left.

Share this post


Link to post
Share on other sites
Let me describe this blow by blow. We clean the spaces off the end and remove any extra CRLF. We then count how many spaces are in the string variable and call it N1.

Hey Cory:

 

I'm still not sure. Ypu wrote that you removes all spaces with TRIM.

But how can the macro then find spaces after this Trim?

 

Tha macro starts like this (i have changed var numbers in my macro):

 

variable set string T8 from clipboard

variable modify string trim T8

// now there are no spaces???

variable set integer N4 from position of text in T8 that are = (the search text box is empty and therefore it looks after space).

// but how find spaces that we have trimmed away before???

 

AND

decrement of N4 is doing what??? removing 1 character i can read in help.. But which one?

Share this post


Link to post
Share on other sites

Maybe it's better if you have time to comment the places with //? and check if the one I think I have understand is right :-)

 

Variable Set String %T8% from Clipboard

//paste clipboard into T8 (first,middlenames,lastname and spaces).

Variable Modify String: Trim %T8%

// remove all spaces a.s.o. (so left is firstnamemiddlenameslastname)

Variable Set Integer %N4% from Position of Text in Variable %T8%

//find first space?

Variable Modify Integer: Dec (%N4%)

//?

If Variable %N4% < 0

Text Box Display: Error:

Macro Stop

End If

Variable Modify String: Copy Part of %T8% to %T4%

//copy firstname into T4

Variable Modify String: Delete Part of %T8%

//delete firstname so middlename1 is first in line

Variable Modify String: Trim %T4%

//Trimming what?

Share this post


Link to post
Share on other sites

I don’t have a lot of time this morning as I’ve had several crisis in my own systems this past couple of days. Stupid router. So I’ll just kind of blast thru your posts one line at a time.

 

Please read my message more carefully. Also you might actually try doing this using the Debug > Debug Windows > Variable Values so you can see what effect the commands are having on the text. Trim does NOT remove spaces in the middle. It simply removes the non-printing characters from the ends of your string variable. The only reason I added this, as I explained before, is because sometimes users aren’t careful when they copy text and pick up superfluous leading or trailing characters that will mess up the parsing routine. If it helps you understand simply remove the trim command. It’s not strictly needed.

 

On to your second post and the annotations:

1- [//find first space?] Yes, N4 is used to find the first space.

2- [//?] The Copy Part of Text will now parse out the first string variable. The command requires a start point and an end point. The start point is 1 in each case and is not variable. The end point is variable. My name is Cory Allen Jackson. The position of the first space is 5. If we grabbed 1-5 the text in your new variable would be “Cory “ with a space on the end. By decrementing N4 prior we get “Cory”.

3- [//Trimming what?] You’re trimming T8. In answer 2 above you have N4 set to 4, right? So now you delete 1-4 which leaves you with “ Allen Jackson”. Now the problem is that you have a leading space. If you ran the routine again it would find a space in position 1, copy 1-1 and you would be left with “ “ instead of the desired “Allen” for my middle name. So you Trim the space off the end. Usually I simply delete one character but Strathos’ approach is slick because it will work regardless of how many spaces there are. Very clever. I’ll have to adopt that one for myself.

 

I think mainly you’re having troubles because of your misunderstanding of what Trim does. Go play with it and once you understand that it only removed non-printable characters from the ends of the string I think the rest will make sense to you.

Share this post


Link to post
Share on other sites

Thanks... I will check it all again and what you have written..

 

one problem I have, is that i'm not english.

So sometimes I don't get the meaning even if it's simple..

 

But for my "defence" I think it's better to get the understanding than just copy code :-))

 

I'm very happy for your time spending with helping me.......

And it's okay for me to say if you don't have the time...

Share this post


Link to post
Share on other sites

Well your english is good enough to fool me! I was writing as if you were so I'll try to write more simply in the future.

 

So where are you from? I live in Southern California although I originally hail from Montana.

Share this post


Link to post
Share on other sites

Denmark.... I have been in California. Have some familly in San Francisco.

Great country or state :-))

 

And one more time...thanks for all the times you have helped me.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...