Discussions
Categories
- 197K All Categories
- 2.5K Data
- 546 Big Data Appliance
- 1.9K Data Science
- 450.8K Databases
- 221.9K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 552 MySQL Community Space
- 479 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.1K ORDS, SODA & JSON in the Database
- 556 SQLcl
- 4K SQL Developer Data Modeler
- 187.2K SQL & PL/SQL
- 21.4K SQL Developer
- 296.4K Development
- 17 Developer Projects
- 139 Programming Languages
- 293.1K Development Tools
- 111 DevOps
- 3.1K QA/Testing
- 646.1K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 161 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.2K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 19 Java Essentials
- 162 Java 8 Questions
- 86K Java Programming
- 81 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 205 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 475 LiveLabs
- 39 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 175 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 233 Portuguese
PL/SQL package could manipulate Microsoft Word/Excel?

Is there any PL/SQL package that in Microsoft Word / Excel, and do operations such as the following tasks:
- Parse and go through the whole document
- Find a specific string, (ie. A), and replaces it with another string (ie. B)
- Add an image
- Operate the word table: create a new line, etc.
Best Answers
-
No problem. If you need help with VBA interacting with the Oracle Database, I can help as I've done it before.
-
Once you've installed the Oracle OLE DB driver on the machine the Excel file resides, you need to reference it from the "Tools -> References" menu of the VBA editor menu:
In the path I can see the directory where I installed the OLE DB driver. You also need to tick the ADO Library driver if it's not the case:
Once this is done, the way to interact with the DB is pretty simple.
First, you need to connect to the DB:
Dim Conn As ADODB.Connection Dim vHost As String Dim vPort As Integer Dim vService As String Dim vUser As String Dim vPwd As String On Error GoTo ErreurConn With Conn .ConnectionString = "PROVIDER=OraOLEDB.Oracle;USER ID=" & vUser & ";PASSWORD=" & vPwd & ";DATA SOURCE=(DESCRIPTION=(ADDRESS_LIST =(ADDRESS=(PROTOCOL=TCP)(HOST = " & vHost & ")(PORT = " & vPort & ")))(CONNECT_DATA =(SERVICE_NAME = " & vService & ")))" .Open End With
The variables needed to connect can be set in the code, or they can be inputs from the worksheet. For example:
vHost = Range("B1").value vPort = Range(« B2 »).value vService = Range(« B3 »).value vUser = Range(« B5 »).value vPwd = Range(« B6 »).value
If you want to test that the connection is ok, you can do:
If Conn.State = 1 Then MsgBox "Connection Successful" Conn.Close Set Conn = Nothing End If
Now any command you want to pass can use this connection. For example, if you want to perform a SELECT statement with bind variables, you do as follows:
'variables Dim vQuery As String Dim SQLCommand As ADODB.Command Dim Param As ADODB.Parameter Dim Rs As ADODB.Recordset vQuery = "SELECT * FROM T WHERE id = :id" Set SQLCommand = New ADODB.Command With CommandSQL .ActiveConnection = Conn .CommandText = vQuery .CommandType = adCmdText 'value for the bind variable Set param = .CreateParameter("id", adNumeric, adParamInput) param.value = 1 .Parameters.Append param Set Rs = .Execute End With 'Browse the recordset Do While Not Rs.EOF 'Retrieve the column values vVal = Rs.Fields("val") vCreationDate = Rs.Fields("creation_date") Rs.MoveNext Loop Set Rs = Nothing
If you have more than 1 bind variable, then add another .CreateParameter construct and append it to the Parameters collection.
The logic is similar for other kind of commands (DML statements, calling stored procedures and functions). I can provide you with examples if necessary.
Answers
-
Hi,
Why would you do that in PL/SQL? A VBA macro seems more appropriate to do this kind of things. If you need input data from the database, then VBA can connect to the Oracle DB through ADO components using the Oracle OLE DB driver.
-
Thanks GregV, the main reason is that PL/SQL is the primary skillset for our team, and any suggestion about how we could leverage OLE DB Driver based on PL/SQL skill to implement this?
-
Ok but pl/sql resides in the database, so it's best used to process data within the database, this data could be from tables, files on the db server, webservices, etc.
Using PL/SQL to perform Word/Excel purely related-tasks is not the way to go. The OLE DB driver will let you connect to the Oracle DB thus interact with it (DML statements, calling procedures/functions). So the purpose is to either push data from the document/worksheet to the DB or retrieve data from the DB to feed some variables.
Amending text, inserting images, moving stuff, ... is better dealt with some VBA code, IMO.
-
-
No problem. If you need help with VBA interacting with the Oracle Database, I can help as I've done it before.
-
Thanks, and what's your suggestion to call the VBA codes to interact with Oracle DB, I mean for the PL/SQL, what the execution entry point will be?
-
Once you've installed the Oracle OLE DB driver on the machine the Excel file resides, you need to reference it from the "Tools -> References" menu of the VBA editor menu:
In the path I can see the directory where I installed the OLE DB driver. You also need to tick the ADO Library driver if it's not the case:
Once this is done, the way to interact with the DB is pretty simple.
First, you need to connect to the DB:
Dim Conn As ADODB.Connection Dim vHost As String Dim vPort As Integer Dim vService As String Dim vUser As String Dim vPwd As String On Error GoTo ErreurConn With Conn .ConnectionString = "PROVIDER=OraOLEDB.Oracle;USER ID=" & vUser & ";PASSWORD=" & vPwd & ";DATA SOURCE=(DESCRIPTION=(ADDRESS_LIST =(ADDRESS=(PROTOCOL=TCP)(HOST = " & vHost & ")(PORT = " & vPort & ")))(CONNECT_DATA =(SERVICE_NAME = " & vService & ")))" .Open End With
The variables needed to connect can be set in the code, or they can be inputs from the worksheet. For example:
vHost = Range("B1").value vPort = Range(« B2 »).value vService = Range(« B3 »).value vUser = Range(« B5 »).value vPwd = Range(« B6 »).value
If you want to test that the connection is ok, you can do:
If Conn.State = 1 Then MsgBox "Connection Successful" Conn.Close Set Conn = Nothing End If
Now any command you want to pass can use this connection. For example, if you want to perform a SELECT statement with bind variables, you do as follows:
'variables Dim vQuery As String Dim SQLCommand As ADODB.Command Dim Param As ADODB.Parameter Dim Rs As ADODB.Recordset vQuery = "SELECT * FROM T WHERE id = :id" Set SQLCommand = New ADODB.Command With CommandSQL .ActiveConnection = Conn .CommandText = vQuery .CommandType = adCmdText 'value for the bind variable Set param = .CreateParameter("id", adNumeric, adParamInput) param.value = 1 .Parameters.Append param Set Rs = .Execute End With 'Browse the recordset Do While Not Rs.EOF 'Retrieve the column values vVal = Rs.Fields("val") vCreationDate = Rs.Fields("creation_date") Rs.MoveNext Loop Set Rs = Nothing
If you have more than 1 bind variable, then add another .CreateParameter construct and append it to the Parameters collection.
The logic is similar for other kind of commands (DML statements, calling stored procedures and functions). I can provide you with examples if necessary.
-
Thanks, Greg, appreciate your kind and professional advice, we will try this if we decide on implementing the VBA approach!