?
Solved

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

Posted on 2011-09-30
5
Medium Priority
?
266 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
[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
  • 2
  • 2
5 Comments
 
LVL 17

Accepted Solution

by:
Carlos Villegas earned 2000 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 23

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 23

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

719 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