Link to home
Start Free TrialLog in
Avatar of rtc_kiran
rtc_kiran

asked on

How to use VBA macros to print a word document

Hi,
I've designed a page in word and saved it as an asp page (word.asp). I would like to have a link from another asp page to automatically open the asp page(word.asp) in word, print a copy of it and then close the document again. I'm building this for an intranet application, so sending word documents which contain macros is not an issue. Where I'm stuck is basically accessing the active document from a vba macro as well as including the macro in the document when it is sent to word

the asp page is sent to word like this..
 Response.ContentType = "application/msword";
 Response.AddHeader("content-disposition","attachment; filename=Orig.doc");

the macro code so far...

Private Sub Document_Open()
Application.Visible = False
ActiveWindow.PrintOut
ActiveDocument.Close
End Sub

I don't think the macro gets included when sent to word although I had it signed and enabled signed macros in word. any help would be greatly appreciated
Avatar of Steiner
Steiner

I don't know that much about asp, but one other thought:
If you use Application.Visible = False and don't close Word using code, what are you doing with all the open instances of Word??

Have you tried putting a msgbox in your code to see whether it get's executed at all? Maybe putting it into Private Sub Document_New might help if it's a template.
Avatar of rtc_kiran

ASKER

Hi,
I the above macro is just example code, I check to see if any other documents are open, if they are only my doc gets closed, else I shut down word

these are not the issues however, what I did was

1. Create a document in word ( to make it look like I wanted it to)

2. Write a vba macro by going to Tools->Macros, adding my code and saving the macro

3. Save the document as type web page (as an asp page by saying  "test.asp" for example)

4. Open the asp page, stick my code in, add the correct content type and the content.addheader

Now I have another asp page with a button, when the user clicks the button he/she gets prompted to open or save the document. since I am working on a intranet application for a small company EVERYBODY opens the doc instead of saving it :)
What I need to do is after the user has clicked on open....

1. Open the document in word (works fine)
2. Automatically print it off (doesn't work)
3. Automatically close the active document (doensn't work)

can this be done?
I have put msgboxes in, to check if the macro gets executed or not....it doesn't.
If I take out the Response.AddHeader("content-disposition","attachment; filename=SAMENAMEASASPFILE.doc");, it reckonginzes that there are macros in the document (it prompts me to enable or disable macros), but then it starts a file download box and stays there (gets stuck?), usually it comes up with an error message if it can't download the file doesn't it? not in this case..I'm completely lost, can u specify multiple attachments in Response.AddHeader, so I could include the xml file word created?
Sorry, but that goes too far beyond my knowledge.
But somehow I doubt Word saves the macros when you save your file as web page (because macros are for Word only, and a Webpage you don't necessarily open in Word).
In don't know if that's just Word97 (what I'm using here), but if I close the doc and open it again, there is no VBA-Code. I can add some, close it and open it again --> nothing.
Why do you want to save the doc as asp if the user has to open it using Word anyway?
Hi,
I think word saves the macros in a folder with the webpage or more specificallly, I had saved the vba code on the server first for example in a macro called printout. Word then saves a link to this macro in a separate folder, I think in an xml document.
The reason why my macro isn't working when sending the asp page to word is because the document gets downloaded into a temporary internet folder first before it gets openend in word. Hence the link to the macro is no longer valid, because the link to the xml document is lost.
I need to somehow pull some records from a database. Now my SQL query needs to accept parameters, that's why I saved the word document as an asp page. I don't know how to run parameter queries from word directly. If this is possible that'd be even better...any ideas?
ASKER CERTIFIED SOLUTION
Avatar of Steiner
Steiner

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial