View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Dave Peterson[_3_] Dave Peterson[_3_] is offline
external usenet poster
 
Posts: 2,824
Default Copy And Paste From Excel To Word

Try changing this portion:

For i = 1 To 3
str = ws.Cells(r, i).Value
With WdApp
.Selection.GoTo What:=wdGoToBookmark, Name:="bkmk" & i
.Selection.TypeText Text:=str
End With
Next i

to:
With WdApp

.Selection.GoTo What:=wdGoToBookmark, Name:="WorkOrder"
.Selection.TypeText Text:=ws.cells(r,1).value

.Selection.GoTo What:=wdGoToBookmark, Name:="ServiceOrder"
.Selection.TypeText Text:=ws.cells(r,3).value

.Selection.GoTo What:=wdGoToBookmark, Name:="OnCor"
.Selection.TypeText Text:=ws.cells(r,2).value

End With

Kris Taylor wrote:

Hi All!

I have deciphered the code to open a word document from excel, however
I
am unaware of how to copy and paste different cells at a time.

Here is a general run down of what I wish for the macro to do.

First, in excel on the active worksheet titled Tecumseh, I want to use
ctrl+down in column A to go to the last entry, copy that cell and
paste it into word at a bookmark titled WorkOrder. I then want to go
right 2
cells to column C and copy and paste that cell into another bookmark
titled ServiceOrder in word. Finally go to column B and copy/paste
that cell in yet another bookmark titled Oncor in word!

If possible please post a similar code for what is required that I
could
play around with a little.

I received this code from Developpers Dex, however it doesn't seem to
work for me. All it does is create a pop up that refers to the word
document that I want opened.

=======================================
Sub CopyToWord()
Dim ws As Worksheet
Dim r As Long
Dim i As Integer
Dim WdApp As Object
Dim str As String
Dim strFile As String
Dim doc As Object
Set ws = Sheets("Sheet1")
r = ws.Cells(Rows.Count, 1).End(xlUp).Row
strFile = "C:\Data\Test.doc"
On Error Resume Next
Set WdApp = GetObject(, "Word.Application")
If Err.Number < 0 Then
Err.Clear
Set WdApp = CreateObject("Word.Application")
End If

WdApp.Documents.Open Filename:=strFile, _
ConfirmConversions:=False, ReadOnly:=False
Set doc = WdApp.activedocument
MsgBox doc.Name
WdApp.Visible = True
For i = 1 To 3
str = ws.Cells(r, i).Value
With WdApp
.Selection.GoTo What:=wdGoToBookmark, Name:="bkmk" & i
.Selection.TypeText Text:=str
End With
Next i
doc.Close SaveChanges:=wdSaveChanges
Set WdApp = Nothing

End Sub
==========================================

Thanks in advance for all those who took the time to read this!

Thanks,

Kris
www.questofages.org


--

Dave Peterson