How do I check for broken internal links in Star Basic? - hyperlink

I am creating a Basic macro for LibreOffice Writer to check for broken internal links. In a nutshell:
generate a list of all anchors
loop through the document, finding internal hyperlinks
if the internal hyperlink is not on the anchor list, open it for editing (and stop)
My code has a few unsolved issues:
(within fnBuildAnchorList) How do we get the numbering for each heading? For example, if the first level 1 heading text is “Introduction”, the correct anchor is #1.Introduction|outline and we are recording Introduction|outline
(within subInspectLink) How do we properly test a hyperlink to a heading? I notice that when I manually follow a link to a heading, it will succeed when the numbering is the same, but also when the text is the same. e.g. if there’s an internal link #1.My first heading|outline, it can be reached with the hyperlink #1.Previous header name|outline but also with the hyperlink #2.3.5.My first heading|outline
(within subInspectLink) How do we open a specific hyperlink for editing? Do we pass parameters to .uno:EditHyperlink? Do we move the cursor? (All moves I found were relative, e.g. .uno:GoRight) Do we use the text portion’s .Start and .End properties?
REM ***** BASIC *****
Option Explicit
' PrintArray displays a MsgBox with the whole array
' for DEBUG purposes only
Sub subPrintArray(sTitle as String, theArray() as String)
Dim sArray
sArray = sTitle & ":" & Chr(13) & Join(theArray,Chr(13))
MsgBox(sArray, 64, "***DEBUG")
End sub
' auxiliary sub for BuildAnchorList
Sub subAddItemToAnchorList (oAnchors() as String, sTheAnchor as String, sType as String)
Dim sAnchor
Select Case sType
Case "Heading":
sAnchor = sTheAnchor + "|outline"
Case "Table":
sAnchor = sTheAnchor + "|table"
Case "Text Frame":
sAnchor = sTheAnchor + "|frame"
Case "Image":
sAnchor = sTheAnchor + "|graphic"
Case "Object":
sAnchor = sTheAnchor + "|ole"
Case "Section":
sAnchor = sTheAnchor + "|region"
Case "Bookmark":
sAnchor = sTheAnchor
End Select
ReDim Preserve oAnchors(UBound(oAnchors)+1) as String
oAnchors(UBound(oAnchors)) = sAnchor
End Sub
' auxiliary sub for BuildAnchorList
Sub subAddArrayToAnchorList (oAnchors() as String, oNewAnchors() as String, sType as String)
Dim i, iStart, iStop
iStart = LBound(oNewAnchors)
iStop = UBound(oNewAnchors)
If iStop < iStart then Exit Sub ' empty array, nothing to do
For i = iStart to iStop
subAddItemToAnchorList (oAnchors, oNewAnchors(i), sType)
Next
End Sub
Function fnBuildAnchorList()
Dim oDoc as Object, oAnchors() as String
oDoc = ThisComponent
' get the whole document outline
Dim oParagraphs, thisPara, oTextPortions, thisPortion
oParagraphs = oDoc.Text.createEnumeration ' all the paragraphs
Do While oParagraphs.hasMoreElements
thisPara = oParagraphs.nextElement
If thisPara.ImplementationName = "SwXParagraph" then ' is a paragraph
If thisPara.OutlineLevel>0 Then ' is a heading
' ***
' *** TO DO: How do we get the numbering for each heading?
' For example, if the first level 1 heading text is “Introduction”,
' the correct anchor is `#1.Introduction|outline`
' and we are recording `Introduction|outline`
' ***
subAddItemToAnchorList (oAnchors, thisPara.String, "Heading")
End if
End if
Loop
' text tables, text frames, images, objects, bookmarks and text sections
subAddArrayToAnchorList(oAnchors, oDoc.getTextTables().ElementNames, "Table")
subAddArrayToAnchorList(oAnchors, oDoc.getTextFrames().ElementNames, "Text Frame")
subAddArrayToAnchorList(oAnchors, oDoc.getGraphicObjects().ElementNames, "Image")
subAddArrayToAnchorList(oAnchors, oDoc.getEmbeddedObjects().ElementNames, "Object")
subAddArrayToAnchorList(oAnchors, oDoc.Bookmarks.ElementNames, "Bookmark")
subAddArrayToAnchorList(oAnchors, oDoc.getTextSections().ElementNames, "Section")
fnBuildAnchorList = oAnchors
End Function
Function fnIsInArray( theString as String, theArray() as String )
Dim i as Integer, iStart as Integer, iStop as Integer
iStart = LBound(theArray)
iStop = UBound(theArray)
If iStart<=iStop then
For i = iStart to iStop
If theString = theArray(i) then
fnIsInArray = True
Exit function
End if
Next
End if
fnIsInArray = False
End function
Function fnIsOutlineInArray ( theString as String, theArray() as String )
Dim i as Integer
For i = LBound(theArray) to UBound(theArray)
If theArray(i) = Right(theString,Len(theArray(i))) then
fnIsOutlineInArray = True
Exit function
End if
Next
fnIsOutlineInArray = False
End function
' auxiliary function to FindBrokenInternalLinks
' inspects any links inside the current document fragment
' used to have an enumeration inside an enumeration, per OOo examples,
' but tables don't have .createEnumeration
Sub subInspectLinks( oAnchors as Object, oFragment as Object, iFragments as Integer, iLinks as Integer )
Dim sMsg, sImplementation, thisPortion
sImplementation = oFragment.implementationName
Select Case sImplementation
Case "SwXParagraph":
' paragraphs can be enumerated
Dim oParaPortions, sLink, notFound
oParaPortions = oFragment.createEnumeration
' go through all the text portions in current paragraph
While oParaPortions.hasMoreElements
thisPortion = oParaPortions.nextElement
iFragments = iFragments + 1
If Left(thisPortion.HyperLinkURL, 1) = "#" then
' internal link found: get it all except initial # character
iLinks = iLinks + 1
sLink = right(thisPortion.HyperLinkURL, Len(thisPortion.HyperLinkURL)-1)
If Left(sLink,14) = "__RefHeading__" then
' link inside a table of contents, no need to check
notFound = False
Elseif Right(sLink,8) = "|outline" then
' special case for outline: since we don't know how to get the
' outline numbering, we have to match the right most part of the
' link only
notFound = not fnIsOutlineInArray(sLink, oAnchors)
Else
notFound = not fnIsInArray(sLink, oAnchors)
End if
If notFound then
' anchor not found
' *** DEBUG: code below up to MsgBox
sMsg = "Fragment #" & iFragments & ", internal link #" & iLinks & Chr(13) _
& "Bad link: [" & thisPortion.String & "] -> [" _
& thisPortion.HyperLinkURL & "] " & Chr(13) _
& "Paragraph:" & Chr(13) & oFragment.String & Chr(13) _
& "OK to continue, Cancel to stop"
Dim iChoice as Integer
iChoice = MsgBox (sMsg, 48+1, "Find broken internal link")
If iChoice = 2 Then End
' ***
' *** TO DO: How do we open a _specific_ hyperlink for editing?
' Do we pass parameters to `.uno:EditHyperlink`?
' Do we move the cursor? (Except all moves I found were relative,
' e.g. `.uno:GoRight`)
' Do we use the text portion’s `.Start` and `.End` properties?
' ***
End If
End if
Wend
' *** END paragraph
Case "SwXTextTable":
' text tables have cells
Dim i, eCells, thisCell, oCellPortions
eCells = oFragment.getCellNames()
For i = LBound(eCells) to UBound(eCells)
thisCell = oFragment.getCellByName(eCells(i))
oCellPortions = thisCell.createEnumeration
While oCellPortions.hasMoreElements
thisPortion = oCellPortions.nextElement
iFragments = iFragments + 1
' a table cell may contain a paragraph or another table,
' so call recursively
subInspectLinks (oAnchors, thisPortion, iFragments, iLinks)
Wend
' xray thisPortion
'SwXCell has .String
Next
' *** END text table
Case Else
sMsg = "Implementation method '" & sImplementation & "' not covered by regular code." _
& "OK to continue, Cancel to stop"
If 2 = MsgBox(sMsg, 48+1) then End
' uses xray for element inspection; if not available, comment the two following lines
BasicLibraries.loadLibrary("XrayTool")
xray oFragment
' *** END unknown case
End Select
End sub
Sub FindBrokenInternalLinks
' Find the next broken internal link
'
' Pseudocode:
'
' * generate link of anchors - *** TO DO: prefix the outline numbering for headings
' * loop, searching for internal links
' - is the internal link in the anchor list?
' * Yes: continue to next link
' * No: (broken link found)
' - select that link text - *** TO DO: cannot select it
' - open link editor so user can fix this
' - stop
' * end loop
' * display message "No bad internal links found"
Dim oDoc as Object, oFragments as Object, thisFragment as Object
Dim iFragments as Integer, iLinks as Integer, sMsg as String
Dim oAnchors() as String ' list of all anchors in the document
' Dim sMsg ' for MsgBox
oDoc = ThisComponent
' get all document anchors
oAnchors = fnBuildAnchorList()
' subPrintArray("Anchor list", oAnchors) ' *** DEBUG ***
' MsgBox( UBound(oAnchors)-LBound(oAnchors)+1 & " anchors found – stand by for checking")
' find links
iFragments = 0 ' fragment counter
iLinks = 0 ' internal link counter
oFragments = oDoc.Text.createEnumeration ' has all the paragraphs
While oFragments.hasMoreElements
thisFragment = oFragments.nextElement
iFragments = iFragments + 1
subInspectLinks (oAnchors, thisFragment, iFragments, iLinks)
Wend
If iLinks then
sMsg = iLinks & " internal links found, all good"
Else
sMsg = "This document has no internal links"
End if
MsgBox (sMsg, 64, "Find broken internal link")
End Sub
' *** END FindBrokenInternalLinks ***
You can check the first issue using any document with a heading – a MsgBox will pop up with all the anchors, and you’ll see the missing outline numbering.
The second issue needs a document with a bad internal link.

