Vb Script how to do a replace and also add text on the same line


Can someone advise on my code please? I already manipulate a line of text by adding 4 characters, easy so far however im having a problem then trying to do a replace of 8 characters at the end of the same line.

Hes the code i already have that adds 4 characters to the line.

Source text line, note 1014 note added yet:

-   LDE1CM108ASN850FD  F1234   DHGYTH   6234652246973101HNPAAAAU

'write line with '1014' added 8 spaces in
w1.WriteLine Left(HeaderText_FirstLine, 8) & "1014" & Right(HeaderText_FirstLine, Len(HeaderText_FirstLine) - 8)

result, note 1014 is now added to the line, all good so far!:
-   LDE11014CM108ASN850FD  F1234   DHGYTH   6234652246973101HNPAAAAU

Now i want to change the last 8 characters of the same line(HNPAAAAU) with NNNTUIPP

Not sure how to do it with it giving me errors

I thought it would be as easy as
w1.WriteLine Left(HeaderText_FirstLine, 8) & "1014" & Right(HeaderText_FirstLine, Len(HeaderText_FirstLine) - 8), replace (replacetxt, "HNPAAAAU","NNNTUIPP")

But this adds another line, arhh!
Who is Participating?
I made two changes to your last line.
1. instead of cutting off only 8 characters from the end, lets cut off the first 8 and last 8 using Mid.
2. Just append the new string to the end
w1.WriteLine Left(HeaderText_FirstLine, 8) & "1014" & Mid(HeaderText_FirstLine, 9, Len(HeaderText_FirstLine) - 16) &"NNNTUIPP"
wilko1000Author Commented:
Excellent works thanks!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.