Completing Paragraph with character using macro in ODT - macros

Supposse that you have the next paragraph in openoffice
ESCRITURA PÚBLICA NÚMERO ESCRITURA PÚBLICA NÚMERO DDSDSSDDS/2015 ESCRITURA PÚBLICA DE DSDSSDDSPODER ESPECIAL, QUE OTORGA HEZ GONZALES A FAVOR DE CARLOS ESANCHEZ YADOS SANCHEZ.
How Can I to use macros to complete automatically the paragraph until before the break line using the character ====?. In other words the output of that macro will be:
ESCRITURA PÚBLICA NÚMERO ESCRITURA PÚBLICA NÚMERO DDSDSSDDS/2015 ESCRITURA PÚBLICA DE DSDSSDDSPODER ESPECIAL, QUE OTORGA HEZ GONZALES A FAVOR DE CARLOS ESANCHEZ YADOS SANCHEZ. ==============================

For a solution without macros, you could modify the paragraph style, using a right-aligned tabstop and a filling caracter. To do so, you should first create a new paragraph style:
Put the cursor into one of the lines to modify;
Open Menu Format -> Styles and Formatting, or hit F11;
Click on the rightmost button (with the little arrow pointing downwards), and select New Style from Selection:
Enter a name for the style, making managing styles easier.
Now, in the paragraph styles list, right-click on the new style, and select Modify... from the context menu;
On the Tabs tab, create a new tabstop with the following settings:
Take care to set the fill character (by default: empty). The position depends on the margin settings and paper size; with A4 paper (21 cm width) and 2cm borders, the right text margin is at 17 cm.
Now, put the cursor at the end of the line that should be filled with =, make sure that the right paragraphs style is active, and hit the Tab key to insert a tabulator. LO Writer will fill the line with the fill character set unto the end of the line:
If you need to add tabstops to multiple paragraphs, you could execute a search/replace, replacing the paragraph mark with a combination of tabstop/paragraph mark. You could also use search/replace to apply the new paragraph style.
Tested with LibreOffice Writer, but should work with OpenOffice, too.

Here is OpenOffice Basic code that does what you want:
Sub addEquals
oDoc = ThisComponent
oVC = oDoc.getCurrentController.getViewCursor
linenum_original = getLinenum(oVC)
For insertEqual = 1 to 255
oVC.collapseToEnd
oVC.gotoEndofLine(False)
oVC.getText().insertString(oVC, "=", False)
If getLinenum(oVC) <> linenum_original Then
' Remove the last =
oVC.gotoEndofLine(False)
oVC.goLeft(1, True)
oVC.setString("")
oVC.goRight(0, False)
oVC.collapseToEnd
Exit For
End If
Next insertEqual
End Sub
Function getLinenum(oVC)
nY = 0 'How many lines from top of page
nPage = oVC.getPage
while oVC.goUp(1, False) and oVC.getPage = nPage
nY = nY + 1
wend
oVC.goDown(nY, False)
getLinenum = nY
End Function
Position the cursor at the end of the paragraph ("SANCHEZ" in your example) and then run this macro.
The way it works is to add one "=" at a time and check if it is still on the same line. If it is on another line, then it stops.
It flashes a lot, so you may want to add code to lock the controllers while this is running. See https://wiki.openoffice.org/wiki/Writer/API/View_cursor.

Related

Change Alt Text for Multiple Form Control Checkboxes

Is there a way to update the alt text for multiple form control checkboxes at once using Excel VBA? I have about 20 check boxes on a worksheet {"Sheet1"} (Check Box 1, Check Box 2, ... Check Box 20) and need to change the text for all to = "In Progress". Thanks in advance!
"Finding and Replacing in Text Boxes
by Allen Wyatt
(last updated July 20, 2019)
Sub TextBoxReplace()
Dim shp As Shape
Dim sOld As String
Dim sNew As String
'Change as desired
sOld = "Old string"
sNew = "New string"
On Error Resume Next
For Each shp In ActiveSheet.Shapes
With shp.TextFrame.Characters
.Text = Application.WorksheetFunction.Substitute( _
.Text, sOld, sNew)
End With
Next
End Sub
This macro steps through all the shapes in the worksheet (text boxes are shapes) and then replaces whatever is in the sOld variable with whatever is in the sNew variable."
From excelribbon.tips.net
Disregard; I found a solution! ^_^
https://excelribbon.tips.net/T009264_Finding_and_Replacing_in_Text_Boxes.html