Check out cOOol. You could either use this
instead of creating a macro,
or else borrow some concepts from the code.
Testing the links (possibly with .uno:JumpToMark) does not seem like it would be helpful,
because internal links always go somewhere even if the target does not exist.
Instead, construct a list of valid targets as you suggested.
To hold the list of valid targets, the cOOol code uses a Python set.
If you want to use Basic, then data structures are more limited.
However it can be done either by declaring a new a
Collection object
or by using Basic arrays, perhaps with ReDim.
Also have a look at how the cOOol code defines the valid target strings. For example:
internal_targets.add('0.' * heading_level + data + '|outline')
To open the hyperlink dialog, select the hyperlinked text and then call:
dispatcher.executeDispatch(document, ".uno:EditHyperlink", "", 0, Array())
EDIT:
Ok, I worked on this for several hours and came up with the following code:
REM ***** BASIC *****
Option Explicit
' PrintArray displays a MsgBox with the whole array
' for DEBUG purposes only
Sub subPrintArray(sTitle as String, theArray() as String)
Dim sArray
sArray = sTitle & ":" & Chr(13) & Join(theArray,Chr(13))
MsgBox(sArray, 64, "***DEBUG")
End sub
' auxiliary sub for BuildAnchorList
Sub subAddItemToAnchorList (oAnchors() as String, sTheAnchor as String, sType as String)
Dim sAnchor
Select Case sType
Case "Heading":
sAnchor = sTheAnchor + "|outline"
Case "Table":
sAnchor = sTheAnchor + "|table"
Case "Text Frame":
sAnchor = sTheAnchor + "|frame"
Case "Image":
sAnchor = sTheAnchor + "|graphic"
Case "Object":
sAnchor = sTheAnchor + "|ole"
Case "Section":
sAnchor = sTheAnchor + "|region"
Case "Bookmark":
sAnchor = sTheAnchor
End Select
ReDim Preserve oAnchors(UBound(oAnchors)+1) as String
oAnchors(UBound(oAnchors)) = sAnchor
End Sub
' auxiliary sub for BuildAnchorList
Sub subAddArrayToAnchorList (oAnchors() as String, oNewAnchors() as String, sType as String)
Dim i, iStart, iStop
iStart = LBound(oNewAnchors)
iStop = UBound(oNewAnchors)
If iStop < iStart then Exit Sub ' empty array, nothing to do
For i = iStart to iStop
subAddItemToAnchorList (oAnchors, oNewAnchors(i), sType)
Next
End Sub
' Updates outlineLevels for the current level.
' Returns a string like "1.2.3"
Function fnGetOutlinePrefix(outlineLevel as Integer, outlineLevels() as Integer)
Dim level as Integer, prefix as String
outlineLevels(outlineLevel) = outlineLevels(outlineLevel) + 1
For level = outlineLevel + 1 to 9
' Reset all lower levels.
outlineLevels(level) = 0
Next
prefix = ""
For level = 0 To outlineLevel
prefix = prefix & outlineLevels(level) & "."
Next
fnGetOutlinePrefix = prefix
End Function
Function fnBuildAnchorList()
Dim oDoc as Object, oAnchors() as String, anchorName as String
Dim level as Integer, levelCounter as Integer
Dim outlineLevels(10) as Integer
For level = 0 to 9
outlineLevels(level) = 0
Next
oDoc = ThisComponent
' get the whole document outline
Dim oParagraphs, thisPara, oTextPortions, thisPortion
oParagraphs = oDoc.Text.createEnumeration ' all the paragraphs
Do While oParagraphs.hasMoreElements
thisPara = oParagraphs.nextElement
If thisPara.ImplementationName = "SwXParagraph" then ' is a paragraph
If thisPara.OutlineLevel>0 Then ' is a heading
level = thisPara.OutlineLevel - 1
anchorName = fnGetOutlinePrefix(level, outlineLevels) & thisPara.String
subAddItemToAnchorList (oAnchors, anchorName, "Heading")
End if
End if
Loop
' text tables, text frames, images, objects, bookmarks and text sections
subAddArrayToAnchorList(oAnchors, oDoc.getTextTables().ElementNames, "Table")
subAddArrayToAnchorList(oAnchors, oDoc.getTextFrames().ElementNames, "Text Frame")
subAddArrayToAnchorList(oAnchors, oDoc.getGraphicObjects().ElementNames, "Image")
subAddArrayToAnchorList(oAnchors, oDoc.getEmbeddedObjects().ElementNames, "Object")
subAddArrayToAnchorList(oAnchors, oDoc.Bookmarks.ElementNames, "Bookmark")
subAddArrayToAnchorList(oAnchors, oDoc.getTextSections().ElementNames, "Section")
fnBuildAnchorList = oAnchors
End Function
Function fnIsInArray( theString as String, theArray() as String )
Dim i as Integer
For i = LBound(theArray()) To UBound(theArray())
If theString = theArray(i) Then
fnIsInArray = True
Exit function
End if
Next
fnIsInArray = False
End function
' Open a _specific_ hyperlink for editing.
Sub subEditHyperlink(textRange as Object)
Dim document As Object
Dim dispatcher As Object
Dim oVC As Object
oVC = ThisComponent.getCurrentController().getViewCursor()
oVC.gotoRange(textRange.getStart(), False)
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dispatcher.executeDispatch(document, ".uno:EditHyperlink", "", 0, Array())
End Sub
' auxiliary function to FindBrokenInternalLinks
' inspects any links inside the current document fragment
' used to have an enumeration inside an enumeration, per OOo examples,
' but tables don't have .createEnumeration
Sub subInspectLinks(oAnchors() as String, oFragment as Object, iFragments as Integer, iLinks as Integer, iBadLinks as Integer)
Dim sMsg, sImplementation, thisPortion
sImplementation = oFragment.implementationName
Select Case sImplementation
Case "SwXParagraph":
' paragraphs can be enumerated
Dim oParaPortions, sLink, notFound
oParaPortions = oFragment.createEnumeration
' go through all the text portions in current paragraph
While oParaPortions.hasMoreElements
thisPortion = oParaPortions.nextElement
iFragments = iFragments + 1
If Left(thisPortion.HyperLinkURL, 1) = "#" then
' internal link found: get it all except initial # character
iLinks = iLinks + 1
sLink = right(thisPortion.HyperLinkURL, Len(thisPortion.HyperLinkURL)-1)
If Left(sLink,14) = "__RefHeading__" then
' link inside a table of contents, no need to check
notFound = False
Else
notFound = not fnIsInArray(sLink, oAnchors)
End if
If notFound then
' anchor not found
' *** DEBUG: code below up to MsgBox
iBadLinks = iBadLinks + 1
sMsg = "Fragment #" & iFragments & ", internal link #" & iLinks & Chr(13) _
& "Bad link: [" & thisPortion.String & "] -> [" _
& thisPortion.HyperLinkURL & "] " & Chr(13) _
& "Paragraph:" & Chr(13) & oFragment.String & Chr(13) _
& "Yes to edit link, No to continue, Cancel to stop"
Dim iChoice as Integer
iChoice = MsgBox (sMsg, MB_YESNOCANCEL + MB_ICONEXCLAMATION, _
"Find broken internal link")
If iChoice = IDCANCEL Then
End
ElseIf iChoice = IDYES Then
subEditHyperlink(thisPortion)
End If
End If
End if
Wend
' *** END paragraph
Case "SwXTextTable":
' text tables have cells
Dim i, eCells, thisCell, oCellPortions
eCells = oFragment.getCellNames()
For i = LBound(eCells) to UBound(eCells)
thisCell = oFragment.getCellByName(eCells(i))
oCellPortions = thisCell.createEnumeration
While oCellPortions.hasMoreElements
thisPortion = oCellPortions.nextElement
iFragments = iFragments + 1
' a table cell may contain a paragraph or another table,
' so call recursively
subInspectLinks (oAnchors, thisPortion, iFragments, iLinks)
Wend
' xray thisPortion
'SwXCell has .String
Next
' *** END text table
Case Else
sMsg = "Implementation method '" & sImplementation & "' not covered by regular code." _
& "OK to continue, Cancel to stop"
If 2 = MsgBox(sMsg, 48+1) then End
' uses xray for element inspection; if not available, comment the two following lines
BasicLibraries.loadLibrary("XrayTool")
xray oFragment
' *** END unknown case
End Select
End sub
Sub FindBrokenInternalLinks
' Find the next broken internal link
'
' Pseudocode:
'
' * generate link of anchors - *** TO DO: prefix the outline numbering
' * for headings loop, searching for internal links
' - is the internal link in the anchor list?
' * Yes: continue to next link
' * No: (broken link found)
' - select that link text - *** TO DO: cannot select it
' - open link editor so user can fix this
' - stop
' * end loop
' * display message "No bad internal links found"
Dim oDoc as Object, oFragments as Object, thisFragment as Object
Dim iFragments as Integer, iLinks as Integer, iBadLinks as Integer, sMsg as String
Dim oAnchors() as String ' list of all anchors in the document
oDoc = ThisComponent
' get all document anchors
oAnchors = fnBuildAnchorList()
' subPrintArray("Anchor list", oAnchors) ' *** DEBUG ***
' MsgBox( UBound(oAnchors)-LBound(oAnchors)+1 & " anchors found – stand by for checking")
' find links
iFragments = 0 ' fragment counter
iLinks = 0 ' internal link counter
iBadLinks = 0
oFragments = oDoc.Text.createEnumeration ' has all the paragraphs
While oFragments.hasMoreElements
thisFragment = oFragments.nextElement
iFragments = iFragments + 1
subInspectLinks (oAnchors, thisFragment, iFragments, iLinks, iBadLinks)
Wend
If iBadLinks > 0 Then
sMsg = iBadLinks & " bad link(s), " & iLinks - iBadLinks & " good link(s)"
ElseIf iLinks Then
sMsg = iLinks & " internal link(s) found, all good"
Else
sMsg = "This document has no internal links"
End if
MsgBox (sMsg, 64, "Find broken internal link")
End Sub
' *** END FindBrokenInternalLinks ***
It now checks for outline numbering. Maybe it's too strict -- perhaps it would be good to have an option to turn off outline number checking.
As far as issue 3, this code now opens the proper links for editing (as long as "Yes" is clicked in the message box).

Related

how to modify code to allow transpose copied data?

I have a code to insert all data from different excel files located in one folder as a loop ,
but the inserted data comes as a table (Vertically) , here is my question
what is the required modification to the existing code to make the inserted data transpose horizontally??????? thanks in advance
here is the pic of loaded data, i need these table to be shown horizontally
enter image description here
here is my code
Sub Basic_Example_1()
Dim MyPath As String, FilesInPath As String
Dim MyFiles() As String
Dim SourceRcount As Long, Fnum As Long
Dim mybook As Workbook, BaseWks As Worksheet
Dim sourceRange As Range, destrange As Range
Dim rnum As Long, CalcMode As Long
'Fill in the path\folder where the files are
MyPath = "C:\Users\mjamal\Desktop\Downloads\Sensor1"
'Add a slash at the end if the user forget it
If Right(MyPath, 1) <> "\" Then
MyPath = MyPath & "\"
End If
'If there are no Excel files in the folder exit the sub
FilesInPath = Dir(MyPath & "*.xl*")
If FilesInPath = "" Then
MsgBox "No files found"
Exit Sub
End If
'Fill the array(myFiles)with the list of Excel files in the folder
Fnum = 1
Do While FilesInPath <> ""
Fnum = Fnum + 1
ReDim Preserve MyFiles(1 To Fnum)
MyFiles(Fnum) = FilesInPath
FilesInPath = Dir()
Loop
'Change ScreenUpdating, Calculation and EnableEvents
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
Set BaseWks = Worksheets("Sheet1")
rnum = 5
'Loop through all files in the array(myFiles)
If Fnum > 0 Then
For Fnum = LBound(MyFiles) To UBound(MyFiles)
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(MyPath & MyFiles(Fnum))
On Error GoTo 0
If Not mybook Is Nothing Then
On Error Resume Next
With mybook.Worksheets(1)
Set sourceRange = .Range("B1:BB7")
End With
If Err.Number > 0 Then
Err.Clear
Set sourceRange = Nothing
Else
'if SourceRange use all columns then skip this file
If sourceRange.Columns.Count >= BaseWks.Columns.Count Then
Set sourceRange = Nothing
End If
End If
On Error GoTo 0
If Not sourceRange Is Nothing Then
SourceRcount = sourceRange.Rows.Count
If rnum + SourceRcount >= BaseWks.Rows.Count Then
MsgBox "Sorry there are not enough rows in the sheet"
BaseWks.Columns.AutoFit
mybook.Close savechanges:=False
GoTo ExitTheSub
Else
'Copy the file name in column A
With sourceRange
BaseWks.Cells(rnum, "C"). _
Resize(.Rows.Count).Value = MyFiles(Fnum)
End With
'Set the destrange
Set destrange = BaseWks.Range("G" & rnum)
'we copy the values from the sourceRange to the destrange
With sourceRange
Set destrange = destrange. _
Resize(.Rows.Count, .Columns.Count)
End With
destrange.Value = sourceRange.Value
rnum = rnum + SourceRcount + 5
End If
End If
mybook.Close savechanges:=False
End If
Next Fnum
BaseWks.Columns.AutoFit
End If
ExitTheSub:
'Restore ScreenUpdating, Calculation and EnableEvents
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
End Sub

