LibreOffice Calc macro - How to remove header and footer in given sheet? - libreoffice

Default header and footer show up when I print out invoice created by macro.
I want to remove them in my macro.

I found this is the way to turn header and footer off:
Dim oDoc As Object
Dim oSheet As Object
Dim oStyles As Variant
Dim oPstyle As Variant
oDoc=ThisComponent
oSheet=oDoc.Sheets.getByName("Sheet1")
oStyles=oDoc.StyleFamilies.getByName("PageStyles")
oPstyle=oStyles.getByName(oSheet.PageStyle)
oPstyle.HeaderOn=FALSE
oPstyle.FooterOn=FALSE

Related

LibreOffice calc Hyperlink from macro doesn't work

I have trouble making hyperlink within macro.
For example just create empty spreadsheet and create another sheet within so we have 2 empty sheets.
In Sheet1 A:1 just paste =HYPERLINK("#Sheet2")
For A:3 create macro and execute it
Sub TestMacro
dim document as object
oSheet = thiscomponent.sheets.getByName("Sheet1")
oCell = oSheet.getCellRangeByName("$A$3")
xx="=HYPERLINK(""#Sheet2"")"
oCell.setString(xx)
End Sub
So sheet1 now have something on cell A1 and A3. the both should be link but just A1 works.
If jou press CTRL+click on Sheet1 A1 the Sheet2 is selected.
If you press CTRL+click on Sheet1 A3 nothing happens
I tested ou xubuntu 18.04LTS
LibreOffice Version: 6.0.7.3
Build ID: 1:6.0.7-0ubuntu0.18.04.10
For functions and calculations, set the cell formula instead of a literal string value.
oCell.setFormula(xx)

How to bold the text in TOC, but not the dot leader not the page number?

I have created a table of contents (TOC) to my Microsoft Office Word 2013 document using different types of headings linked to the TOC. When I try to modify my TOC 1 style so that only the text inside that TOC paragraph would be bolded, it makes the whole paragraph bolded. What I want to achieve is this:
ONLY THE TEXT HERE IS BOLDED................................1
rather than bolding the dot leader lines nor the page number like this
THE WHOLE PARAGRAPH IS BOLDED.........................1
I can do this manually, but everytime I update my TOC, all these changes go away (everything is bolded/not bolded again). How can I bold only only the text (not dot leaders or page number) inside TOC-style?
Fields throw away edits when updated - there's little you can do to change this. There is a * MergeFormat switch that can be added to field codes in order to retain formatting. The major drawback, however, is that it "remembers" formatting by character location. So if the text changes (gets shorter or longer) the formatting "goes crazy". Therefore, I don't think that will help you.
So all you can really do is re-apply the formatting after each TOC update. The fastest way to do that is using code (a macro, for example).
Here's a macro that works in a quick test on my system. It searches for each TAB character in the TableOfContents. Then, based on the additional information from the OP that only the second tab should be recognized, uses a second range to check whether the position AFTER the tab is within one centimeter of the right margin.
If that's the case, it repositions the find-target Range from that point back towards the beginning of the paragraph, then applies the formatting.
Note 1: You can change the "cushion" used to determine the distance to the right margin by changing the formula assigned to PageNumLimit.
Note 2: I defined a STYLE named Toc1_Text and use that, rather than formatting with BOLD directly. Doesn't really matter, but it feels "cleaner" to me than direct formatting :-)
Sub FormatTextInTOC()
Dim rngFind As word.Range, rngFound As word.Range
Dim bFound As Boolean
Dim toc As word.TableOfContents
Dim infoH As Double, pageNumLimit As Double
Set toc = ActiveDocument.TablesOfContents(1)
toc.Update
Set rngFind = toc.Range
pageNumLimit = rngFind.Sections(1).PageSetup.RightMargin _
- CentimetersToPoints(1)
With rngFind.Find
.ClearFormatting
.Text = vbTab
.Style = word.WdBuiltinStyle.wdStyleTOC1
Do
bFound = .Execute
If bFound Then
Set rngFound = rngFind.Duplicate
'rngFound.Select
rngFound.Collapse wdCollapseEnd
infoH = rngFound.Information(_
wdHorizontalPositionRelativeToTextBoundary)
If infoH >= pageNumLimit Then
'rngFind.Select
rngFind.Collapse wdCollapseStart
rngFind.MoveStart wdParagraph, -1
rngFind.Style = "Toc1_Text"
rngFind.Start = rngFind.Paragraphs(1).Range.End
'rngFind.Select
End If
End If
Loop While bFound
End With
End Sub

