Solved

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

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

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 21

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

705 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

13 Experts available now in Live!

Get 1:1 Help Now