Macro to find paragraphs in text selection in Open/Libre/Neo Office

I am trying to enumerate the paragraphs selected by the user in (Neo|Libre|Open)Office.
When I use the code below, modified version from here,
Sub CheckForSelection
Dim oDoc as Object
Dim oText
oDoc = ThisComponent
oText = oDoc.Text
if not IsAnythingSelected(oDoc) then
msgbox("No text selected!")
Exit Sub
end if
oSelections = oDoc.getCurrentSelection()
oSel = oSelections.getByIndex(0)
' Info box
'MsgBox oSel.getString(), 64, "Your Selection"
oPE = oSel.Text.createEnumeration()
nPars = 0
Do While oPE.hasMoreElements()
oPar = oPE.nextElement()
REM The returned paragraph will be a paragraph or a text table
If oPar.supportsService("com.sun.star.text.Paragraph") Then
nPars = nPars + 1
ElseIf oPar.supportsService("com.sun.star.text.TextTable") Then
nTables = nTables + 1
end if
Loop
' Info box
MsgBox "You selection has " & nPars & " paragraphs.", 64
end Sub
it finds ALL the paragraphs in the document, not just in the selection. Google has failed me. Any thoughts on how to find individual paragraphs in the selection?
The oSel.Text is a shortcut for oSel.getText() which "Returns the text interface in which the text position is contained." https://www.openoffice.org/api/docs/common/ref/com/sun/star/text/XTextRange.html#getText
So to get a ParagraphEnumeration only from the Selection, you should use oPE = oSel.createEnumeration() instead of oPE = oSel.Text.createEnumeration().

