I've written a script using Libreoffice Basic that pulls data from a calc file and puts it in a writer doc and then exports the doc as a PDF. While that works there is an unwanted effect, none of the links work in the PDF. If I manually export the PDF, the links work. How do I alter the script so that the PDFs created will have working links too?
Script follows (links to example PDFs at the end):
REM Making example pdfs based on writer file while pulling data from a calc file
Dim strPath As String
Dim objWriterDoc As Object
Dim objCalcDoc As Object
Dim objCalcSheet as Object
Dim objCalcCellForID as Object
Dim objCalcCellForName as Object
Dim rg as Object
Dim lngRows as Long
Dim args()
Dim PDFargs(1) As New "com.sun.star.beans.PropertyValue"
Dim Watermarkargs(4) As New "com.sun.star.beans.PropertyValue"
REM set PDF export args
PDFargs(0).Name = "FilterName"
PDFargs(0).Value = "writer_pdf_Export"
PDFargs(1).Name = "FilterData"
PDFargs(1).Value = Array(Array("UseLosslessCompression",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("Quality",0,70,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("ReduceImageResolution",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("MaxImageResolution",0,300,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("UseTaggedPDF",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("SelectPdfVersion",0,0,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("PDFUACompliance",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("ExportNotes",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("ViewPDFAfterExport",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("ExportBookmarks",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("OpenBookmarkLevels",0,1,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("UseTransitionEffects",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("IsSkipEmptyPages",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("ExportPlaceholders",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("IsAddStream",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("ExportFormFields",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("FormsType",0,0,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("AllowDuplicateFieldNames",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("HideViewerToolbar",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("HideViewerMenubar",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("HideViewerWindowControls",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("ResizeWindowToInitialPage",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("CenterWindow",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("OpenInFullScreenMode",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("DisplayPDFDocumentTitle",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("InitialView",0,1,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("Magnification",0,0,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("Zoom",0,100,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("PageLayout",0,0,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("FirstPageOnLeft",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("InitialPage",0,1,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("Printing",0,2,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("Changes",0,4,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("EnableCopyingOfContent",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("EnableTextAccessForAccessibilityTools",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("ExportLinksRelativeFsys",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("PDFViewSelection",0,0,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("ConvertOOoTargetToPDFTarget",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("ExportBookmarksToPDFDestination",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("SignPDF",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("_OkButtonString",0,"",com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("Watermark",0,"",com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("EncryptFile",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("PreparedPasswords",0,,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("RestrictPermissions",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("PreparedPermissionPassword",0,Array(),com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("SignatureLocation",0,"",com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("SignatureReason",0,"",com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("SignatureContactInfo",0,"",com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("SignaturePassword",0,"",com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("SignatureCertificate",0,,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("SignatureTSA",0,"",com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("UseReferenceXObject",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE))
REM open the writer file
strPath = ConvertToUrl("/home/wrg/Downloads/example.odt")
objWriterDoc = StarDesktop.loadComponentFromURL(strPath, "default", 0, args())
REM open the calc list
strPath = ConvertToUrl("/home/wrg/Downloads/example_list.ods")
objCalcDoc = StarDesktop.loadComponentFromURL(strPath, "default", 0, args())
objCalcSheet = objCalcDoc.Sheets(0)
REM select the cols of all the needed rows
PTListRanage = objCalcSheet.getCellRangeByName("A1:B10")
REM set watermark params
Watermarkargs(0).Name = "Text"
Watermarkargs(0).Value = "Init" ' Will change this to custom text based on the calc file '
Watermarkargs(1).Name = "Font"
Watermarkargs(1).Value = "Malgun Gothic Semilight"
Watermarkargs(2).Name = "Angle"
Watermarkargs(2).Value = 315 ' Degrees, int '
Watermarkargs(3).Name = "Transparency"
Watermarkargs(3).Value = 88 ' Percent, int '
Watermarkargs(4).Name = "Color"
Watermarkargs(4).Value = 12632256 ' FF0000 = Red; number only '
document = objWriterDoc.CurrentController.Frame
oDispHelper = createUnoService("com.sun.star.frame.DispatchHelper")
REM main loop of dataset
lngRows = PTListRanage.Rows.Count - 1
For r = 0 To lngRows
objCalcCellForID = objCalcSheet.getCellByPosition(0,r)
objCalcCellForName = objCalcSheet.getCellByPosition(1,r)
REM process the row if there is a name to process
If objCalcCellForName.String <> "" Then
REM set the WhoFor text field
if objWriterDoc.getTextFieldMasters.hasByName("com.sun.star.text.fieldmaster.SetExpression.WhoFor") then
oVar = objWriterDoc.getTextFieldMasters.getByName("com.sun.star.text.fieldmaster.SetExpression.WhoFor")
oVar.DependentTextFields(0).content = objCalcCellForName.String
else
msgbox "WhoFor not found"
end if
REM set the WhoForID text field
if objWriterDoc.getTextFieldMasters.hasByName("com.sun.star.text.fieldmaster.SetExpression.WhoForID") then
oVar = objWriterDoc.getTextFieldMasters.getByName("com.sun.star.text.fieldmaster.SetExpression.WhoForID")
oVar.DependentTextFields(0).content = objCalcCellForID.String
else
msgbox "WhoForID not found"
end if
REM add text watermark
Watermarkargs(0).Value = "For ID "+objCalcCellForID.String ' Which text will be shown as the watermark '
oDispHelper.executeDispatch(document, ".uno:Watermark", "", 0, Watermarkargs())
REM export to PDF format
strPath = ConvertToURL("/home/wrg/Downloads/example("+objCalcCellForID.String+").pdf")
objWriterDoc.storeToURL(strPath, PDFargs())
End If
Next r
REM close all the files we opened
objCalcDoc.close(True)
objWriterDoc.close(True)
Links to example PDFs, manually made with working links and script made links don't work. Also, here is a zip that includes PDFs, writer file, calc file, and bas file.
Sorry for the terrible wording on my last question, I was half asleep and it was midnight. This time I'll try to be more clear.
I'm currently writing some code for a mini barcode scanner and stock manager program. I've got the input and everything sorted out, but there is a problem with my arrays.
I'm currently trying to extract the contents of the stock file and sort them out into product tables.
This is my current code for getting the data:
Using fs As StreamReader = New StreamReader("The File Path (Is private)")
Dim line As String = "ERROR"
line = fs.ReadLine()
While line <> Nothing
Dim pos As Integer = 0
Dim split(3) As String
pos = products.Length
split = line.Split("|")
productCodes(productCodes.Length) = split(0)
products(products.Length, 0) = split(1)
products(products.Length, 1) = split(2)
products(products.Length, 2) = split(3)
line = fs.ReadLine()
End While
End Using
I have made sure that the file path does, in fact, go to the file. I have looked through debug to find that all the data is going through into my "split" table. The error throws as soon as I start trying to transfer the data.
This is where I declare the two tables being used:
Dim productCodes() As String = {}
Dim products(,) As Object = {}
Can somebody please explain why this is happening?
Thanks in advance
~Hydro
By declaring the arrays like you did:
Dim productCodes() As String = {}
Dim products(,) As Object = {}
You are assigning size 0 to all your arrays, so during your loop, it will eventually try to access a position that haven't been previously declared to the compiler. It is the same as declaring an array of size 10 Dim MyArray(10) and try to access the position 11 MyArray(11) = something.
You should either declare it with a proper size, or redim it during execution time:
Dim productCodes(10) As String
or
Dim productCodes() As String
Dim Products(,) As String
Dim Position as integer = 0
'code here
While line <> Nothing
Redim Preserve productCodes(Position)
Redim Preserve products(2,Position)
Dim split(3) As String
pos = products.Length
split = line.Split("|")
productCodes(Position) = split(0)
products(0,Position) = split(1)
products(1,Position) = split(2)
products(2,Position) = split(3)
line = fs.ReadLine()
Position+=1
End While
I have a 2D chart in Excel. I need to get the value of a cell using two string variables. The chart looks like this:
Document person1 person2
Text1 5 8
Text2 2 1
Text3 9 6
After looking online I am finding this difficult because:
the values are strings, not integers;
the strings will change depending on which person and document combination comes up.
This should be the only code that is relevant:
Dim document as string
Dim person as string
Dim oExcel as excel.application
Dim oWB as workbook
Set oExcel = New Excel.application
Set oWB = oExcel.Workbooks.open. ("C:")
oExcel.Visible = True
oWB.Sheets ("sheet1").Cells(documemt, person)
Assuming that document and person are string variables that hold string representations of integers (e.g. document = "1", person = "2") then something like
oWB.Sheets ("sheet1").Cells(val(document), val(person))
will work. If the contents of the string variables are more complicated then you would need to do some parsing of those strings.
Assuming by "2d Chart" you mean a table in a Worksheet, and that person would be the full text "person1", or "person2", etc. and likewise for document, then perhaps this function will do the trick.
Function FindDocPerson(person As String, document As String) As Variant
Const MatchExact As Integer = 0
Dim ws As Excel.Worksheet
Set ws = ActiveWorkbook.Worksheets("Sheet1")
Dim table As Excel.Range
Set table = ws.UsedRange
Dim docRange As Excel.Range
Set docRange = table.Columns(1).Offset(1, 0).Resize(table.Columns(1).Rows.Count - 1)
Dim personRange As Excel.Range
Set personRange = table.Rows(1).Offset(0, 1).Resize(1, table.Columns.Count - 1)
Dim personIndex As Long
Dim docIndex As Long
On Error GoTo errHandler
personIndex = Application.WorksheetFunction.Match(person, personRange, MatchExact) + 1
docIndex = Application.WorksheetFunction.Match(document, docRange, MatchExact) + 1
FindDocPerson = table.Cells(docIndex, personIndex).Value2
Exit Function
errHandler:
FindDocPerson = VBA.CVErr(Excel.xlErrNA)
End Function
calling syntax:
Dim result As Variant
result = FindDocPerson("person2", "text1")
If Application.WorksheetFunction.IsError(result) Then
' handle it
Else
' found it
End If
There is a typo in your code,
oWB.Sheets ("sheet1").Cells(documemt, person)
documemt should be document
All that aside though it is unclear what you want to do, can you give a little more description please?
All we know is you need to get the value of a cell using two string variables and that it could be a string or a number. The code you posted doesn't give much more of a hint to your goal.
To convert between strings and numbers you can use CLng to convert to a long number or CStr to convert to a string. eg CLng("3") = 3 and CStr(3) = "3"
In your code this:
Set oWB = oExcel.Workbooks.open. ("C:")
Doesn't work because you are trying to open a workbook without specifying a name, I also note the ("C:") is spaced far to the right of the command call which leads me to believe this is has been typed freestyle ie not in the VBE. This makes it even harder to decode into your requirements.
Lastly, this code:
Set oExcel = New Excel.application
Why are you starting another session of Excel from Excel VBA code? Is this code somewhere other than Excel ie Outlook / Access / PowerPoint / Word / Business Objects etc etc.