hometech
11-21-2012, 08:13 AM
Hello Guys, i am currently working on a macro that stores the values of the current selected or highlighted text in a word document into an access database, the code works very well without errorrs
Sub insertValuesToDB()
Dim valueRead As String
Application.Selection.Expand wdLine
valueRead = Application.Selection.Text
Dim adoConn As ADODB.Connection
Dim adoCmd As ADODB.Command
Set adoConn = New ADODB.Connection
With adoConn
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\CPMS\M&CAutomation.mdb"
.Open
End With
Set adoCmd = New ADODB.Command
With adoCmd
.ActiveConnection = adoConn
.CommandText = "INSERT INTO Comments (cmtComment) VALUES ('" & (valueRead) & "')"
End With
adoCmd.Execute
adoConn.Close
Set adoConn = Nothing
MsgBox "Value was added to your database table."
End Sub
okay so now i want to do is to be able to have multiple selection in my document and write the different selected text to different fields within the database using the first selection as a criteria to write into the data base
so this is what i mean
i have lines of text like this in my table
1 bla, bla, bla.....
2 bla, bla, bla.....
3 bla, bla, bla.....
4 bla, bla, bla.....
i want to be able to select item 2 and select the sentence that follows it without the space (which is very possible in ms word) and now assign the sentence to valueread which was done in the code above and also assign the number (2) as another selection so that i would be able to refer to them differently programatically as selection(1).text and selection(2).text as we can refer to tables, shapes etc in vba so that i can do something like this
"INSERT INTO Comments (cmtComment) VALUES ('" & (valueRead) & "' where ID = '" & (valueRead2) & "')"
valueread2 refers to the list number (1,2, 3 or 4)
please any ideas, appreciate your suggestions
Thanks
Sub insertValuesToDB()
Dim valueRead As String
Application.Selection.Expand wdLine
valueRead = Application.Selection.Text
Dim adoConn As ADODB.Connection
Dim adoCmd As ADODB.Command
Set adoConn = New ADODB.Connection
With adoConn
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\CPMS\M&CAutomation.mdb"
.Open
End With
Set adoCmd = New ADODB.Command
With adoCmd
.ActiveConnection = adoConn
.CommandText = "INSERT INTO Comments (cmtComment) VALUES ('" & (valueRead) & "')"
End With
adoCmd.Execute
adoConn.Close
Set adoConn = Nothing
MsgBox "Value was added to your database table."
End Sub
okay so now i want to do is to be able to have multiple selection in my document and write the different selected text to different fields within the database using the first selection as a criteria to write into the data base
so this is what i mean
i have lines of text like this in my table
1 bla, bla, bla.....
2 bla, bla, bla.....
3 bla, bla, bla.....
4 bla, bla, bla.....
i want to be able to select item 2 and select the sentence that follows it without the space (which is very possible in ms word) and now assign the sentence to valueread which was done in the code above and also assign the number (2) as another selection so that i would be able to refer to them differently programatically as selection(1).text and selection(2).text as we can refer to tables, shapes etc in vba so that i can do something like this
"INSERT INTO Comments (cmtComment) VALUES ('" & (valueRead) & "' where ID = '" & (valueRead2) & "')"
valueread2 refers to the list number (1,2, 3 or 4)
please any ideas, appreciate your suggestions
Thanks