Change the caption in a form in VBA

I have created 2 forms in excel, userform1 and userform2. How do I set the caption of userform2 so that it displays whatever is in textbox1 of userform1 and updates itself when the value of textbox1 changes?
As Rory mentioned here just add it to the UserForm1 TextBox1 Change Sub.
Paste the script below into the code part of UserForm1.
Private Sub TextBox1_Change()
'Assign the TextBox Text to a variant
SomeText = TextBox1.Text
'Set the UserForm2 Caption to the variant
UserForm2.Caption = SomeText
End Sub
To paste the script into the code part of your UserForm, double click the Userform in the left side Window of VBA called "Projects Window" then press F7 button on your key board to access the code part of the UserForm then paste the code into the bottom of the page.

Run Word mail merge for one line only

First let me start by saying that I have no idea what I'm doing. Well, some idea, but mostly I'm just winging it. I've been working on an Excel macro to run a Word mail merge from Excel for a couple of weeks and I've pieced together different codes to encompass everything I need it to do. My problem is that it works the first time but it gets stuck each subsequent time if I do not reset the macro.
The sheet I made is designed so the macro can pull all of the information it needs from the sheet (code contains no hard coded locations). This is because a folder containing the Excel form, the letter templates and the finished letters will be sent to multiple users and will therefore be saved in a different location on each user's computer. The user enters details into the Excel form which will ultimately create a formatted disciplinary letter in a Word document to send out to the player being reviewed. There may need to be multiple infractions included in the letter so the Excel form provides that option, whereas a Word form would have a bunch of unnecessary empty fields. (I initially tried setting up the letter in Word as a form but I couldn't get all of the factors that I needed to work together) A Word form also doesn't have the ability to hide fields and can't support the dependent drop down lists or the amount of text in each drop down that I require. Anyway...
The process:
User opens the Excel file and enters information into a nicely laid out, user friendly form which contains dependent drop down lists, etc as well as buttons that hide and show fields (in case the user needs to include multiple infractions being reviewed).
When the user is finished entering their information, they click a command button to run the mail merge ("Create Disciplinary Letter").
The information they select in the form is linked to another worksheet (same workbook) called "Data Sheet" that contains the columns that the merge will pull from.
The workbook also contains a "Control Sheet" worksheet which provides the location of the files and folders that the macro is to pull from.
The macro should:
Open Word merge template (it chooses the correct template based on a choice that the user makes in the form)
Run the merge
Send merged product to a new document
Close the original merge template without saving changes
Save the new document with a specific file name (based on choices the user made in the original Excel form)
Save it to a "Final Documents" folder which is located in the original folder that I send out to the user(s).
The newly saved document/letter stays open for further editing (if necessary)
New doc contains a button that will save the finished letter as a .pdf (also to a specific location) but that macro is in Word so it is not part of my problem.
The Excel form would likely be closed and reopened before a user needs to use it again, and in that case, the macro would run fine. There is a good possibility though, that the user will see the finished letter in Word, realize that they forgot the include an infraction, go back to the open Excel form to add the infraction, and click the macro button again. If this happens, the macro will get stuck at macro process #4 onward (from list above). I don't know what is in the code (or missing) that is causing this but I've been fighting with it for days and I can't find anything out there that I can apply to my issue. Or maybe I have, but I'm unaware because I'm seriously just winging it.
Sub RunMerge()
Dim bCreatedWordInstance As Boolean
Dim wdapp As Word.Application
Dim wddoc As Word.Document
Dim rng1 As Range
Dim wb As Workbook
Dim wsControl As Worksheet
Dim wsData As Worksheet
Dim strWorkbookName As String
Dim strTemplateFolder As String
Dim strTemplateName As String
Dim lngTemplateNameColumn As Long
Dim strFinalDocumentFolder As String
Dim strFinalDocumentName As String
Dim lngDocumentNameColumn As Long
Dim lngRecordKount As Long ' not used but retained for future use
Set wb = ThisWorkbook
Set wsControl = wb.Worksheets("Control Sheet")
wsControl.Activate
strTemplateFolder = wsControl.[Template_Folder].Value
strFinalDocumentFolder = wsControl.[Document_Folder].Value
Set wsData = wb.Worksheets(wsControl.[Data_Sheet].Value)
wsData.Activate
lngTemplateNameColumn = wsData.[Template_Name].Column
lngDocumentNameColumn = wsData.[Document_Name].Column
Set rng1 = wsData.Range("B1:B8")
strTemplateName = strTemplateFolder & "\" & wsData.Cells(2, lngTemplateNameColumn) & ".doc"
strFinalDocumentName = strFinalDocumentFolder & "\" & wsData.Cells(2, lngDocumentNameColumn)
strWorkbookName = ThisWorkbook.Path & "\" & ThisWorkbook.Name\
On Error Resume Next
' Create a Word Application instance
bCreatedWordInstance = False
Set wdapp = GetObject(, "Word.application")
If wdapp Is Nothing Then
Err.Clear
Set wdapp = CreateObject("Word.Application")
bCreatedWordInstance = True
End If
If wdapp Is Nothing Then
MsgBox "Could not start Word"
Err.Clear
On Error GoTo 0
Exit Sub
End If
' Let Word trap the errors
On Error GoTo 0
' Set to True if you want to see the Word Doc flash past during construction
wdapp.Visible = True
' check that template exists
If Dir(strTemplateName) = "" Then
MsgBox strTemplateName & " not found"
End If
Set wddoc = wdapp.Documents.Open(strTemplateName)
If wddoc Is Nothing Then Set wddoc = wdapp.Documents.Open(strTemplateName)
wddoc.Activate
With wddoc
.MailMerge.OpenDataSource Name:=strWorkbookName, SQLStatement:="SELECT * FROM `Data Sheet$`"
With wddoc.MailMerge 'With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
.Execute Pause:=False
End With
End With
' Save new file
ActiveDocument.SaveAs strFinalDocumentName
' Close the New Mail Merged Document
If bCreatedWordInstance Then
wddoc.Close savechanges:=wdDoNotSaveChanges
Set wddoc = Nothing
End If
0:
Set wdapp = Nothing
Set rng1 = Nothing
Set wsData = Nothing
Set wsControl = Nothing
Set wb = Nothing
End Sub
It gets stuck the second time around on:
' Save new file
ActiveDocument.SaveAs strFinalDocumentName

