4 Replies - 791 Views - Last Post: 31 August 2014 - 09:05 PM Rate Topic: -----

#1 Joseph_D   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 27
  • Joined: 05-October 12

Function to get name of file which started the program

Posted 29 August 2014 - 07:53 AM

Hello everyone!
I started learning Python a couple days ago and today my coworker presented me with a interesting problem. He wants to have excel open a separate instance of excel each time he double clicks a file. He is on Windows 7 and using Office 2010.

So I am thinking I can create a program, associate all data types that excel uses and when he double clicks, my program will start. Once my program starts I will then using it to start excel in a new instance.

My problem is I cannot find a function to retrieve the name of the file responsible for opening my program.

For example if the file I click on is "test.xlsx" I want to know what function or at least where to look for a function that can retrieve the name of the file responsible for starting my program. Once I have the name I will simply us it to start the excel executable with that file as a argument.

I checked that os module but it did not seem to supply what I needed.
If anything is unclear please let me know.
Thanks for any help!

Is This A Good Question/Topic? 0
  • +

Replies To: Function to get name of file which started the program

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,246
  • Joined: 12-December 12

Re: Function to get name of file which started the program

Posted 29 August 2014 - 08:01 AM

I expect the filename will appear as a command line argument.

http://www.diveintop..._arguments.html

I haven't checked this but I believe you will need an executable file in order to use it with file associations.

This post has been edited by andrewsw: 29 August 2014 - 08:09 AM

Was This Post Helpful? 1
  • +
  • -

#3 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,246
  • Joined: 12-December 12

Re: Function to get name of file which started the program

Posted 29 August 2014 - 08:08 AM

You should investigate opening workbooks in new instances of Excel first:

http://answers.micro...19-c23a0e3a314b

otherwise, you might be wasting your time.
Was This Post Helpful? 0
  • +
  • -

#4 Joseph_D   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 27
  • Joined: 05-October 12

Re: Function to get name of file which started the program

Posted 29 August 2014 - 08:49 AM

Thank you Andrew,

I will have a look at handling command line arguments and let you know how it goes.

Unfortunately I have already researched that and tried disabling DDE and it worked for a couple minutes then started crashing excel. Our current work around involves having an excel short cut in the SendTo folder so he can just right click a file and send to excel.

Thanks for the help.
Was This Post Helpful? 0
  • +
  • -

#5 Joseph_D   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 27
  • Joined: 05-October 12

Re: Function to get name of file which started the program

Posted 31 August 2014 - 09:05 PM

Thank you for your help Andrew,

You were correct the name of the file does come in as commandline arguments. You were also correct about needing executable to do the file association. Next up I need to add a icon to my program and I am going to create a installer to automate the file association. Currently I do it manually which is time consuming.

I am including the code for my extremely simple program should anyone want to do something similar.

I used Py2exe to create the executable.

If anyone should have any suggestions on how to improve my code please let me know.

import sys
import os

#First part of the command to start excel with the starting quote that is needed to enter a file name with whitespace in Windows commandline.
command = 'start excel.exe "'

#Skips the first commandline argument because it is the name of the program.
#Concatenates all of the arguments into one string with spaces, needed if the name of the file contains whitespace.
#If the the original file name contains spaces it will separate each word into its own argument.
#The arguments must be reassembled into one string with spaces between the words to correctly find the specified file.
for args in sys.argv[1:]:
    command += args + ' '

#Remove trailing whitespace and concatenate the ending quote
command = command.strip()
command += '"'
#Issue the command to the commandline
os.system(command)


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1