Click here to Skip to main content
12,505,104 members (59,328 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB VB.NET
I am processing 270 books of poetry in word documents in vb.net using word automation.

One of the problems is locating anomalous lines that have between 5 and 12 tabs at the beginning. These have to be processed separately.

However, when I open one of the documents in vb.net using word automation, I cannot find a way to locate these lines. I have looked for tabs using "^t", ChrW(9) and ControlChars.Tab. I even tried typing a tab on the keyboard between quotes. None of them find the line with all the tabs. Using the same code with "A" or anything similar as a search term works fine.

Here's a snippet of the code:


Dim rng As Word.Range = docWord.Range()
 
rng.Select()
 
Dim fnd As Word.Find = rng.Find
 
' Clear existing formatting.
fnd.ClearFormatting()
 
' Execute the search
fnd.Text = "^t"
Can anyone help me with why this doesn't work?

Thanks very much
Posted 19-Oct-11 10:37am
Updated 19-Oct-11 10:40am
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Try using vbTabs[^].
  Permalink  
Comments
Dhruva Hein 20-Oct-11 7:29am
   
Thanks, Andre. Unfortunately, vbTab doesn't work any better than ControlChars.Tab.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Take a look at this code (from MS Word):
Option Explicit
 
Sub FindTabs()
 
Dim doc As Document
Dim rng As Range, fnd As Range
 
On Error GoTo Err_FindTabs
 
Set doc = ActiveDocument 'or Documents(docname) or Documents.Open(FullFilename)
Set rng = doc.Range
 
With rng.Find 
    .ClearFormatting
    .Text = "^t"
    .MatchWildcards = True
    .Forward = True
    Do While .Execute()
        Set fnd = rng.Find.Parent
        fnd.Underline = wdUnderlineDouble 'underline tab range
    Loop
End With
 
Exit_FindTabs:
    On Error Resume Next
    Set fnd = Nothing
    Set rng = Nothing
    Set doc = Nothing
    Exit Sub
 
Err_FindTabs:
    MsgBox Err.Description, vbExclamation, Err.Number
    Resume Exit_FindTabs
End Sub
To use it in Vb.Net, you need to create instance of MS Word Application - like this:
wrdApp = CreateObject("Word.Application")
To change wdUnderline constant to its value, see here
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160919.1 | Last Updated 20 Oct 2011
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100