How to read quoted field from CSV using VBScript

In a sample.csv file, which has fixed number of columns, I have to extract a particular field value and store it in a variable using VBScript.
sample.csv
100,SN,100.SN,"100|SN| 435623| serkasg| 15.32|
100|SN| 435624| serkasg| 15.353|
100|SN| 437825| serkasg| 15.353|"," 0 2345"
101,SN,100.SN,"100|SN| 435623| serkasg| 15.32|
100|SN| 435624| serkasg| 15.353|
100|SN| 437825| serkasg| 15.353|"," 0 2346"
I want to parse the last two fields which are within double quotes and store them in two different array variables for each row.
You could try using an ADO connection
Option Explicit
dim ado: set ado = CreateObject("ADODB.Connection")
ado.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\txtFilesFolder\;Extended Properties=""text;HDR=No;FMT=Delimited"";"
ado.open
dim recordSet: set recordSet = ado.Execute("SELECT * FROM [samples.csv]")
dim field3, field4
do until recordSet.EOF
field3 = recordSet.Fields(3).Value
field4 = recordSet.Fields(4).Value
' use your fields here
recordSet.MoveNext
loop
recordSet.close
ado.close
You may have an issue if those fields are greater than 255 characters in length - if they are, they may return truncated. You also may have better luck with ODBC or ACE connection strings instead of the Jet one I've used here.
Since CSV's are comma-separated, you can use the Split() function to separate the fields into an array:
' Read a line from the CSV...
strLine = myCSV.ReadLine()
' Split by comma into an array...
a = Split(strLine, ",")
Since you have a static number of columns (5), the last field will always be a(4) and the second-to-last field will be a(3).
Your CSV data seems to contain 2 embedded hard returns (CR, LF) per line. Then the first line ReadLine returns is:
100,SN,100.SN,"100|SN| 435623| serkasg| 15.32|
The solution below unwraps these lines before extracting the required fields.
Option Explicit
Const ForReading = 1
Const ForAppending = 8
Const TristateUseDefault = 2 ' Opens the file using the system default.
Const TristateTrue = 1 ' Opens the file as Unicode.
Const TristateFalse = 0 ' Opens the file as ASCII.
Dim FSO, TextStream, Line, LineNo, Fields, Field4, Field5
ExtractFields "sample.csv"
Sub ExtractFields(FileName)
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FileExists(FileName) Then
Line = ""
LineNo = 0
Set TextStream = FSO.OpenTextFile(FileName, ForReading, False, TristateFalse)
Do While Not TextStream.AtEndOfStream
Line = Line & TextStream.ReadLine()
LineNo = LineNo + 1
If LineNo mod 3 = 0 Then
Fields = Split(Line, ",")
Field4 = Fields(3)
Field5 = Fields(4)
MsgBox "Line " & LineNo / 3 & ": " & vbNewLine & vbNewLine _
& "Field4: " & Field4 & vbNewLine & vbNewLine _
& "Field5: " & Field5
Line = ""
End If
Loop
TextStream.Close()
Else
MsgBox "File " & FileName & " ... Not found"
End If
End Sub
Here is an alternative solution that allows for single or multiline CSV records. It uses a regular expression which simplifies the logic for handling multiline records. This solution does not remove CRLF characters embedded in a record; I've left that as an exercise for you :)
Option Explicit
Const ForReading = 1
Const ForAppending = 8
Const TristateUseDefault = 2 ' Opens the file using the system default.
Const TristateTrue = 1 ' Opens the file as Unicode.
Const TristateFalse = 0 ' Opens the file as ASCII.
Dim FSO, TextStream, Text, MyRegExp, MyMatches, MyMatch, Field4, Field5
ExtractFields "sample.csv"
Sub ExtractFields(FileName)
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FileExists(FileName) Then
Set MyRegExp = New RegExp
MyRegExp.Multiline = True
MyRegExp.Global = True
MyRegExp.Pattern = """([^""]+)"",""([^""]+)"""
Set TextStream = FSO.OpenTextFile(FileName, ForReading, False, TristateFalse)
Text = TextStream.ReadAll
Set MyMatches = MyRegExp.Execute(Text)
For Each MyMatch in MyMatches
Field4 = SubMatches(0)
Field5 = SubMatches(1)
MsgBox "Field4: " & vbNewLine & Field4 & vbNewLine & vbNewLine _
& "Field5: " & vbNewLine & Field5, 0, "Found Match"
Next
Set MyMatches = Nothing
TextStream.Close()
Else
MsgBox "File " & FileName & " ... Not found"
End If
End Sub

