Solved

Visual Studio search word table and return Cell index

Posted on 2016-11-04
8
77 Views
Last Modified: 2016-11-07
Hi,

I have an application in Visual Studio I'm using to connect to WORD in order to search a table and return the Row index of where the result was found, it is searching project numbers from a datagridview in a seperate word document and returning some values back in some new columns I added, I found a way to do this but I feel it's slow because it has to scan each row in the WORD table, I feel like this would go faster if instead I told word to FIND the project number and return the row index instead

Is it then possible to ask word to search for text in a table and return back the row index it found the text in?

My current code is as follows:

        Me.Cursor = System.Windows.Forms.Cursors.AppStarting
        Dim col As New DataGridViewTextBoxColumn
        col.DataPropertyName = "PropertyName"
        col.HeaderText = "Terrain"
        col.Name = "Terrain"
        DataGridView1.Columns.Add(col)


        Dim col1 As New DataGridViewTextBoxColumn
        col1.DataPropertyName = "PropertyName"
        col1.HeaderText = "Labo"
        col1.Name = "Labo"
        DataGridView1.Columns.Add(col1)


        Dim col2 As New DataGridViewTextBoxColumn
        col2.DataPropertyName = "PropertyName"
        col2.HeaderText = "Rapport"
        col2.Name = "Rapport"
        DataGridView1.Columns.Add(col2)

        Dim col3 As New DataGridViewTextBoxColumn
        col3.DataPropertyName = "PropertyName"
        col3.HeaderText = "Date fin rédaction"
        col3.Name = "fin"
        DataGridView1.Columns.Add(col3)


        DataGridView1.Columns("Terrain").DisplayIndex = 1
        DataGridView1.Columns("Labo").DisplayIndex = 2
        DataGridView1.Columns("Rapport").DisplayIndex = 3
        DataGridView1.Columns("fin").DisplayIndex = 4
        DataGridView1.Columns("fin").Width = 150

        Dim appWord As New Word.Application
        appWord.Visible = False
        Dim docWord As New Word.Document
        Dim myFile = Directory.GetFiles("S:\mike2\work\projects").OrderByDescending(Function(f) New FileInfo(f).LastWriteTime).First()
        docWord = appWord.Documents.Add(myFile.Split(".")(0) & ".doc")
        Dim tbl As Word.Table = docWord.Tables(1)
        For x = 0 To DataGridView1.Rows.Count - 1

            If docWord.Content.Find.Execute(DataGridView1.Item(11, x).Value) = True Then
                For i = 3 To tbl.Range.Rows.Count
                    If tbl.Cell(i, 1).Range.Text.Substring(0, tbl.Cell(i, 1).Range.Text.Length - 1).Trim = DataGridView1.Item(11, x).Value Then
                        DataGridView1.Item(76, x).Value = tbl.Cell(i, 4).Range.Text.Substring(0, tbl.Cell(i, 4).Range.Text.Length - 1).Trim & "%"
                        DataGridView1.Item(78, x).Value = tbl.Cell(i, 6).Range.Text.Substring(0, tbl.Cell(i, 6).Range.Text.Length - 1).Trim & "%"
                        DataGridView1.Item(77, x).Value = tbl.Cell(i, 5).Range.Text.Substring(0, tbl.Cell(i, 5).Range.Text.Length - 1).Trim & "%"
                        DataGridView1.Item(79, x).Value = tbl.Cell(i, 7).Range.Text.Substring(0, tbl.Cell(i, 7).Range.Text.Length - 1)
                        Exit For
                    End If
                Next
            Else
            End If

        Next
        appWord.Quit()
        Me.Cursor = System.Windows.Forms.Cursors.Arrow

Open in new window

0
Comment
Question by:FCapo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 41874307
HI,

you could use

Range.Information(wdEndOfRangeRowNumber)
'or
Range.Information(wdStartOfRangeRowNumber)

Open in new window

Regards
0
 

Author Comment

by:FCapo
ID: 41874314
Hi,

I added this to test :  MsgBox(docWord.Range.Information(Word.WdInformation.wdEndOfRangeRowNumber))

and it just keeps giving me back -1 everytime
0
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 41874350
can you test if you are really in the table

Range.Information(wdWithInTable)

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 76

Expert Comment

by:GrahamSkan
ID: 41874359
Make the Find range that of the table, not the whole document
This was edited in VBA, so might have to be tweaked for .Net

Dim rng As Word.Range
Dim rw As Word.Row
'...

        rng = tbl.Range
        With rng.Find
            .Text = DataGridView1.Item(11, X).Value
            If .Execute Then
                rw = rng.Rows(1) 'Within the Find With block,rng acts as the found range
                DataGridView1.Item(76, X).Value = GetCellText(rw.Cells(4)) & "%"
                DataGridView1.Item(78, X).Value = GetCellText(rw.Cells(6)) & "%"
                DataGridView1.Item(77, X).Value = GetCellText(rw.Cells(5)) & "%"
                DataGridView1.Item(79, X).Value = GetCellText(rw.Cells(7))
                Exit For
                
            Else
            End If
        End With

Open in new window


Here is the code to return the text of a table cell
Function GetCellText(cl As Word.Cell) As String
    Dim rng As Word.Range
    
    Set rng = cl.Range
    rng.MoveEnd wdCharacter, -1
    GetCellText = rng.Text
End Function

Open in new window

0
 

Author Comment

by:FCapo
ID: 41874390
Rgonzo1971 I tested with  MsgBox(docWord.Content.Information(Word.WdInformation.wdWithInTable)) and the result back is FALSE
0
 

Author Comment

by:FCapo
ID: 41874439
Hi GrahamSkan,

I tried your method and I'm getting the following error :
Additional information: Cannot access individual rows in this collection because the table has vertically merged cells.
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 41875307
Oh dear. I hadn't expected that.
Never mind, we can use still use the row number from the RowIndex property. This version assumes that the cells to be addressed haven't been merged away.
Dim rng As Word.Range
Dim r As Integer
'...

        rng = tbl.Range
        With rng.Find
            .Text = DataGridView1.Item(11, X).Value
            If .Execute Then
                r = rng.Cells(1).RowIndex 'within the Find With block,rng acts as the found range
                DataGridView1.Item(76, X).Value = GetCellText(tbl.Cell(r, 4)) & "%"
                DataGridView1.Item(78, X).Value = GetCellText(tbl.Cell(r, 6)) & "%"
                DataGridView1.Item(77, X).Value = GetCellText(tbl.Cell(r, 5)) & "%"
                DataGridView1.Item(79, X).Value = GetCellText(tbl.Cell(r, 7))
                Exit For
                
            Else
            End If
        End With

Open in new window

0
 

Author Closing Comment

by:FCapo
ID: 41877763
Thanks GrahamSkan, great solution to my problem!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
API v SOA 8 41
microsoft Accesss Basic Coding 44 65
YouTube API get the Video Tags - vb.net 7 32
Need to export the list of AD users with below attrubutes 2 19
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

733 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