Link to home
Start Free TrialLog in
Avatar of yo_bee
yo_beeFlag for United States of America

asked on

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

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.


 User generated image
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

ASKER CERTIFIED SOLUTION
Avatar of Carlos Villegas
Carlos Villegas
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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")



Avatar of yo_bee

ASKER

Thanks.  Worked like a charm.
Glad to have been of help
Avatar of yo_bee

ASKER

@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.