Solved

VB.net Code that opens and updates Text fields in a Word Doc Prompts File in use

Posted on 2011-09-30
5
262 Views
Last Modified: 2012-05-12
I have am app I build and when it gets to the oword.Application.Quit() I get this prompt if another Word File is open. I can cancel the save as process, but Task Manager shows both instances of still running.

If there are no word files open it works as planned.


 Word Prompt
Here is my code snippet

 
Imports System.IO
Imports Microsoft.Office.Interop.Word

        Private Sub oDoc(ByVal szUser As String, ByRef szmatter As String)

        Dim oword As New Application
        Dim doc = oword.Documents.Open("\\xxxappc\sys\APPS\~New FTP Login Files\00903221.dotx", Visible:=False, [ReadOnly]:=False)
        Dim svDir As String = "\\xxxappc\sys\APPS\~New FTP Login Files\" & szUser & "\" & szmatter

        If Not Directory.Exists(svDir) Then
            Directory.CreateDirectory(svDir)
        End If

        doc.FormFields("textbox1").Result = szUser & szmatter & "public"
        doc.FormFields("textbox2").Result = "pa$$w0rd"
        doc.ExportAsFixedFormat(svDir & "\" & szUser & szmatter & "public.pdf", WdExportFormat.wdExportFormatPDF, OpenAfterExport:=False)


        'Loops through an opened Word document with a Text Form Field and adds
        'a value to each textbox then repeats 5 more times.

        For i = 1 To 6
            doc.FormFields("textbox1").Result = szUser & szmatter & "area" & i
            doc.FormFields("textbox2").Result = "pa$$w0rd" & i
            doc.ExportAsFixedFormat(svDir & "\" & szUser & szmatter & "area" & i & ".pdf", WdExportFormat.wdExportFormatPDF, OpenAfterExport:=False)
        Next

        'It never gets to this section due to the error
        doc.FormFields("textbox1").Result = szUser & szmatter & "RSPTY"
        doc.FormFields("textbox2").Result = "pa$$w0rd"
        doc.ExportAsFixedFormat(svDir & "\" & szUser & szmatter & "RSPTY.pdf", WdExportFormat.wdExportFormatPDF, OpenAfterExport:=False)

        doc.Close(WdSaveOptions.wdDoNotSaveChanges)
        oword.Application.Quit()

    End Sub

Open in new window

0
Comment
Question by:yo_bee
  • 2
  • 2
5 Comments
 
LVL 17

Accepted Solution

by:
Carlos Villegas earned 500 total points
ID: 36892877
Hello, I recommend to do these changes:
Private Sub oDoc(ByVal szUser As String, ByRef szmatter As String)
    Dim oword As Application
    Try
        oword = New Application
        Dim doc = oword.Documents.Open("\\xxxappc\sys\APPS\~New FTP Login Files\00903221.dotx", Visible:=False, [ReadOnly]:=False)
        Dim svDir As String = "\\xxxappc\sys\APPS\~New FTP Login Files\" & szUser & "\" & szmatter

        If Not Directory.Exists(svDir) Then
            Directory.CreateDirectory(svDir)
        End If

        doc.FormFields("textbox1").Result = szUser & szmatter & "public"
        doc.FormFields("textbox2").Result = "pa$$w0rd"
        doc.ExportAsFixedFormat(svDir & "\" & szUser & szmatter & "public.pdf", WdExportFormat.wdExportFormatPDF, OpenAfterExport:=False)


        'Loops through an opened Word document with a Text Form Field and adds
        'a value to each textbox then repeats 5 more times.

        For i = 1 To 6
            doc.FormFields("textbox1").Result = szUser & szmatter & "area" & i
            doc.FormFields("textbox2").Result = "pa$$w0rd" & i
            doc.ExportAsFixedFormat(svDir & "\" & szUser & szmatter & "area" & i & ".pdf", WdExportFormat.wdExportFormatPDF, OpenAfterExport:=False)
        Next

        'It never gets to this section due to the error
        doc.FormFields("textbox1").Result = szUser & szmatter & "RSPTY"
        doc.FormFields("textbox2").Result = "pa$$w0rd"
        doc.ExportAsFixedFormat(svDir & "\" & szUser & szmatter & "RSPTY.pdf", WdExportFormat.wdExportFormatPDF, OpenAfterExport:=False)

        doc.Close(WdSaveOptions.wdDoNotSaveChanges)
        doc = Nothing
    Finally
        oword.Quit(False)
        oword = Nothing
    End Try
End Sub

Open in new window


Be sure to finish (with the task manager) the current Word instances before test your code.
0
 
LVL 40
ID: 36892934
You are opening the template for editing instead of using it to create a new document. You can thus have multiple users working on the template at the same time. Even worse, you will change the template.

Replace

Dim doc = oword.Documents.Open("\\xxxappc\sys\APPS\~New FTP Login Files\00903221.dotx", Visible:=False, [ReadOnly]:=False)

with

Dim doc = oword.Documents.Add("\\xxxappc\sys\APPS\~New FTP Login Files\00903221.dotx")



0
 
LVL 22

Author Closing Comment

by:yo_bee
ID: 36893252
Thanks.  Worked like a charm.
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 36893287
Glad to have been of help
0
 
LVL 22

Author Comment

by:yo_bee
ID: 36893476
@JamesBurger
Thanks, but this DOTX is only used by this app to fill out the two fields and generate the PDF.

I am not saving the file.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

685 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