Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-10-2015, 07:09 AM
alexolcz alexolcz is offline Row Number of Selected Cell in Table Windows 7 64bit Row Number of Selected Cell in Table Office 2010 64bit
Novice
Row Number of Selected Cell in Table
 
Join Date: Dec 2015
Posts: 1
alexolcz is on a distinguished road
Default Row Number of Selected Cell in Table

Hi

I am trying to write a VBA program to return the row number of the current selection. This would allow me to copy and paste the entire row which is found searching for a key word.

The code I have written so far seems to return the cell that the cursor was in before the code is run, and not the cell selected using Find.

Many thanks in advanced for any help with this,
Alex

Sub RowNumber()



Dim wApp As Word.Application
Dim wDoc As Word.Document
Dim i As Integer

'OPEN WORD IF NOT OPEN
Dim wObj As Word.Application
On Error Resume Next

' Get existing instance of Word if it exists.
Set wApp = GetObject(, "Word.Application")

If Err <> 0 Then
' If GetObject fails, then use CreateObject instead.
Set wApp = CreateObject("Word.Application")
End If

wApp.Visible = True

Set wDoc = wApp.Documents.Open("C:\Users\alex.olczak\Document s\Proposal Automation\ERS Proposal\ERS Proposal Template Edit\PeopleParagraphMacro\TableTest.docx")


'wDoc.Application.Selection.Find.ClearFormatting
wDoc.Application.Selection.Find.Replacement.ClearF ormatting
wDoc.Application.Selection.Find.Replacement.Highli ght = True
With wDoc.Application.Selection.Find
.Text = "H"
.Replacement.Text = "H"
.Forward = Flase
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With



wDoc.Application.Selection.Find.Execute Replace:=wdReplaceAll
wDoc.Application.Selection.Find.Execute
wDoc.Application.Options.DefaultHighlightColorInde x = wdYellow


a = wDoc.Application.Selection.Range.Information(wdEnd OfRangeRowNumber)
a = wDoc.Application.Selection.Information(wdEndOfRang eRowNumber)
'wDoc.Application.Selection.GoTo

MsgBox a

End Sub
Reply With Quote
  #2  
Old 12-10-2015, 01:57 PM
macropod's Avatar
macropod macropod is offline Row Number of Selected Cell in Table Windows 7 64bit Row Number of Selected Cell in Table Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

See: https://www.msofficeforums.com/word-...addresses.html
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 12-10-2015, 09:54 PM
gmayor's Avatar
gmayor gmayor is offline Row Number of Selected Cell in Table Windows 7 64bit Row Number of Selected Cell in Table Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

If this is Word VBA, the you don't have to get or create the Word object. You are already in Word. If the aim is to find the row number of the first instance of a cell containing the upper case letter 'H', then
Code:
Sub Macro1()

Dim wDoc As Document
Dim oRng As Range
Dim i As Integer
    Set wDoc = wApp.Documents.Open("C:\Users\alex.olczak\Document s\Proposal Automation\ERS Proposal\ERS Proposal Template Edit\PeopleParagraphMacro\TableTest.docx")
    Set oRng = wDoc.Range
    With oRng.Find
        Do While .Execute(FindText:="H", MatchCase:=True)
            If oRng.Information(wdWithInTable) Then
                oRng.HighlightColorIndex = wdYellow
                i = oRng.Rows(1).Index 'This line is not required to copy the row
                MsgBox i 'This line is not required to copy the row
                oRng.Rows(1).Range.Copy 'The row is copied to the clipboard
                Exit Do        'Find only the first instance
            End If
        Loop
    End With
    'Do something with the copied row e.g.
    Set oRng = wDoc.Range 'set a range to the document
    oRng.Collapse 0 'collapse the range to its end
    oRng.Paste 'Paste the found row at then end of the document
lbl_Exit:
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
Reply

Tags
find, table, vba



Similar Threads
Thread Thread Starter Forum Replies Last Post
Data validation,force cell to be filed with number if respective cell is not empty nicholes Excel Programming 0 08-01-2015 09:08 AM
Row Number of Selected Cell in Table Entering a number in a cell causes it to fill with the color of a nearby cell Bill Martz Excel 1 04-23-2015 07:57 PM
Setting a particular table cell background color when an option button is selected in Word 2007 a888 Word VBA 11 03-25-2015 05:33 AM
Pivot Table Question- Can the table display in the order the boxes were checked/selected? blackjack Excel 0 09-13-2014 08:57 PM
Row Number of Selected Cell in Table Heading row disappears from table styles when pasted table is selected andrewballem Word Tables 2 11-12-2013 05:18 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:18 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft