ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Open One Word File from Excel (https://www.excelbanter.com/excel-programming/289287-open-one-word-file-excel.html)

stratuser

Open One Word File from Excel
 
Using Excel VBA, I am trying to copy data from one Excel
file to one Word file, and cycle back and forth between
the two files without opening a new Word file each time I
dump data from Excel into Word. When I use CreateObject,
I get a new instance of the Word file, but I don't know
how to use GetObject to activate an already open Word file
since the Word file is not saved yet and at this point is
just Document1. I don't want 20 instances of Word, just
one, with one Word file to contain all the dumped data.
Any ideas?

Jim[_39_]

Open One Word File from Excel
 
Hi Stratuser,

One idea is to use the sendkeys command while word is active.

You could also activate word with the send keys command.

It has worked for me in the past.

I hope that helps.

Jim



stratuser

Open One Word File from Excel
 
Isn't there any way to just activate an existing open Word
document from Excel without creating a new one?

-----Original Message-----
Hi Stratuser,

One idea is to use the sendkeys command while word is

active.

You could also activate word with the send keys command.

It has worked for me in the past.

I hope that helps.

Jim



chandlm[_12_]

Open One Word File from Excel
 
Stratuser,

you can try this.

Sub copyexceltoword()

Set wd = CreateObject("word.application")
wd.Application.Visible = True
wd.Application.Documents.Open "c:\matt.doc" ' Open your word document
Application.Visible = True

For a = 1 To 5

Application.Range("a" & a).Copy 'copy data from excel
wd.Selection.Paste 'Paste data into word
Next a



Set wd = Nothing 'Clear the memory. Very important.


End Sub


let me know if you need further help.


---
Message posted from http://www.ExcelForum.com/


stratuser

Open One Word File from Excel
 
Thanks, but that doesn't work either, because I don't want
to create a new instance of Word each time I run this
subroutine. If I use CreateObject("word.application") I
get a new instance every time I call the procedure.


-----Original Message-----
Stratuser,

you can try this.

Sub copyexceltoword()

Set wd = CreateObject("word.application")
wd.Application.Visible = True
wd.Application.Documents.Open "c:\matt.doc" ' Open your

word document
Application.Visible = True

For a = 1 To 5

Application.Range("a" & a).Copy 'copy data from excel
wd.Selection.Paste 'Paste data into word
Next a



Set wd = Nothing 'Clear the memory. Very important.


End Sub


let me know if you need further help.


---
Message posted from http://www.ExcelForum.com/

.


Chip Pearson

Open One Word File from Excel
 
Use GetObject. This will return an existing Word application. If
it returns Nothing, then use CreateObject to create a new
instance of Word. E.g.,

Dim WordObj As Object
Set WordObj = GetObject(, "Word.Application") 'note leading comma
If WordObj Is Nothing Then
Set WordObj = CreateObject("Word.Application")
End If



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"Stratuser" wrote in
message ...
Thanks, but that doesn't work either, because I don't want
to create a new instance of Word each time I run this
subroutine. If I use CreateObject("word.application") I
get a new instance every time I call the procedure.


-----Original Message-----
Stratuser,

you can try this.

Sub copyexceltoword()

Set wd = CreateObject("word.application")
wd.Application.Visible = True
wd.Application.Documents.Open "c:\matt.doc" ' Open your

word document
Application.Visible = True

For a = 1 To 5

Application.Range("a" & a).Copy 'copy data from excel
wd.Selection.Paste 'Paste data into word
Next a



Set wd = Nothing 'Clear the memory. Very important.


End Sub


let me know if you need further help.


---
Message posted from http://www.ExcelForum.com/

.




stratuser

Open One Word File from Excel
 
Thanks, that did the trick.

-----Original Message-----
Use GetObject. This will return an existing Word

application. If
it returns Nothing, then use CreateObject to create a new
instance of Word. E.g.,

Dim WordObj As Object
Set WordObj = GetObject(, "Word.Application") 'note

leading comma
If WordObj Is Nothing Then
Set WordObj = CreateObject("Word.Application")
End If



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"Stratuser" wrote in
message ...
Thanks, but that doesn't work either, because I don't

want
to create a new instance of Word each time I run this
subroutine. If I use CreateObject("word.application") I
get a new instance every time I call the procedure.


-----Original Message-----
Stratuser,

you can try this.

Sub copyexceltoword()

Set wd = CreateObject("word.application")
wd.Application.Visible = True
wd.Application.Documents.Open "c:\matt.doc" ' Open your

word document
Application.Visible = True

For a = 1 To 5

Application.Range("a" & a).Copy 'copy data from excel
wd.Selection.Paste 'Paste data into

word
Next a



Set wd = Nothing 'Clear the memory. Very important.


End Sub


let me know if you need further help.


---
Message posted from http://www.ExcelForum.com/

.



.



All times are GMT +1. The time now is 04:46 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com