Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > VBA / Office Integration > Word, PowerPoint, Outlook, and Other Office Products > Open Word file using an Excel Macro


Reply
 
Thread Tools Display Modes
  #1  
Old 01-07-2003, 04:42 AM
internationally
Guest
 
Posts: n/a
Default Open Word file using an Excel Macro


Hi All,

What VB command do I use to open a Word document from an Excel document? Then, within the same Excel macro, how do I initiate a Macro within the word document I have just opened?

Help is much appreciated,

internationally
Reply With Quote
  #2  
Old 01-07-2003, 07:53 AM
Wamphyri's Avatar
Wamphyri Wamphyri is offline
Variable not defined

Retired Moderator
* Guru *
 
Join Date: Apr 2002
Location: Ottawa, Ontario
Posts: 4,793
Default

You use the Run method to run the macro and the opening of a word document from excel is the same as opening a word document from vb6.

Remember to add a reference to Microsoft Word Object Library (see Tools -> References)
Code:
Dim wdApp As Word.Application Set wdApp = New Word.Application wdApp.Visible = True wdApp.Documents.Open FileName:="C:\yourdoc.doc" wdApp..Run "MyProject.MyModule.MyProcedure" wdApp.Quit Set wdApp = Nothing
__________________
-Carl
Reply With Quote
  #3  
Old 01-07-2003, 10:16 AM
internationally
Guest
 
Posts: n/a
Default Multiple pastes

Excellent - thanks.

I need to do this twice. But when I do the same thing twice it states that obviously I cannot open the file again and thus can only open it read only.

So the second time when I have copied the data, how should I access the file, which is already open? (paste further data in)

Thanks for your help,

internationally
Reply With Quote
  #4  
Old 01-07-2003, 11:03 AM
Wamphyri's Avatar
Wamphyri Wamphyri is offline
Variable not defined

Retired Moderator
* Guru *
 
Join Date: Apr 2002
Location: Ottawa, Ontario
Posts: 4,793
Default

If you are copying from and pasting to the same there is no need to open it a second time. Just copy and paste to the end of the document or where ever.

Code:
Dim myRange As Range Dim EPos As Long 'Select entire doc WdApp.Documents(1).Select WdApp.Selection.Copy 'You may have to use 'EPos = WdApp.Selection.End - 1 EPos = WdApp.Selection.End Set myRange = WdApp.Documents(1).Range(Start:=EPos, End:=EPos) myRange.Paste
__________________
-Carl
Reply With Quote
  #5  
Old 01-08-2003, 05:23 AM
internationally
Guest
 
Posts: n/a
Default further macro

I have noticed that rather than just have 2 basic macros (to paste the data) I could add further code to then tell my computer to go back to Excel after pasting in to Word. But how do I tell it to go back, from the Word file, to a particular Excel file (which is already open) and then initiate the macro in the Excel file?

I apologise for the change in macro choice,

internationally
Reply With Quote
  #6  
Old 01-08-2003, 07:27 AM
Wamphyri's Avatar
Wamphyri Wamphyri is offline
Variable not defined

Retired Moderator
* Guru *
 
Join Date: Apr 2002
Location: Ottawa, Ontario
Posts: 4,793
Default

I assume you are still writing this code within Excel, correct?
The code written in excel will only effect word when you use a variable that points to Excel.Application (directly or indirectly). In the examples above it would be any line of code with the variable WdApp in it. To return back to modifying the Excel worksheet just go back to using regular Excel code.

Running a macro in excel is the same as running a macro in word.

So for example.
Code:
'controling Word WdApp.Selection.Copy 'back to controling Excel 'You may wish to use the actual worksheet's name rather than a number Workbooks(1).Worksheets(1).Cells(1,1).Value = "Selection Copied" 'Run Excel Macro from Excel Application.Run("MyMacro")
__________________
-Carl
Reply With Quote
  #7  
Old 01-08-2003, 08:39 AM
internationally
Guest
 
Posts: n/a
Default

Wamphyri

In the above I have not used:

Dim myRange As Range
Dim EPos As Long
(and references to it)

1. because I dont know what they do
2. It works without

Perhaps there is a reason why they should be there?


The basic function of the macro works fine - But the final stage is formatting - After selecting the picture and going to "Format" on the top menu, then "Format picture..." there is no further the macro can go as the options are 'greyed-out'. I cannot choose any of the format styles. The one of which I want being the "In line with text".

How can I gain access or what coding can be used to enable this formatting using a macro - - - or are macros limited in terms of formatting capabilities?


Hope this sorts out every problem with this macro as its' looking pretty smart,

Thanks again,

internationally
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->