yo_bee
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.
Here is my code snippet
If there are no word files open it works as planned.
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks. Worked like a charm.
Glad to have been of help
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.
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.
Replace
Dim doc = oword.Documents.Open("\\xx
with
Dim doc = oword.Documents.Add("\\xxx