Solved

Word 2010 - VBA - Cutting a string of text up to the TAB character

Posted on 2014-01-01
3
1,070 Views
Last Modified: 2014-01-06
I have a string of text that ends with a TAB character and would like to select and cut this string of text.  (without the tab character!)
My cursor is at the beginning of the line
What is the VBA code to do this?
0
Comment
Question by:brothertruffle880
  • 2
3 Comments
 
LVL 59

Assisted Solution

by:Chris Bottomley
Chris Bottomley earned 333 total points
ID: 39749698
Personally I'd use split i.e.

split(activedocument.Paragraphs(1).Range.Text, vbtab)(0)

Open in new window


Chris
0
 
LVL 59

Assisted Solution

by:Chris Bottomley
Chris Bottomley earned 333 total points
ID: 39749702
Within the stated scenario then this could be as easy as:

Sub splitPara()
Dim rng As Range
Dim str As String

    Set rng = Selection.Paragraphs(1).Range
    str = Split(rng.Text, vbTab)(0)
    MsgBox str
End Sub

Open in new window


Chris
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 167 total points
ID: 39749722
If you actually want to do a Cut operation on the paragraph, you could do it like this
Sub CutToTab()
    Dim rng As Range

    Set rng = Selection.Range
    rng.MoveEndUntil vbTab
    rng.MoveEnd wdCharacter, 1
    rng.Cut
End Sub

Open in new window

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question