PDA

View Full Version : Solved: Interaction With Word From Excel



Ken1000
04-10-2006, 10:46 AM
Hello ... how are you today?

I am using Office 2003 and am trying to have an interaction between
Excel and Word. All of my macro code works except for one area.

I have successfully created code in Excel which
creates a new Word doc and pastes from Excel a
specific range. By doing this, that range now
exists in the Word Doc as a table. What I next am
trying to do (but am not successfull) is to create an
empty line directly below that new table and have the
cursor positioned at the very beginning of the empty
line.

I have tried these three lines of code, but nothing works:
:banghead:


'Selection.TypeParagraph

'Selection.GoTo What:=wdGoToLine, Which:=wdGoToAbsolute, Count:=5

'Selection.EndKey Unit:=wdStory

Below is the macro code in its entirety, with the "troublesome area" showing between the two lines with asterisks.

Could you please review my macro and tell me what changes are
needed for this line creation and cursor placement to work?

Thank you very much for your help.

Ken


Sub PlaceExcelRangeIntoWord()

Dim oWord As Word.Application
Dim oDoc As Word.Document

'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

'copy range from Excel
Range("Sheet1!A1:E4").Copy

'create new Word doc
oWord.Visible = True
oWord.Activate

Set oDoc = oWord.Documents.Add

'adjust margins
With ActiveDocument.PageSetup
.TopMargin = InchesToPoints(0.5)
.BottomMargin = InchesToPoints(0.5)
.LeftMargin = InchesToPoints(0.5)
.RightMargin = InchesToPoints(0.5)
End With

'paste Excel range into Word doc
oDoc.Range.Paste

'* * * * * * * * * * * * * *
'move to next empty line after the inserted range


'line below does not work
'Selection.GoTo What:=wdGoToLine, Which:=wdGoToAbsolute, Count:=5

Selection.EndKey Unit:=wdStory

'* * * * * * * * * * * * * *

'free up selection on Excel
Application.CutCopyMode = False

'save the Word doc and close
ActiveDocument.SaveAs Filename:="FizzButt1.doc"
oDoc.Close
oWord.Quit


'Make sure you release object references.

Set oWord = Nothing
Set oDoc = Nothing

End Sub

Ken1000
04-10-2006, 12:45 PM
Hi. After many trial and error attempts, I solved my dilemna.

I did try to mark this thread as SOLVED but could not find that option anywhere.

For those who are interested, I post below the code which works.

Thank you.

Ken



Dim oWord As Word.Application
Dim oDoc As Word.Document

Application.ScreenUpdating = False

'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
End If

Range("Sheet1!A1:E4").Select

oWord.Visible = False
oWord.Activate

Set oDoc = oWord.Documents.Add

With ActiveDocument.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientPortrait
.TopMargin = InchesToPoints(0.5)
.BottomMargin = InchesToPoints(0.5)
.LeftMargin = InchesToPoints(0.5)
.RightMargin = InchesToPoints(0.5)
.Gutter = InchesToPoints(0)
.HeaderDistance = InchesToPoints(0)
.FooterDistance = InchesToPoints(0)
.PageWidth = InchesToPoints(8.5)
.PageHeight = InchesToPoints(11)
.FirstPageTray = wdPrinterDefaultBin
.OtherPagesTray = wdPrinterDefaultBin
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = False
.MirrorMargins = False
.TwoPagesOnOne = False
.BookFoldPrinting = False
.BookFoldRevPrinting = False
.BookFoldPrintingSheets = 1
.GutterPos = wdGutterPosLeft
End With

Selection.Copy
oWord.Selection.Paste

'save the Word Doc and minimize it
oDoc.SaveAs Filename:="FizzButt1.doc"

oWord.WindowState = wdWindowStateMinimize

'Make sure you release object references
Set oWord = Nothing
Set oDoc = Nothing

Range("Sheet1!A1").Select
Sheets("Sheet1").Activate

lucas
04-11-2006, 07:00 AM
Hi Ken,
Sorry no one came along in time to help with this but your resolved it in record time. Thanks for posting your solution. I hope you don't mind that I enclosed your code in vba tags for easier reading and I will mark the thread solved for you as the board is going through an upgrade and that function is not working yet.