Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Using an Excel macro to open a Word File

Can someone please help me with an example of what the code would look like
to open a Word file from a macro in Excel? Is it better to use the shell or
the createobject function?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,163
Default Using an Excel macro to open a Word File

In my opinion, neither: use automation with the Word object library. Add the
reference to Word to your project and then:

Dim WordApp as Word.Application
Dim WordDoc as Word.Document

Set WordApp = New Word.Application
Set WordDoc = WordApp.Documents.Open("C:\Blah\Blah\MyDoc.doc")
....
WordDoc.Close
WordApp.Quit

--
- K Dales


"lhyer" wrote:

Can someone please help me with an example of what the code would look like
to open a Word file from a macro in Excel? Is it better to use the shell or
the createobject function?

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Using an Excel macro to open a Word File

If you want to control the word document, createobject seems better.

If you just want to open the document, you could use shell or even
followhyperlink().

ActiveWorkbook.FollowHyperlink Address:="c:\path\name.doc"

An example saved from a previous post:

Option Explicit
Sub Testme()

Dim WDApp As Object
Dim WDDoc As Object
Dim myDocName As String

myDocName = "s:\lost property master sheets\sheet3.doc"

Set WDApp = CreateObject("Word.Application")
WDApp.Visible = True 'at least for testing!

Set WDDoc = WDApp.documents.Open(Filename:=myDocName)
WDDoc.PrintOut '.printPreview while testing???
WDDoc.Close savechanges:=False

WDApp.Quit

Set WDDoc = Nothing
Set WDApp = Nothing

End Sub





lhyer wrote:

Can someone please help me with an example of what the code would look like
to open a Word file from a macro in Excel? Is it better to use the shell or
the createobject function?


--

Dave Peterson
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,163
Default Using an Excel macro to open a Word File

Dave: Curious - what is the advantage of createobject over the method I have
been using? I have been using it under an assumption that it was always
better to use a more explicit reference to an object rather than more generic
methods of creating and referencing them, and that createobject was a
"weaker" way of doing this, but that was only an assumption and not based on
a very extensive knowledge of the technical aspects behind both methods -
perhaps you can illuminate me?
--
- K Dales


"Dave Peterson" wrote:

If you want to control the word document, createobject seems better.

If you just want to open the document, you could use shell or even
followhyperlink().

ActiveWorkbook.FollowHyperlink Address:="c:\path\name.doc"

An example saved from a previous post:

Option Explicit
Sub Testme()

Dim WDApp As Object
Dim WDDoc As Object
Dim myDocName As String

myDocName = "s:\lost property master sheets\sheet3.doc"

Set WDApp = CreateObject("Word.Application")
WDApp.Visible = True 'at least for testing!

Set WDDoc = WDApp.documents.Open(Filename:=myDocName)
WDDoc.PrintOut '.printPreview while testing???
WDDoc.Close savechanges:=False

WDApp.Quit

Set WDDoc = Nothing
Set WDApp = Nothing

End Sub





lhyer wrote:

Can someone please help me with an example of what the code would look like
to open a Word file from a macro in Excel? Is it better to use the shell or
the createobject function?


--

Dave Peterson

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Using an Excel macro to open a Word File

Say you use xl2003 and your users are using xl97-xl2003.

When you explicitly set the reference, then those who don't have the same
version of office as you could have trouble (always have trouble???).

But I would use the reference while developing the code. I get all those
intellisense and autocomplete features in the VBE.

But when I'm ready to release it to others, I'll get rid of the references,
declare any constants I used and fix my DIMs to be more generic (As Object).

I don't think it's weaker--it'll probably be a bit slower (but I can't notice
the difference).

K Dales wrote:

Dave: Curious - what is the advantage of createobject over the method I have
been using? I have been using it under an assumption that it was always
better to use a more explicit reference to an object rather than more generic
methods of creating and referencing them, and that createobject was a
"weaker" way of doing this, but that was only an assumption and not based on
a very extensive knowledge of the technical aspects behind both methods -
perhaps you can illuminate me?
--
- K Dales

"Dave Peterson" wrote:

If you want to control the word document, createobject seems better.

If you just want to open the document, you could use shell or even
followhyperlink().

ActiveWorkbook.FollowHyperlink Address:="c:\path\name.doc"

An example saved from a previous post:

Option Explicit
Sub Testme()

Dim WDApp As Object
Dim WDDoc As Object
Dim myDocName As String

myDocName = "s:\lost property master sheets\sheet3.doc"

Set WDApp = CreateObject("Word.Application")
WDApp.Visible = True 'at least for testing!

Set WDDoc = WDApp.documents.Open(Filename:=myDocName)
WDDoc.PrintOut '.printPreview while testing???
WDDoc.Close savechanges:=False

WDApp.Quit

Set WDDoc = Nothing
Set WDApp = Nothing

End Sub





lhyer wrote:

Can someone please help me with an example of what the code would look like
to open a Word file from a macro in Excel? Is it better to use the shell or
the createobject function?


--

Dave Peterson


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Word and Excel crash when I try to open a folder to get to a file DGD41 Excel Discussion (Misc queries) 7 April 15th 06 09:14 PM
Open Word Doc From Excel Macro MATT Excel Discussion (Misc queries) 4 December 14th 04 12:09 AM
open Word from excel Macro hurst,j Excel Worksheet Functions 0 December 2nd 04 03:27 PM
Open One Word File from Excel Stratuser Excel Programming 6 January 27th 04 06:37 PM
Graph Excel Selection, Open Word File, Embed Graph Into Word Steve Excel Programming 0 November 17th 03 05:35 PM


All times are GMT +1. The time now is 04:09 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"