|
-
Dec 13th, 2011, 04:13 PM
#1
Thread Starter
Junior Member
Passing a value
This is a piece of code I got from somewhere on the Internet or this site, and I'd like to know if it's possible to pass a value to it from a form, the value would be the name of a view on an sql server (oper_obm) it's at the end of about the 9th line of code below after the Dim. What I'd like to do is when a button is clicked on a form it sends a value (textbox.text) to this code (called module1, but i wouldn't know a module from a class) obviously, i don't know much about code, but if someone could help me along with this it would be huge, thanks, Dan
i know i'd have to substitute the oper_obm with the variable, that part i get.
HTML Code:
Imports System.Data
Imports System.IO
Imports System.Reflection
Imports Microsoft.Office.Interop
'Mark a Reference to Microsoft word Object from Com Tab
Module Module1
Sub Main()
Try
Dim sqlQry As String = "select operatortitle, operatorfirstname, operatorlastname, operatorjobtitle, operatoraddress1, operatorcity, operatorstate, operatorpostalcode, operatortitle, operatorlastname, obmtitle, obmfirstname, obmlastname, facility from oper_obm"
'Get the values from table
Dim dttable As DataTable = DatabaseOperation.DatabaseOperation(sqlQry, DataBaseQueryType.Datatable)
Dim sTemplateFileName As String = "aaa.doc"
'Word merging
Dim moApp As Object
moApp = CreateObject("Word.Application")
If IsNothing(moApp) = False Then
moApp.visible = False
Dim sDocFileName As String
For Each row As DataRow In dttable.Rows
'Open the Document
moApp.Documents.Open(Path.Combine(ApplicationPath, sTemplateFileName))
sDocFileName = Convert.ToString(row(13)) & ".doc"
Dim intCount As Integer = 0
For Each MergeField As Word.MailMergeField In moApp.ActiveDocument.MailMerge.Fields
'Select the text
MergeField.Select()
'Type the text
moApp.Selection.TypeText(Convert.ToString(row(intCount)))
intCount += 1
Next
'Save the document
moApp.ActiveDocument.SaveAs("C:\Program Files\acoLetterGenerator\texts\" & sDocFileName)
'Close the document
moApp.Documents.Close()
Next
'Dont save the template changes
moApp.Quit(Word.WdSaveOptions.wdDoNotSaveChanges)
'Dispose
End If
moApp = Nothing
dttable.Dispose()
dttable = Nothing
Catch ex As Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
End Sub
Private Function ApplicationPath() As String
Return _
"C:\Program Files\acoLetterGenerator\docs"
'Path.GetDirectoryName([Assembly].GetEntryAssembly().Location)
End Function
End Module
Last edited by OCD Dan; Dec 13th, 2011 at 04:18 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|