Access word documents header containing a table with vbscript - vbscript

I need to read the pure text values from cells of a table located in the page header of a word document using vbscript.
All i could achieve until now is to read some kind of text from the header by this:
wscript.echo WordApp.ActiveDocument.Sections(1).Headers(1).Range.FormattedText.Text
But how can i gain access to the table object and read each cell value?

Tables in Word documents can be enumerated via the Tables collection.
For Each tbl In WordApp.ActiveDocument.Sections(1).Headers(1).Range.Tables
For i = 1 To tbl.Rows.Count
For j = 1 To tbl.Columns.Count
WScript.Echo tbl.Cell(i, j).Value
Next
Next
Next

Related

How to enter content to an existing CSV file

I am trying to enter data cell by cell to a CSV file, but when I open it, I get all the contents in column A of the CSV file separated by "," (comma).
I am trying it in the following way:
oExcel = CREATEOBJECT("Excel.Application")
oWorkBook=oExcel.Workbooks.Open("c:\planilla.csv")
oExcel.application.Cells(1,1).Value = "StockCode"
oExcel.application.Cells(1,2).Value = "In-Stock"
oExcel.application.Cells(1,3).Value = "Kit"
oWorkbook.Save()
oWorkbook.Close()
oExcel.Quit()
RELEASE oWorkbook
RELEASE oExcel
When opening the CSV document, the output is in a single cell (column A), like this:
StockCode, In-Stock, Kit.
I hope you can help me, regards
First off, I don't understand why you are trying to use the Excel object in order to create a csv file. Why don't you just create a csv file with the commands in VFP?
Second, I did try your example and it did create one cell with the 3 items separated by 2 tabs. But, if I do the same thing on an xlsx file (an actual Excel workbook), your example works. I don't think the Excel office automation object was ever intended to be used on a CSV file.
If you need to output to a CSV file, you can do that with VFP commands without having to create an Excel object.
Saw your comment... try running this in VFP:
* create cursor and some records
CREATE CURSOR test (nid i, stockcode c(10),instock i,kit c(20), lincsv l)
INSERT INTO test (nid,stockcode,instock,kit,lincsv) VALUES (1,"SC-001",10,"kit001",.f.)
INSERT INTO test (nid,stockcode,instock,kit,lincsv) VALUES (2,"SC-010",0,"kit003",.f.)
INSERT INTO test (nid,stockcode,instock,kit,lincsv) VALUES (3,"SC-200",5,"kit010",.f.)
* call function to add records to csv file
=AddToCSV()
* insert a couple more records to cursor
INSERT INTO test (nid,stockcode,instock,kit,lincsv) VALUES (4,"SC-201",6,"kit001",.f.)
INSERT INTO test (nid,stockcode,instock,kit,lincsv) VALUES (5,"SC-005",520,"kit099",.f.)
* call function again to add new records to existing csv file
=AddToCSV()
FUNCTION AddToCSV
* open csv file (create if it doesn't exist, add to existing)
SET ALTERNATE TO c:\temp\planilla.csv ADDITIVE
SET ALTERNATE ON
SCAN FOR NOT linCSV
?? stockcode + ","
?? instock
?? "," + kit
replace lincsv WITH .t.
?
ENDSCAN
SET ALTERNATE OFF
SET ALTERNATE TO
RETURN

Filter in OpenOffice Calc

Scenario:
I have a spreadsheet with info from a giveaway campaign in which I get paid per new Twitter follow my client receives through my campaign. Unfortunately the application I use does not track new followers vs existing ones because they offer an entry for new and existing followers for the "Follow on Twitter for 1 Entry". Because I also offer other means to gain entries I need to export the data and filter the results to show only those who've gained an entry on the Twitter Follow and then filter out those who are new vs existing by means of a separate application.
Problem:
There should be a separate column for each data type; name,email,action, etc. The action column is where I would expect to find the "Follow On Twitter" but the file is very disorganized and the action can be found in many different columns. Therefore I need a way to show only the rows in which there is a field with "Follow on Twitter". I am at a loss to try and figure out how to do this.
The following macro will search for "Follow On Twitter" in each cell. For each row, if a match is found, the row will be shown, else it will be hidden. You will have to adjust the macro to match your sheet's total number of rows/columns.
Sub Dummy()
GlobalScope.BasicLibraries.LoadLibrary("Tools")
Dim ActiveSheet As Object
ActiveSheet = ThisComponent.CurrentController.ActiveSheet
Dim r,c As Integer
For r = 0 To 25
Dim found As Boolean
found = False
For c = 0 to 10
Dim cell As Object
cell = ActiveSheet.getCellByPosition(c, r)
If cell.String = "Follow On Twitter" Then
found = True
Exit For
End If
Next c
Dim row As Object
row = ActiveSheet.getRows.getByIndex(r)
row.IsVisible = found
Next r
MsgBox "Done"
End Sub

Tables got over-written

I want to loop thru a dbf and create word table for each record meeting the condition, and I got a one-page report with only the last rec in a single table. Look like all records are written to the same table. I tried to use n = n + 1 to place the variable as an element to the table
oTable = oDoc.tables[n]
But seems it only support numerical rather than variable ?
You have to add each table as you go, making sure to leave space in between them (because Word likes to combine tables).
You'll need something like this inside your loop:
* Assumes you start with oDoc pointing to the document,
* oRange set to an empty range at the beginning of the area where you want to add the tables,
* and that nRows and nCols give you the size of the table.
oTable = oDoc.Tables.Add(m.oRange, m.nRows, m.nCols)
oRange = oTable.Range()
oRange.Collapse(0)
oRange.InsertParagraphAfter()
oRange.Collapse(0)
After this code, you can use oTable to add the data you want to add. Then, on the next time through the loop, you're ready to add another table below the one you just filled.

Filtering a validation list based on columns within a named range

I am looking for a way to filter a list validation in Excel based on a multi-column named range.
I have a list of product releases on one sheet, contained in a named range that has the columns: Name, Type, Status. On another sheet, I want the user to be able to select from a validation list containing 'Name' only.
Question 3741060 here covers how to make the validation list only contain the 'Name' column. However I also need to filter so that the user cannot select a release with the status 'Completed'. [The status column only allows 'Planned', 'Allocated' or 'Completed'.]
Ideally I would also like to dynamically show only 'Planned' OR 'Allocated' releases based on yet another validation - but I think if I can get the list filtered at all I should be able to do the rest.
BTW - I am forced to use Excel 2003 for this, although I don't believe would be a major factor.
I use
an extra range LOV (for List of Values) in a hidden sheet that I fill with the current criteria the user can choose from (in my case this varies from line to line as he/she fills the sheet)
all cells in the main sheet are validated against this range LOV
a Selection_Change() trigger loads the LOV after each cursor move from the original range of possible choices
This is how I re-generate my LOV (in essence the user has already selected a country code in another cell passed here in string CtyCd, and the sheet now is prepered to offer a selection of possible choices of something called GINI for only this country ... so maybe similar to your demand)
Sub LoadL2LOV(CtyCd As String, LOVL2 As Range)
'
' CtyCd is a selection criterium for the original list in range GINI
' LOVL2 is the target range containing the current list of values
' all cells in sheet have a validation against range LOV defined
'
Dim GINI As Range, Idx As Long, Jdx As Long, LName As Name, Adr As String
' clear current PoP_L2
Set LName = ActiveWorkbook.Names(LOVL2.Name.Name)
Set GINI = Worksheets("GINI Availability").Range("GINI")
LOVL2.ClearContents
' set new LOV for PoP_L2
If CtyCd <> "" Then
Idx = 2
Jdx = 1
' find 1st occurence of CtyCd in GINI
Do While GINI(Idx, 4) <> CtyCd And GINI(Idx, 4) <> ""
Idx = Idx + 1
Loop
' GINI is sorted, just read until the end of selected CtyCd
Do While GINI(Idx, 4) = CtyCd
LOVL2(Jdx, 1) = GINI(Idx, 1) & "-" & GINI(Idx, 2) & "-" & GINI(Idx, 3)
Idx = Idx + 1
Jdx = Jdx + 1
Loop
End If
' redefine LOV name to contain all current valid choices
LOVL2.CurrentRegion.Name = LOVL2.Name.Name
End Sub
In your case, as the data seems to be more or less static, you can copy all valid selections from [Prod_Release] to LOV at Sheet_Activate or any appropriate activation trigger.
Hope this helps .... good luck MikeD

How to get sets of Records in VB6?

Hey guys, just want to ask you a simple question that I know you're familiar with... I am using VB6, I just want to get sets of records from my database. What I mean is that I have UserID and with a part of code provided below, it only gets a single set of record. Like for instance, the value of UserID is A12, and so, all sets of records with the UserID of A12 must display in Textboxes respectively with the aid of datPayroll.Recordset.MoveNext.
With datPayroll
.RecordSource = "select * from tblpayroll where empid like '" & UserID & "'"
.Refresh
Me.txtRegularHours.Text = .Recordset.Fields!reghours
End With
-datPayroll : DataControl
-txtRegularHours : Textbox
-UserID : Variable
You probably want to look at MoveFirst, MoveNext, etc. and also EOF
Here is a link or two to get you started:
EOF, BOF
MoveFirst, MoveNext
You need to check that you have some data in your Recordset using EOF, then MoveFirst to move to the first record, and loop through using MoveNext.

Resources