Do you want to save or open this file?

  • Thread starter Thread starter Derek
  • Start date Start date
D

Derek

I am trying to print a Word document from with an ASP.Net 2005 application.
My code is:

Dim myFile As New FileInfo(fileName)
Response.Clear()
Response.AddHeader("Content-Disposition", "attachment; filename=" +
(UserSession.LoggedInUser.PrinterName + "^").Replace(" ", "$").Replace("\",
"~") + "Barcode.Doc")
Response.AddHeader("Content-Length", myFile.Length.ToString())
Response.ContentType = "application/msword"
Response.WriteFile(myFile.FullName)
Response.Flush()

When the code runs I am asked "Do you want to save or open this file?".

I click open and a macro runs in the word document and word closes down.

How can I by pass the dialog box and just print the document?
 
When the code runs I am asked "Do you want to save or open this file?".

I click open and a macro runs in the word document and word closes down.

How can I by pass the dialog box and just print the document?

AFAIK, you can't do this in ASP.NET. The reason is simple: the code is on
hte server side and the application is opening on the client side. As a
Word doc can contain evil macros (while your print macro does not qualify
as evil, some people are not as nice), the user should always be prompted
so he can abort opening the document if it does not come from a trusted
source. This does not help you, of course.

The only option I can think of that might avoid this is setting up Word as
a MIME type, but even then I think you would need some corporate trust
policy to avoid save/open and force the open.

I could be partially incorrect on this, as I have not heavily invested in
Office/web automation, but the concept that the browser tries to save a
user from himself is sound.

If you want to retool so the file is printed from the server side, you have
some options, esp. with third party office libs. This can work in a
corporate environment, but would not work on a public website.

Peace and Grace,

--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
You can't for safety reasons...

More precisely unless workstations are under your control. If they are you
could perhaps come up with a different scheme but you may still want to
double check you want to implement this. If allowed by the security zone in
which the site is, a web page could automate Word to open the file and print
it (but you may have an additional dialog to connect to the site that holds
the word document as Word will use its own connection)...

I believe you can also configure this at the mime type level but then it
would be a security issue for other sites as well...

So it's likely better to just live with the extra click.
 
Under no circumstances should a web page ever try to automate Word or any
of the other Office products.

Not server side but client side to print to the local printer.

That said this is mainly for completeness as IMO it would be really overkill
to avoid an additional click and it would have its own problem likely adding
additional clicks (such as authentication) unless you can configure every
settings just to achieve this very specific goal.
 
Back
Top