Solved

Need help understanding this code..

Posted on 2009-03-31
7
311 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Stressed Out?

Watch some penguins on the livecam!

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

691 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