Solved

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

Posted on 2011-09-30
5
259 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

914 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now