Merging word template through .jsp url

I have to add a word template to an existing .jsp.  this is the code for the jsp.  when the print button is clicked, the template should be opened with the merged fields. here's the .jsp code

function printClicked () {
            document.mainForm.order_Letter_No.focus();
            document.mainForm.order_Letter_No.select();
            Copied = document.mainForm.order_Letter_No.createTextRange();
            Copied.execCommand("Copy");
            var objWord = new ActiveXObject("Word.Application");
            if (objWord != null)
              {
              var templett = document.mainForm.corres_List.options[document.mainForm.corres_List.selectedIndex].value
              
                  switch (templett)
                  {
                        case "orderletter":
                              if (document.mainForm.country_Code.value == "US") {
                                    objWord.Documents.Open("http://ncr-tomcat/Ladas/LPPCI/LPPCIUS.dot");      
                                    }
                                else
                                    {
                                    objWord.Documents.Open("http://ncr-tomcat/Ladas/LPPCI/LPPCI.dot");            
                                    }
                              break;
                        case "filingreceipt":
                              objWord.Documents.Open("http://ncr-tomcat/Ladas/LPPCI/filingreceipt.dot");
                              break;
                        case "noticeofpublication":
                              objWord.Documents.Open("http://ncr-tomcat/Ladas/LPPCI/noticeofpublication.dot");
                              break;
                        case "noticeofallowance":
                              objWord.Documents.Open("http://ncr-test/Ladas/LPPCI/noticeofallowance.dot");
                              break;                  
                        case "transmittalletter":
                              objWord.Documents.Open("http://ncr-tomcat/Ladas/LPPCI/transmittalletter.dot");
                              break;                        
                        case "filingpriority":
                                    objWord.Documents.Open("http://ncr-tomcat/Ladas/LPPCI/filingpriority.dot");
                                    break;
                  }            
            
            objWord.Visible = true;
//            objWord.Selection.Paste();
      }
      }
poporon5Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GrahamSkanRetiredCommented:
What difficulty are you having? Can you not just add another case?
poporon5Author Commented:
It is not merging the data like name, address etc from the stored procedure.
GrahamSkanRetiredCommented:
OK. I hope that the main document has already been designed and the datasource set.

I'm not good with javascript, but I think it would be something like this:

                       case "mergeit":
                                    objDoc = objWord.Documents.Open("http://ncr-tomcat/merge/mergemain.doc");
                                    objDoc.MailMerge.Execute
                                    break;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

poporon5Author Commented:
Do I need to code the word template and if so can you give me an example?
poporon5Author Commented:
what should i do to make sure that the data from the .jsp for the unique matter gets on the word template?  Please help.
GrahamSkanRetiredCommented:
Not sure that I understand that.

To do a mail merge, set up the Word document in the Word with a datasource and some mergefields for the columns. There is a Wizard in Word for this if you have any difficulty. The datasource can be almost anything with a table-like structure: an excel spreadsheet, another Word document with a table; a CSV file or a database table or stored query.

If you want to change the data, load some different data into the datasource
poporon5Author Commented:
The merge data has to be from the selected record being displayed from the url or jsp page
poporon5Author Commented:
Do I need to create a  macro?
GrahamSkanRetiredCommented:
No.

Here is a sample main document and an Excel workbook as a datasource.

I have set the document as an ordinary document to prevent difficulties in not being able to find the datasource, because the path on your system will be different from mine.

In Word, ribbon 'Mailings' tab, use the 'Start Mail Merge' dropdown and select 'Letters' for the output type of the merge. Then use the 'Select Recipients' dropdown, 'Use Existing List...' item to browse to where you have save the workbook. Select Sheet1 as the table.

You can now use the 'Preview Results' button to show the data in the main document, or you can do 'Finish & Merge', The 'Edit Individual Documents' choice will produce a new output document.
Name.docx
GrahamSkanRetiredCommented:
Oops. Hit the wrong button (I think) . I'm having system problems in sending the Excel Workbook for the datasource.
GrahamSkanRetiredCommented:
Rebooted, so I'm trying again.
Clients.xlsx
poporon5Author Commented:
I'm able to merge the data using a view and produce mass mailings.  My problem is I'm having trouble linking the data referring to a specific client from the url or jsp.
GrahamSkanRetiredCommented:
Does my suggestion about adding a WHERE clause to the QueryString address that problem?
poporon5Author Commented:
it's quite possible that I'm not explaining this right.  I need to be able to pass that unique id and the other needed fields from the URL to the word document, whichever id i called through the url and only the data relating to this id,   Please help
GrahamSkanRetiredCommented:
I don't understand you request in terms of Word Mail Merge. The data needs to be from fields in the datasource. I have shown you how to modify the query programmatically in order to filter the records. Do you want to get something out of the URL, e.g, the site name, like "www.experts-exchange.com" from this URL?

 http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Word/Q_27378162.html?cid=1572#a36945585

Perhaps it would help if you were to post one of your mail merge main documents (or something similar), so we can see what your situation is.
poporon5Author Commented:
how do i code so that the template will access the stored procedure and get the data of the specific customer that the user is displaying?  The data is not passing to the template to be merged.  My understanding is that the specific customer id that is being displayed has to be passed thru the User interface to the stored procedure to extract the data from the database or datasource.  Thank you.
GrahamSkanRetiredCommented:
Are you trying to use the Word Mail Merge process, with a stored procedure in a database to be the datasource?

Although I asked if the WHERE clause helped, I don't seem to have given details in this question. Perhaps you should have picked me up on that.

In a Word mail merge main document, you can see the query here:

objDoc.Mailmerge.DataSource.QueryString

It will look like this
SELECT * FROM I:\Allwork\MyAccess.mdb

You can add a filter with a WHERE clause, like this

SELECT * FROM I:\Allwork\MyAccess.mdb WHERE  LastName = 'Smith'

 
poporon5Author Commented:
how do i get that particular, for example, "smith" to be recognized automatically from the user interface.  For example, the user will pull customer smith on her screen (jsp page) by putting smith's customer id 1234.  the customer id is coming from the database.  "smith" cannot be part of the stored procedure.  customer id or "smith" is a field name in the database and the data can be '1234" or 7890.  The customer id or name "smith" gets displayed for the user when she enters the customer id or the name "smith" in the user interface.  The needed data, will then merge with the template to create the letter for only the chosen customer id.  Thank you.
GrahamSkanRetiredCommented:
Lastname was an example. It would probably be better to use the ID, which will be unique.

If you are using jsp, I expect that you know more about Web page design than I do. I have simply tried to explain about using Word Mail merge, and I'm still not sure if that is what you are trying to use.
poporon5Author Commented:
it did not completely help me.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.