Solved

How can I open a blank MS Word Document and MS Excel Document from a template using shell function?

Posted on 2004-09-13
18
193 Views
Last Modified: 2010-05-02
I wish to open a blank MS Word Doc and MS Excel Document based on templates. I have worked out how to open a blank document/worksheet using the following code:

Shell ("C:\Program Files\Microsoft Office\Office\WINWORD.EXE") or for Excel "\EXCEL.EXE"

Is there an additional line of code which allows me to open a blank document/worksheet based on a template?

Thank you
0
Comment
Question by:GarethWham
  • 7
  • 5
  • 3
  • +3
18 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 12042337
For Word you use the /t switch
Shell ("C:\Program Files\Microsoft Office\Office\WINWORD.EXE /tTemplateName")
0
 

Author Comment

by:GarethWham
ID: 12042592
Thanks Graham,

However I put the template name in and a message box is displayed saying file not found. Any suggestions?

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 12042632
It needs the full path of the template and it's best to put it in quotes in case there are spaces in the path string.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:GarethWham
ID: 12043274
Cheers,

That works fine. Do you know how to make the document visible? At the moment it opens fine but is minimised.
0
 
LVL 3

Expert Comment

by:gillgates
ID: 12043313

Shell ("C:\Program Files\Microsoft Office\Office\WINWORD.EXE /tTemplateName", vbMaximizedFocus)


See this link for other options...

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbenlr98/html/vafctShell.asp
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 12043325
Try Shell(...,1)
0
 
LVL 7

Expert Comment

by:_agj_
ID: 12043944
0
 
LVL 2

Expert Comment

by:Lethal_J
ID: 12044059
Shell ("C:\Program Files\Microsoft Office\Office\WINWORD.exe C:\Program Files\Microsoft Office\Office\(location of the template file)", vbMaximizedFocus)

Basically, to break that statement down, it is as follows:

Shell ("PATH TO APPLICATION then FILE TO OPEN", SET THE FOCUS)
0
 
LVL 19

Expert Comment

by:RanjeetRain
ID: 12044595
I would rather suggest using OLE automation. That is more efficient and provides much greater control including using templates.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 12044682
Yes, I agree Ranjeet, but this is Gareth's first question & he may not be ready for that.
0
 

Author Comment

by:GarethWham
ID: 12045036
Yes, I am not sure about OLE.

Does anyone know how to open an excel template. I have tried the same process as Word but have been unsuccessful. Thank for your help so far
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 12045135
If you want to create a new document in Excel, this is where the OLE automation (COM) will be necessary:

Private Sub Command2_Click()
Dim xlApp As Excel.Application
Dim xlb As Excel.Workbook
Set xlApp = New Excel.Application
xlApp.Visible = True
Set xlb = xlApp.Workbooks.Add("TemplatePath")


End Sub

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 12045173
Forgot to say that you should set a reference to the Microsoft Excel library.
0
 

Author Comment

by:GarethWham
ID: 12046534
Now you have written the code I know how to implement OLE automation.
I will try this and see what happens thanks
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 12046971
I was trying to keep it simple, because I thought you had a particular need to use the Shell function.

However, now that we're going down tha route, a bit of advice:

Apart from how you get the Application object, the code is virtually the same as in VBA, where it is called Application, and is the default object.

If you haven't written any VBA, you can get to an application's IDE by opening the application and pressing Alt-F11
0
 

Author Comment

by:GarethWham
ID: 12049037
Ok thanks for the advice. I am building a user interface which offers the user various templates for them to open, fill out, then save to their hard drive.

Will try OLE
0
 
LVL 19

Expert Comment

by:RanjeetRain
ID: 12049309
Something like this would be simple enough!


public sub Sleep(byval duration as integer)
   dim x as integer
   for x = 1 to duration * 10000
        doevents
   next x
end sub


0
 
LVL 19

Expert Comment

by:RanjeetRain
ID: 12049310
Sorry!rong thread! Please ignore my last post :)
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now