Size of SSRS Report in Bytes is not correct for sending it to the printer

I want to send my SSRS report directly to Printer. For that I have rendered my report into array of bytes and then sent these array of Bytes to Printer.
When printing the report from SSRS print button the size of the report printed is exactly the size of the report. But the problem is: when printed through my code the size of the report is not correct.
Code to generate the bytes:
Private Function ExporttoPrinter()
'Start : Code to Check the Page Count -- Binit
Dim deviceInfo As String
Dim format As String = "IMAGE"
Dim encoding As String = ""
Dim mimeType As String = ""
Dim extension As String = ""
Dim warnings As Warning() = Nothing
Dim streamids As String() = Nothing
Dim bytes As Byte() = New Byte(0) {&H0}
Dim numberOfPages As Integer = 1
Dim pages As Byte()() = New [Byte](-1)() {}
Dim currentPageStream As Byte() = New Byte(0) {&H0}
While currentPageStream.Length > 0
deviceInfo = String.Format("<DeviceInfo><OutputFormat>EMF</OutputFormat><PrintDpiX>200</PrintDpiX><PrintDpiY>200</PrintDpiY> <StartPage>{0}</StartPage><PageWidth>15in</PageWidth><PageHeight>12in</PageHeight><MarginTop>0.5in</MarginTop><MarginLeft>1in</MarginLeft><MarginRight>0in</MarginRight><MarginBottom>0.5in</MarginBottom></DeviceInfo>", numberOfPages)
currentPageStream = ReportViewer1.ServerReport.Render(format, deviceInfo, mimeType, encoding, extension, streamids, warnings)
If currentPageStream.Length = 0 AndAlso numberOfPages = 1 Then
'renderException = EnumRenderException.ZERO_LENGTH_STREAM;
Exit While
End If
'Add the byte stream of current page in pages[] array so that we can have complete report in pages[][] array
If currentPageStream.Length > 0 Then
Array.Resize(pages, pages.Length + 1)
pages(pages.Length - 1) = currentPageStream
numberOfPages += 1
End If
End While
numberOfPages = numberOfPages - 1
' new Checking End
Dim objPrint As New clsReportPrinting
objPrint.PrintReport(pages, "CutePDF Writer")
Return True
End Function
Code To Sent Bytes into Printer:
Public Class clsReportPrinting
Private byaPagesToPrint()() As Byte = Nothing, _
m_oMetafile As Metafile = Nothing, _
m_iNumberOfPages As Int32 = 0, _
m_iCurrentPrintingPage As Int32 = 0, _
m_iLastPrintingPage As Int32 = 0, _
m_oCurrentPageStream As MemoryStream = Nothing, _
m_oDelegate As Graphics.EnumerateMetafileProc = Nothing
Public Function PrinterExists(ByVal PrinterName As String) As Boolean
'Returns TRUE if the named printer is amongst installed printers, FALSE otherwise
Try
PrinterName = PrinterName.Trim.ToUpper
For Each sPrinter As String In PrinterSettings.InstalledPrinters
'Printers may have UNC names, so we use .EndsWith to deal with \\Server\SharedPrinter
'if we pass "SharedPrinter" as PrinterName
If sPrinter.Trim.ToUpper.EndsWith(PrinterName) Then
Return True
End If
Next
Return False
Catch oEx As Exception
Throw (oEx)
Return False
End Try
End Function
Public Function MetafileCallback(ByVal oRecType As EmfPlusRecordType, _
ByVal iFlags As Int32, _
ByVal iDataSize As Int32, _
ByVal dpData As IntPtr, _
ByVal oCallbackData As PlayRecordCallback _
) As Boolean
Dim byaDataArray() As Byte = Nothing
If dpData <> IntPtr.Zero Then
'Copy unmanaged data to managed array for PlayRecord call
Array.Resize(Of Byte)(byaDataArray, iDataSize)
Marshal.Copy(dpData, byaDataArray, 0, iDataSize)
End If
'Play the record
m_oMetafile.PlayRecord(oRecType, iFlags, iDataSize, byaDataArray)
Return True
End Function
Private Function MoveToPage(ByVal lPage As Int32) As Boolean
'Check current page does exist
If Me.byaPagesToPrint(m_iCurrentPrintingPage - 1) Is Nothing Then
Return False
End If
'Set current page stream to desired rendered page
m_oCurrentPageStream = New MemoryStream(Me.byaPagesToPrint(m_iCurrentPrintingPage - 1))
'Set curernt stream position to its start
m_oCurrentPageStream.Position = 0
'Get rid of any former metafile
If Not m_oMetafile Is Nothing Then
m_oMetafile.Dispose()
m_oMetafile = Nothing
End If
'Set local metafile to page
m_oMetafile = New Metafile(m_oCurrentPageStream)
'Must always return TRUE
Return True
End Function
Public Sub pd_PrintPage(ByVal oSender As Object, _
ByVal oEV As PrintPageEventArgs)
oEV.HasMorePages = False
If (m_iCurrentPrintingPage <= m_iLastPrintingPage) And _
(MoveToPage(m_iCurrentPrintingPage)) Then
'Draw the page
DrawPage(oEV.Graphics)
'Point to next page
m_iCurrentPrintingPage += 1
'If there are more pages, flag so.
oEV.HasMorePages = (m_iCurrentPrintingPage <= m_iLastPrintingPage)
End If
End Sub
'This draws the current selected stream into a metafile
Public Sub DrawPage(ByVal oGrx As Graphics)
If m_oCurrentPageStream Is Nothing Or _
m_oCurrentPageStream.Length = 0 Or _
m_oMetafile Is Nothing Then
Return
End If
'Critical section follows (no more than one thread a time)
SyncLock Me
Dim iWidth As Int32 = m_oMetafile.Width, _
iHeight As Int32 = m_oMetafile.Height, _
oDestPoint As Point = Nothing
'Prepare metafile delegate
m_oDelegate = New Graphics.EnumerateMetafileProc(AddressOf MetafileCallback)
'Draw in the rectangle
oDestPoint = New Point(0, 0)
oGrx.EnumerateMetafile(m_oMetafile, oDestPoint, m_oDelegate)
'Clean up
m_oDelegate = Nothing
End SyncLock
End Sub
Public Function PrintReport(ByVal byaReport()() As Byte, _
ByVal sPrinterName As String _
) As Boolean
'Report data is an array of pages. Each page in turn is another byte array.
Me.byaPagesToPrint = byaReport
m_iNumberOfPages = Me.byaPagesToPrint.Length
Try
Dim oPS As PrinterSettings = New PrinterSettings
oPS.MaximumPage = m_iNumberOfPages
oPS.MinimumPage = 1
oPS.PrintRange = PrintRange.SomePages
oPS.FromPage = 1
oPS.ToPage = m_iNumberOfPages
oPS.PrinterName = sPrinterName
'oPS.DefaultPageSettings.Landscape = False
'oPS.DefaultPageSettings.Margins.Top = 0.5
'oPS.DefaultPageSettings.Margins.Bottom = 0.5
'oPS.DefaultPageSettings.Margins.Left = 0.83
'oPS.DefaultPageSettings.Margins.Right = 0
'oPS.DefaultPageSettings.PaperSize.Kind = PaperKind.Custom
oPS.DefaultPageSettings.PaperSize = New PaperSize("Custom", 1500, 1200)
'Dim value As Boolean
'value = oPS.LandscapeAngle
'oPS.LandscapeAngle = value
Dim oPD As PrintDocument = New PrintDocument
m_iCurrentPrintingPage = 1
m_iLastPrintingPage = m_iNumberOfPages
oPD.PrinterSettings = oPS
'Do print the report
AddHandler oPD.PrintPage, AddressOf Me.pd_PrintPage
oPD.Print()
Return True
Catch oEx As Exception
Throw (oEx)
End Try
End Function
'Private Function RenderReport(ByVal reportPath As String, ByVal parameters As ExecutionService.ParameterValue()) As Byte()()
' ' Private variables for rendering
' Dim historyId As String = Nothing
' Dim execHeader As New ExecutionService.ExecutionHeader()
' Try
' rs.Timeout = 300000
' rs.ExecutionHeaderValue = execHeader
' rs.LoadReport(reportPath, historyId)
' If (parameters IsNot Nothing) Then
' rs.SetExecutionParameters(parameters, "en_us")
' End If
' Dim pages As Byte()() = New [Byte](-1)() {}
' Dim format As String = "IMAGE"
' Dim numberOfPages As Integer = 1
' Dim currentPageStream As Byte() = New Byte(0) {&H0}
' ' put a byte to get the loop started
' Dim extension As String = Nothing
' Dim encoding As String = Nothing
' Dim mimeType As String = Nothing
' Dim streamIDs As String() = Nothing
' Dim warnings As ExecutionService.Warning() = Nothing
' While currentPageStream.Length > 0
' Dim deviceInfo As String = [String].Format("<DeviceInfo><OutputFormat>EMF</OutputFormat><PrintDpiX>200</PrintDpiX><PrintDpiY>200</PrintDpiY>" & "<StartPage>{0}</StartPage></DeviceInfo>", numberOfPages)
' 'rs.Render will render the page defined by deviceInfo's <StartPage>{0}</StartPage> tag
' currentPageStream = rs.Render(format, deviceInfo, extension, encoding, mimeType, warnings, _
' streamIDs)
' If currentPageStream.Length = 0 AndAlso numberOfPages = 1 Then
' 'renderException = EnumRenderException.ZERO_LENGTH_STREAM;
' Exit While
' End If
' 'Add the byte stream of current page in pages[] array so that we can have complete report in pages[][] array
' If currentPageStream.Length > 0 Then
' Array.Resize(pages, pages.Length + 1)
' pages(pages.Length - 1) = currentPageStream
' numberOfPages += 1
' End If
' End While
' numberOfPages = numberOfPages - 1
' Return pages
' Catch ex As System.Web.Services.Protocols.SoapException
' Console.WriteLine(ex.Detail.InnerXml)
' Catch ex As Exception
' Console.WriteLine(ex.Message)
' ' Console.WriteLine("Number of pages: {0}", pages.Length);
' Finally
' End Try
' Return Nothing
'End Function
End Class

