PDA

View Full Version : Simple code to store first multiple characters into variable?



l0aded
11-20-2014, 03:40 PM
Right now I have a code that uses this which stores the first word in paragraph 30

counter = ActiveDocument.Paragraphs(n). _
Range.Words(1)

Is there a way to write it so that it stores only the first 6 characters of that word into a variable?
So for example if first word is Mcondalds, counter would be equal to "Mcdona"?

Thanks.

gmaxey
11-20-2014, 03:52 PM
If Len(ActiveDocument.Paragraphs(n).Range.Words(1)) >= 7 Then
counter = VBA.Left(ActiveDocument.Paragraphs(n).Range.Words(1), 7)
Else
counter = ActiveDocument.Paragraphs(n).Range.Words(1)
End If
,

l0aded
11-20-2014, 03:57 PM
If Len(ActiveDocument.Paragraphs(n).Range.Words(1)) >= 7 Then
counter = VBA.Left(ActiveDocument.Paragraphs(n).Range.Words(1), 7)
Else
counter = ActiveDocument.Paragraphs(n).Range.Words(1)
End If
,

Thanks!

macropod
11-20-2014, 09:49 PM
Is there a way to write it so that it stores only the first 6 characters of that word into a variable?
So for example if first word is Mcondalds, counter would be equal to "Mcdona"?
Do you really want the characters re-arranged? :tease:

If not:

Sub Demo()
Dim StrTmp As String
StrTmp = Left(Trim(ActiveDocument.Words.First), 6)
End Sub