Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 239
  • Last Modified:

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

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
GarethWham
Asked:
GarethWham
  • 7
  • 5
  • 3
  • +3
1 Solution
 
GrahamSkanCommented:
For Word you use the /t switch
Shell ("C:\Program Files\Microsoft Office\Office\WINWORD.EXE /tTemplateName")
0
 
GarethWhamAuthor Commented:
Thanks Graham,

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

0
 
GrahamSkanCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
GarethWhamAuthor Commented:
Cheers,

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

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
 
GrahamSkanCommented:
Try Shell(...,1)
0
 
_agj_Commented:
0
 
Lethal_JCommented:
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
 
RanjeetRainCommented:
I would rather suggest using OLE automation. That is more efficient and provides much greater control including using templates.
0
 
GrahamSkanCommented:
Yes, I agree Ranjeet, but this is Gareth's first question & he may not be ready for that.
0
 
GarethWhamAuthor Commented:
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
 
GrahamSkanCommented:
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
 
GrahamSkanCommented:
Forgot to say that you should set a reference to the Microsoft Excel library.
0
 
GarethWhamAuthor Commented:
Now you have written the code I know how to implement OLE automation.
I will try this and see what happens thanks
0
 
GrahamSkanCommented:
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
 
GarethWhamAuthor Commented:
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
 
RanjeetRainCommented:
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
 
RanjeetRainCommented:
Sorry!rong thread! Please ignore my last post :)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 5
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now