Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 319
  • Last Modified:

Need help understanding this code..

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
JoshinHtown
Asked:
JoshinHtown
  • 4
  • 2
1 Solution
 
oobaylyCommented:
Is Word installed on the web server?
0
 
JoshinHtownAuthor Commented:
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
 
JoshinHtownAuthor Commented:
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
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
ALaRivaCommented:
"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
 
JoshinHtownAuthor Commented:
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
 
ALaRivaCommented:
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
 
JoshinHtownAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now