Addition (Newbie): 17 + 1 usually equals 18, What am I doing wro

M

Marceepoo

1. The following code is from a userform in MS Word, and is intended to
Title Cap the selection, and remove all spaces.
2. The problem is the line: intCounter = iStrSelektion + 1
3. In debug mode, I get 17 as the value of iStrSelektion (when I hover the
cursor over it), but I get zero (0) when I hover the cursor over intCounter
4. Why don't I get 18? What am I doing wrong?

Any ideas would be much appreciated.
Marc


Private Sub btnRemovSpcsNdInitCap_Click()
Me.Hide
'
Dim MyData As DataObject
Dim strClip As String

Dim strSelektion As String, strCkChar As String, strHoldChars As String
Dim iStrSelektion As Integer, intCounter As Integer

Selection.Range.Case = wdTitleWord
strSelektion = Selection
iStrSelektion = Len(strSelektion)
intCounter = iStrSelektion + 1
strCkChar = ""
strHoldChars = ""

Do While intCounter > 0
intCounter = intCounter - 1
strCkChar = Mid(strSelektion, intCounter, 1)
'MsgBox "strCkChar =" & strCkChar
If strCkChar = Chr(32) Then strCkChar = ""
If strCkChar = Chr(64) Then strCkChar = ""
If strCkChar = Chr(160) Then strCkChar = ""
strHoldChars = strHoldChars & strCkChar
Loop
'MsgBox strHoldChars
'Selection.TypeParagraph 'Hit Enter key
'Selection.EndKey Unit:=wdStory 'Ctrl-End, go to bottom
'Selection.TypeText Text:=strHoldChars

Set MyData = New DataObject

MyData.SetText strHoldChars
MyData.PutInClipboard
'
End Sub
 
M

Malcolm Smith

Marc

If you have stopped the execution on the line:

intCounter = iStrSelektion + 1

then you will have stopped the excecution before that line is processed so
intCounter will still be zero. If you go on one line more then you
should see the correct value.

One thing in your code which you ought to look at. It's the line:

strSelektion = Selection

Now, you are trying to pass a Selection object, which is a Range object
anyway, to a string. I would rather something like:

strSelektion = Selection.Text

If you are using Office 2000 and upwards then you could just as easily use
the Replace$() command to replace the characters that you don't wish to
have.

This would also make your code that little faster.

- Malc
 

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