Parsing POST request with unexpected URL encoding

This question follows an earlier one.
Here is some code that reproduces the problem:
POST:
str = "accountRequest=<NewUser>" & vbLf & _
"Hello" & vbTab & "World" & vbLf & _
"</NewUser>"
Set objHTTP = Server.CreateObject("Msxml2.ServerXMLHTTP.3.0")
objHTTP.open "POST", "service.asp", False
objHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objHTTP.send str
response.Write(objHTTP.responseText)
Set objHTTP = Nothing
service.asp:
function w (str)
response.Write(str & "<br>")
end function
str = request.Form("accountRequest")
w(str)
w("Tabs: "& InStr(str,vbTab))
w("Lines: "& InStr(str,vbLf))
output:
HelloWorld
Tabs: 0
Lines: 0
Can anyone please help?
Try:
Replace(Request.Form("accountRequest"), vbLF, vbCRLF))
Or:
Replace(Request.Form("accountRequest"), vbLF, "<br>"))|
Depending on where you're displaying it, either should work.
Or possibly this:
Function URLDecode(sConvert)
Dim aSplit
Dim sOutput
Dim I
If IsNull(sConvert) Then
URLDecode = ""
Exit Function
End If
' convert all pluses to spaces
sOutput = REPLACE(sConvert, "+", " ")
' next convert %hexdigits to the character
aSplit = Split(sOutput, "%")
If IsArray(aSplit) Then
sOutput = aSplit(0)
For I = 0 to UBound(aSplit) - 1
sOutput = sOutput & _
Chr("&H" & Left(aSplit(i + 1), 2)) &_
Right(aSplit(i + 1), Len(aSplit(i + 1)) - 2)
Next
End If
URLDecode = sOutput
End Function
From here: http://www.aspnut.com/reference/encoding.asp
If they are coming across the wire as the actual "\" and "n" characters, you can do a replace on those characters with the appropriate vbCRLF and vbTAB constants.
Finally figured out that ASP Request.Form method doesn't preserve tabs if they're in the "\t" format (as opposed to URL encoded). However, PHP's $_POST does.

Resources