PDA

View Full Version : Sleeper: Word object in Excel



sangeeta
06-10-2005, 12:55 AM
Hi All,
I am using the folowing piece of code to insert data in word table fom excel.


Set WordApp = CreateObject("Word.Application")
With WordApp
.Documents.Open "C:\Documents and Settings\sb008078\Desktop\AUTOMATION\Automating word\test.doc"
.Visible = True
.WindowState = wdWindowStateMaximize
.Selection.GoTo What:=wdGoToBookmark, Name:="bk1"
.Selection.MoveRight Unit:=wdCell
.Selection.Find.ClearFormatting
For Rindex = 2 To 5
For Cindex = 2 To 3
.Selection.TypeText text:=Worksheets("Sheet 1").Cells(Rindex, Cindex).Value
.Selection.MoveRight Unit:=wdCell
Next Cindex
.Selection.MoveDown Unit:=wdLine
.Selection.MoveLeft Unit:=wdCell, Count:=4
Next Rindex

i have to do this many times with different row numbers and column numbers.
i thought of writing a sub with these values as paramaters. but i'm stuck at this point: the selection being used is word selection.(as you must have noticed ,i'm using .selection) how do i pass this to the sub?
hope i have made my question clear!
thanks in advance.

Jacob Hilderbrand
06-14-2005, 10:14 PM
Is the bookmark name all that is changing? Basically you can do something like this:


Sub Macro1(FName as String, BookMarkName As String)

Set WordApp = CreateObject("Word.Application")
With WordApp
.Documents.Open FName
.Visible = True
.WindowState = wdWindowStateMaximize
.Selection.GoTo What:=wdGoToBookmark, Name:=BookMarkName
.Selection.MoveRight Unit:=wdCell
.Selection.Find.ClearFormatting
For Rindex = 2 To 5
For Cindex = 2 To 3
.Selection.TypeText text:=Worksheets("Sheet 1").Cells(Rindex, Cindex).Value
.Selection.MoveRight Unit:=wdCell
Next Cindex
.Selection.MoveDown Unit:=wdLine
.Selection.MoveLeft Unit:=wdCell, Count:=4
Next Rindex
End Sub

In this example you need to send the file name (and path) and the bookmark name when calling the Sub.



Call Macro1("C:\Documents and Settings\sb008078\Desktop\AUTOMATION\Automating word\test.doc","bk1")