Solved

Need help understanding this code..

Posted on 2009-03-31
7
302 Views
Last Modified: 2012-05-06
Hello All-

I have found bits and pieces of code that takes a datatable, converts it to a csv, and then displays mailing labels in a Word document.  The code below does exactly that and works great on my testing machine when I debug it through Visual Studio.  Now when I access this web application from another machine I get an error (see below).  Is this not done on the client side?  If someone knows what this error means, how can I fix my code to get this to work?  

Please let me know.

Page_Error
The message filter indicated that the application is busy. (Exception from HRESULT: 0x8001010A (RPC_E_SERVERCALL_RETRYLATER))
Dim dgv As Data.DataTable = DirectCast(Session("StoreGrid"), Data.DataTable)
 

        '++++++++++++++++++++++ try to convert datatable to csv

        Dim sw As New System.IO.StreamWriter("C:\LabelsFile.csv", False)

        Dim icolcount As Integer = dgv.Columns.Count

        For i As Integer = 0 To icolcount - 1

            sw.Write(dgv.Columns(i))

            If i < icolcount - 1 Then

                sw.Write(",")

            End If

        Next

        sw.Write(sw.NewLine)

        For Each drow As Data.DataRow In dgv.Rows

            For i As Integer = 0 To icolcount - 1

                If Not Convert.IsDBNull(drow(i)) Then

                    sw.Write(drow(i).ToString())

                End If

                If i < icolcount - 1 Then

                    sw.Write(",")

                End If

            Next

            sw.Write(sw.NewLine)

        Next

        sw.Close()
 

        '++++++++++++++++++++++++++ end convert to csv
 
 

        '++++++++++++ for word

        Dim oApp As Microsoft.Office.Interop.Word.Application 'Word.Application

        Dim oDoc As Microsoft.Office.Interop.Word.Document 'Word.Document
 

        'Start a new document in Word

        oApp = CreateObject("Word.Application")

        oDoc = oApp.Documents.Add
 

        With oDoc.MailMerge
 

            'Insert the mail merge fields temporarily so that

            'you can use the range that contains the merge fields as a layout

            'for your labels -- to use this as a layout, you can add it

            'as an AutoText entry.

            With .Fields

                .Add(oApp.Selection.Range, "owner_name")

                oApp.Selection.TypeParagraph()

                .Add(oApp.Selection.Range, "address")

                oApp.Selection.TypeParagraph()

                .Add(oApp.Selection.Range, "Mail_City")

                oApp.Selection.TypeText("  ")

                .Add(oApp.Selection.Range, "Mail_State")

                oApp.Selection.TypeText(" -- ")

                .Add(oApp.Selection.Range, "Mail_Zip")

            End With

            Dim oAutoText As Microsoft.Office.Interop.Word.AutoTextEntry 'Word.AutoTextEntry

            oAutoText = oApp.NormalTemplate.AutoTextEntries.Add("MyLabelLayout", oDoc.Content)

            oDoc.Content.Delete() 'Merge fields in document no longer needed now

            'that the AutoText entry for the label layout

            'has been added so delete it.
 

            'Set up the mail merge type as mailing labels and use

            'a tab-delimited text file as the data source.

            .MainDocumentType = WdMailMergeMainDocType.wdMailingLabels 'wdMailingLabels

            .OpenDataSource(Name:="C:\LabelsFile.csv") 'Specify the data source here
 

            'Create the new document for the labels using the AutoText entry

            'you added -- 5160 is the label number to use for this sample.

            'You can specify the label number you want to use for the output

            'in the Name argument.

            oApp.MailingLabel.CreateNewDocument(Name:="5160", Address:="", _

                AutoText:="MyLabelLayout") 'LaserTray:=wdPrinterManualFeed)
 

            'Execute the mail merge to generate the labels.

            .Destination = WdMailMergeDestination.wdSendToNewDocument 'wdSendToNewDocument

            .Execute()
 

            'Delete the AutoText entry you added

            oAutoText.Delete()
 

        End With
 

        'Close the original document and make Word visible so that

        'the mail merge results are displayed

        oDoc.Close(False)

        oApp.Visible = True
 

        'Prevent save to Normal template when user exits Word

        oApp.NormalTemplate.Saved = True

Open in new window

0
Comment
Question by:JoshinHtown
  • 4
  • 2
7 Comments
 
LVL 15

Expert Comment

by:oobayly
Comment Utility
Is Word installed on the web server?
0
 

Author Comment

by:JoshinHtown
Comment Utility
Hello- Yes Word is installed.  I've done some further research and discovered this article.  http://support.microsoft.com/kb/246018

It could be an issue with the antivirus protection or a number of other things.  I will keep testing but if anyone has any suggestions to help find the problem faster and resolve the situation.  Please let me know.

Regards,
0
 

Author Comment

by:JoshinHtown
Comment Utility
I can't get this to work.  Can ANYONE help??  So far the only thing I have determined is that my Web Application runs fine through Visual Studio.  It opens up Word and creates Mailing Labels just fine.  But if I try to open the website through the Web Browser, Word never opens.

What could be the issue here??

Regards,
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 10

Expert Comment

by:ALaRiva
Comment Utility
"It opens up Word and creates Mailing Labels just fine.  But if I try to open the website through the Web Browser, Word never opens."

The code you are showing is Server-Side Code, and that is the only place that you can run those particular actions.

The reason you are getting Word to open when you run it from your local host is because your system is serving as the "SERVER".  I'm sure if you go to the Server that is hosting the website, you will find a few instances of Word open, but you cannot spark new instances of word to a client with the methods you are thinking of, that's not how Web Applications work.

- Anthony
0
 

Author Comment

by:JoshinHtown
Comment Utility
Hi Alariva-

So you are saying there is no way at all that I can get Word to open on the client side??  There must be some kind of way to accomplish this.. Thoughts?
0
 
LVL 10

Accepted Solution

by:
ALaRiva earned 500 total points
Comment Utility
Your only option would be to stream the word doc to the client, but again, this would still be a disconnected version of the file, not a version from your server.

If you are filling the Word Doc with data before presenting to them, then you'll need to perform this action on the server, save the file with a unique id so that you can keep it separate from other visitors' files, and then stream that particular file to the client.

HTH
- Anthony
0
 

Author Comment

by:JoshinHtown
Comment Utility
Yes Anthony-  This sounds like my only option.  

So what code do I need to add at the end of my code to save the Word document on the Server in directory "C:\Stored"?  How can I assign it a uniqueID for the naming convention as well?  And last I guess if I had a hyperlink displayed when the Word file was created what would be the code for that so client could access that file?

This sounds like it could work..
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now