Solved

Need help understanding this code..

Posted on 2009-03-31
7
308 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
ID: 24030618
Is Word installed on the web server?
0
 

Author Comment

by:JoshinHtown
ID: 24030654
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
ID: 24033840
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 10

Expert Comment

by:ALaRiva
ID: 24035038
"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
ID: 24038825
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
ID: 24041063
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
ID: 24041246
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

840 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