Word Verses Excel Macro

J

Joel

I'm trying to understand differences between excel and word macros. Trying to
get Last sentence before first table in a document. I got the word macro to
run correctly. I tried applying same code to Excel macro and MoveUp fails.
I posted both macro below. Can somebody explain?????????????????

Word Macro - Works Correctly
-----------------------------------------------------------

Sub getlastline()

With ActiveDocument
Set FirstTable = .Tables(1)
Set FirstCell = FirstTable.Rows(1).Cells(1)
FirstCell.Select
Selection.MoveUp unit:=wdLine, Count:=1

Selection.Move unit:=wdSentence, Count:=-1
Selection.Sentences(1).Select
End With

End Sub

Excel Code - Move Up fails
----------------------------------------------------------------


Sub getlastline()

Set WdObj = CreateObject("Word.Application")
WdObj.Visible = True
Set WDDoc = WdObj.Documents.Open(Filename:="c:\temp\table test.doc")

With WDDoc
Set FirstTable = .Tables(1)
Set FirstCell = FirstTable.Rows(1).Cells(1)
FirstCell.Select
Selection.MoveUp unit:=wdLine, Count:=1

Selection.Move unit:=wdSentence, Count:=-1
Selection.Sentences(1).Select
End With

End Sub
 
D

Doug Robbins - Word MVP

Use something like:

Dim myrange As Range
With ActiveDocument
Set myrange = .Range
myrange.End = .Tables(1).Range.Start
End With
myrange.Sentences.Last.Select
Set myrange = Selection.Range
myrange.End = myrange.Paragraphs(1).Range.End
myrange.Select


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
J

Joel

I have the same problem with you code as mine. It works in word but doesn't
work in excel. With youcode it doesn't like the "end". gives error
"Arguement not optional".

Excel expects for end a parameter and word doesn't require one. I n Excel

LastRow = Range("A" & Rows.Count).end(xlup).Row

Even if I get rid of the compiling error by doing this

WDDoc.Application.myrange.End = .Tables(1).Range.Start

The line doesn't execute.
 
D

Doug Robbins - Word MVP

I believe that the issue is in the use of the selection object

The following code run from Excel selects the same area of the document as
running the Word code:

Sub getlastline()

Dim oWord As Word.Application
Dim WordWasNotRunning As Boolean
Dim oDoc As Word.Document
Dim trange As Word.Range
'Get existing instance of Word if it's open; otherwise create a new one

On Error Resume Next

Set oWord = GetObject(, "Word.Application")
If Err Then
Set oWord = New Word.Application
WordWasNotRunning = True
End If

On Error GoTo Err_Handler

oWord.Visible = True
oWord.Activate
Set oDoc = oWord.Documents.Open(Filename:="c:\documents\tabletest.docx")
Set trange = oDoc.Tables(1).Range
trange.Start = trange.Start - 1
trange.Collapse wdCollapseStart
trange.Move wdSentence, -1
trange.Sentences(1).Select

If WordWasNotRunning Then
oWord.Quit
End If

'Make sure you release object references.

Set oWord = Nothing
Set oDoc = Nothing
Set myDialog = Nothing

'quit
Exit Sub

Err_Handler:
MsgBox "Word caused a problem. " & Err.Description, vbCritical, "Error:
" _
& Err.Number
If WordWasNotRunning Then
oWord.Quit
End If

End Sub


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
T

Tony Jollans

I believe that the issue is in the use of the selection object

Indeed - without qualification, Selection refers to Excel's Selection, not
Word's. As does Range.

This should make your original macro work:

Sub getlastline()

Set WdObj = CreateObject("Word.Application")
WdObj.Visible = True
Set WDDoc = WdObj.Documents.Open(Filename:="c:\temp\table test.doc")

With WDDoc
Set FirstTable = .Tables(1)
Set FirstCell = FirstTable.Rows(1).Cells(1)
FirstCell.Select
WdObj.Selection.MoveUp unit:=wdLine, Count:=1

WdObj.Selection.Move unit:=wdSentence, Count:=-1
WdObj.Selection.Sentences(1).Select
End With

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top