Trying to insert some text at the end of a series of characters

N

Novice

Hey all, I apologize in advanced if I have posted this to the wrong group -
but I'm not sure of where this should be posted.

I have some text in a text file in Visual Studio (not that it is relevant
but I'm developing a C# application). I would like to add a few characters
at the end of each line in this text file.

Right now I have this code:
Sub TemporaryMacro()
Dim Counter
Do While Counter < 10
Counter = Counter + 1
DTE.ActiveDocument.Selection.Paste()
DTE.ActiveDocument.Selection.Text = Counter & ", '"
DTE.ActiveDocument.Selection.EndOfLine()
'Need to back up because there is sometimes a space at end of line
DTE.ActiveDocument.Selection.CharLeft()
DTE.ActiveDocument.Selection.Text = "')"
DTE.ActiveDocument.Selection.LineDown()

DTE.ActiveDocument.Selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstText)
Loop
End Sub

However, sometimes at the end of the line there is a space - I want to
ensure that the characters I add occur before the space characters at the end
of the line.

I tried using some code like this:
If (DTE.ActiveDocument.Selection.Text = " ") Then
MsgBox("found a space character")
Else
DTE.ActiveDocument.Selection.CharRight()
End If

But the above conditional never finds the space character.

Any suggestions?

Thanks,
Novice

PS The input is something like this:
Executive
Senior Executive
President
Secretary
Administrative Assistant

And the output will have some text before and after the above text:
some text 1, 'Executive')
some text 2, 'Senior Executive')
some text 3, 'President')
some text 4, 'Secretary')
some text 5, 'Administrative Assistant')

notice that there are no spaces after each of the above titles (that is what
I'm trying to prevent).
 
N

Novice

Hey all, I found a solution - but I would like to see if anyone has a better
suggestion - basically in regards to the handling of the space character.
Here is the solution that works:
Dim Counter As Integer
Do While Counter < 200
Counter = Counter + 1
Selection.TypeText Text:="some text (" & Counter & ", '"
Selection.EndKey Unit:=wdLine
Selection.MoveLeft
Selection.MoveLeft
If Not (Selection.Text = " ") Then
Selection.MoveRight
If Not (Selection.Text = " ") Then
Selection.MoveRight
End If
End If
Selection.TypeText Text:="')"
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.HomeKey Unit:=wdLine
Loop

I basically just create the assumption that there are no more than 2 extra
spaces at the end of the line - which makes the code fragile (i.e. not
robust). I realize I could put a looping structure in there instead of the
conditional statement - but I'm guessing there is some way to do this
elogantly using some native functions (which would also likely make it
quicker).

Thanks,
Novice
 
H

Helmut Weber

Hi,
if you are talking of text-files, not word documents,
then you don't need word at all. It is a matter of
opening files, reading, doing very basic string-processing, writing.
That's all that is to it. Using VB or VBA it could look like this:

Dim s As String
Open "c:\test\input.txt" For Input As #1
Open "c:\test\output.txt" For Output As #2
While Not EOF(1)
Line Input #1, s
While Right(s, 1) = " " ' cut off trailing spaces
' which are of no good anyway
s = Left(s, Len(s) - 1)
Wend
Print #2, s & "'" ' Add '
Wend
Close #1
Close #2
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
C

Chuck

Adding to Helmut's suggested code slightly, you could use the Trim or RTrim
functions to remove all spaces from both ends of a string (Trim) or just the
right end of the string (RTrim):

Dim s As String
Open "c:\test\input.txt" For Input As #1
Open "c:\test\output.txt" For Output As #2
While Not EOF(1)
Line Input #1, s
s = Trim(s) 'Replaces string s with trimmed string
'OR
s = RTrim(s) 'Replaces string s with right trimmed string
Print #2, s & "'" ' Add '
Wend
Close #1
Close #2

HTH
Chuck
 

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