how to set paragraph column counts using MS Word

I just want to set the last para columns in below code:
wordDoc.Paragraphs.Last.Range.PageSetup.TextColumns.SetCount(3);
But it sets the page 3 columns;
So, what code can set the last para columns?
There is no such thing as a paragraph columns property - only a page columns property (TextColumns). If you want to limit the scope of TextColumns, insert a Section break before (and after, if applicable), the paragraph concerned. For example:
Sub Demo()
Dim wordDoc As Document
Set wordDoc = ActiveDocument
With wordDoc.Paragraphs.Last.Range
.InsertBreak Type:=wdSectionBreakContinuous
.PageSetup.TextColumns.SetCount (3)
End With
End Sub

insert and delete text after an Range-position in Word

I have a SET-field in Word 2007. After the set-field there could be everything (text,bookmark, SET field,...). I want to add a text (e.g. "exampletext") in between.
After this I want to delete this inserted text (but I don't want to search through the whole document).
Is there a method?
Trial 1 (it inserts it in the field - and not after the field):
' xStartReturn is a field
Dim myExampletext As WordApp.Range = objDoc.Range(xStartReturn.Code.End, xStartReturn.Code.End )
myExampletext.Text = "exampletext"
Trial 2 (leads to the problem that I don't get the Range-field to delete the exampletext afterwards):
xEndeReturn.insertAfter("exampletext")
Trial 3:
'xStartReturn.Code.End + 1 doesn't work.. but I found out that the "}"-Sign in the setField is +20 after xStartReturn.Code.End. Theoretical this should work - but there could be e.g. also paragraph afterwards.
'-> I can automatically check that there is a paragraph - but why is the exampletext added **after** the paragraph?
Dim example As WordApp.Range = objDoc.Range(xStartReturn.Code.End + 20, xStartReturn.Code.End + 20)
example.Text = "exampletext"
Dim later As WordApp.Range = objBasisvorlage_.Range(objXStartReturn.Code.End + 20, objXStartReturn.Code.End + 20 + "SDFSD".Length) 'this is wrong?!
later.Delete()
The following works for me. Since you didn't give us a minimum code with which to reproduce the problem I don't know how relevant the framework is that I used. But you should be able to follow the steps.
Watch what I do with r_f_Code (field code range). You can ignore/remove r_f_Result as I had that in for reference and debugging purposes.
Collapsing the field code range to its end-point leaves the range just within the field braces. Moving the starting point one character to the right puts it just outside the braces, but before anything else. (Note: I tested with two immediately adjacent SET fields.)
My code then enters some text and bookmarks it. That's the only way you do what you ask if what follows the SET field can be "anything". Although I suppose you could insert a Content Control - that would be uniquely identifiable if you go about it correctly...
Sub PositionAfterFieldCode()
Dim f As word.Field
Dim r_f_Code As word.Range, r_f_Result As word.Range
For Each f In ActiveDocument.Fields
If f.Type = wdFieldSet Then
Set r_f_Code = f.code
Set r_f_Result = f.result
'Debug.Print Len(r_f_Code), r_f_Code.Text, Len(r_f_Result), r_f_Result.Text
r_f_Code.Collapse wdCollapseEnd
r_f_Code.MoveStart wdCharacter, 1
'r_f_Code.Select
r_f_Code.Text = "abc"
r_f_Code.Bookmarks.Add "AfterSet", r_f_Code
Exit For
End If
Next
End Sub

Finding text AND fields with variable content in Word

I need to find and delete every occurrence of the following pattern in a Word 2010 document:
RPDIS→ text {INCLUDEPICTURE c:\xxx\xxx.png" \*MERGEFORMAT} text ←RPDIS
Where:
RPDIS→ and ←RPDIS are start and end delimiters
Between the start and end delimiters there can be just text or text and fields with variable content
The * wildcard in the Word Find and Replace dialog box will find the pattern if it contains text only but it will ignore patterns where text is combined with fields. And ^19 will find the field but not the rest of the pattern until the end delimiter.
Can anyone help, please?
Here's a VBA solution. It wildcard searches for RPDIS→*←RPDIS. If the found text contains ^19 (assuming field codes visible; if objects are visible instead of field codes, then the appropriate test is text contains ^01), the found text is deleted. Note that this DOES NOT care about the type of embedded field --- it will delete ANY AND ALL embedded fields that occur between RPDIS→ and ←RPDIS, so use at your own risk. Also, the code has ChrW(8594) and ChrW(8592) to match right-arrow and left-arrow respectively. You may need to change that if your arrows are encoded differently.
Sub test()
Dim wdDoc As Word.Document
Dim r As Word.Range
Dim s As String
' Const c As Integer = 19 ' Works when field codes are visible
Const c As Integer = 1 ' Works when objects are visible
Set wdDoc = ActiveDocument
Set r = wdDoc.Content
With r.Find
.Text = "RPDIS" & ChrW(8594) & "*" & ChrW(8592) & "RPDIS"
.MatchWildcards = True
While .Execute
s = r.Text
If InStr(1, s, chr(c), vbTextCompare) > 0 Then
Debug.Print "Delete: " & s
' r.Delete ' This line commented out for testing; remove comments to actively delete
Else
Debug.Print "Keep: " & s
End If
Wend
End With
End Sub
Hope that helps.

How to use non breaking space in iTextSharp

How can the non breaking space can be used to have a multiline content in a PdfPTable cell. iTextSharp is breaking down the words with the space characters.
The scenario is I want a multiline content in a table head, such as in first line it may display "Text1 &" and on second line it would display "Text", on rendering the PDF the Text1 is displayed in first line, then on second line & is displayed and on third it takes the length of the first line and truncates the remaining characters to the next line.
Or can I set specific width for each and every column of the table so as to accomodate text content within it, such as the text would wrap within that specific width.
You didn't specify a language so I'll answer in VB.Net but you can easily convert it to C# if needed.
To your first question, to use a non-breaking space just use the appropriate Unicode code point U+00A0:
In VB.Net you'd declare it like:
Dim NBSP As Char = ChrW(&HA0)
And in C#:
Char NBSP = '\u00a0';
Then you can just concatenate it where needed:
Dim Text2 As String = "This is" & NBSP & "also" & NBSP & "a test"
You might also find the non-breaking hyphen (U+2011) helpful, too.
To your second question, yes you can set the width of every column. However, column widths are always set as relative widths so if you use:
T.SetTotalWidth(New Single() {2.0F, 1.0F})
What you are actually saying is that for the given table, the first column should be twice as large as the second column, you are NOT saying that the first column is 2px wide and the second is 1px. This is very important to understand. The above code is the exact same as the next two lines:
T.SetTotalWidth(New Single() {4.0F, 2.0F})
T.SetTotalWidth(New Single() {100.0F, 50.0F})
The column widths are relative to the table's width which by default (if I remember correctly) is 80% of the writable page's width. If you would like to fix the table's width to an absolute width you need to set two properties:
''//Set the width
T.TotalWidth = 200.0F
''//Lock it from trying to expand
T.LockedWidth = True
Putting the above all together, below is a full working WinForms app targetting iTextSharp 5.1.1.0:
Option Explicit On
Option Strict On
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
''//File that we will create
Dim OutputFile As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "TableTest.pdf")
''//Standard PDF init
Using FS As New FileStream(OutputFile, FileMode.Create, FileAccess.Write, FileShare.None)
Using Doc As New Document(PageSize.LETTER)
Using writer = PdfWriter.GetInstance(Doc, FS)
Doc.Open()
''//Create our table with two columns
Dim T As New PdfPTable(2)
''//Set the relative widths of each column
T.SetTotalWidth(New Single() {2.0F, 1.0F})
''//Set the table width
T.TotalWidth = 200.0F
''//Lock the table from trying to expand
T.LockedWidth = True
''//Our non-breaking space character
Dim NBSP As Char = ChrW(&HA0)
''//Normal string
Dim Text1 As String = "This is a test"
''//String with some non-breaking spaces
Dim Text2 As String = "This is" & NBSP & "also" & NBSP & "a test"
''//Add the text to the table
T.AddCell(Text1)
T.AddCell(Text2)
''//Add the table to the document
Doc.Add(T)
Doc.Close()
End Using
End Using
End Using
Me.Close()
End Sub
End Class