Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Need help understanding this code..

Posted on 2009-03-31
7
Medium Priority
?
314 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
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

610 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