Format (make bold or italics) a portion of a Textbox or Formula object in Crystal Reports

I am trying to format (bold or italics) only a portion of a textbox or Formula object (IFieldObject) in Crystal Reports for Visual Studio 2008.
I know how to make the entire field bold, but I want only a portion.
For example:
...blah blah blah May 12, 2009 blah blah blah...
Is this possible? I'm thinking there must be some sort of markup, but can't find any reference
to it.
You can do this by inserting HTML markup in the field (using a formula) and then displaying the new formula as a HTML field.
e.g.
Here's a basic Syntax formula that takes a field and adds the bold tag around the text before the colon.
dim sTmp as string
dim sLeft as string
dim sRight as string
dim sAll as string
sTmp = {yourtable.yourfield}
sLeft = (split(sTmp,":"))(1)
sRight = (split(sTmp,":"))(2)
sAll = "<b>"+sLeft+":</b>"+sRight
formula = sAll
If you place this new formula into the report and then ...
Right Click the field and choose "Format Field"
Change Text Interpretation to HTML Text
Click 'OK'
There are Gotchas here. The original text is not HTML encoded, and I'm sure my example code does a simple one-line thing in about ten lines. Also, if your field has no colons in it, you'll force an error. However, it should give you the right idea.
Create a formula for the date part, then embed it into a text box, then you can format any way you like
I don't know, if it is doable.
You could create a separate formula field for the value that you want to make bold & insert the formula field between other formula field, which will make it look like single sentence.