|
|
|
|
- Overview
- Handling the Word Processing control through programming
- Opening and creating a docx document in a Word Processing control
- Saving a docx document
- Performing a search in a document and modifying the search result
- Handling the selection
- Adding text into a document
- Adding a page break into a document
- Management of bookmarks
- Sequencing reports and Word Processing documents
- Word Processing control properties
Handling Word Processing controls programmatically
A Word Processing control can be handled through programming. To handle the Word Processing control, WLanguage proposes: - a set of functions used to perform all the common operations (direct programming via the WLanguage DocXXX functions. For more details, see Word Processing control functions.
- an advanced programming via the different advanced types of WLanguage used to directly handle the content of the docx file displayed by the Word Processing control. For more details, see Types associated with the Word Processing control.
This help page explains how to handle a Word Processing control through programming.
Handling the Word Processing control through programming Opening and creating a docx document in a Word Processing control To open an existing docx document through programming in a Word Processing control, you can: - assign the docx file to the Word Processing control. For example:
// Sélectionne un fichier .docx dans le répertoire en cours sNomFichier is string sNomFichier = fSelect("", "présentation.docx", ... "Sélection de fichier DocX", ... "Fichier Docx (*.docx)" + TAB + "*.docx", "*.docx", fselOpen) IF sNomFichier ~= "" THEN // Aucun fichier sélectionné RETURN END // Chargement du fichier docx TT_MonDocument = sNomFichier - use DocOpen.
For example:
TT_MonDocument = DocOpen("C:\Users\test\Documents\fichier.docx") IF ErrorOccurred() THEN Error(ErrorInfo()) RETURN - use a Document variable and assign it to the Word Processing control.
For example:
MonDocument is Document MonDocument = "C:\temp\presentation.docx" TT_MonDocument = MonDocument or:
MonDocument is Document MonDocument = DocOpen("C:\Users\test\Documents\fichier.docx") IF ErrorOccurred() THEN Error(ErrorInfo()) ELSE TT_MonDocument = MonDocument END
Caution: only the files in docx format can be handled by the Word Processing control. Tip: To display an empty document in a Word Processing control, all you have to do is assign an empty string to the Word Processing control. Example: // Création d'un document vierge TT_MonDocument = "" Saving a docx document To save a docx document, all you have to do is use DocSave. Example:
// Choose the directory and the backup name sFileName is string = fSelect(CompleteDir(fExeDir()),"presentation.docx", ... "Selecting DocX files","Docx file (*.docx)" + TAB + "*.docx" , "*.docx", fselCreate) // Save the file DocSave(WP_NoName1, sFileName) IF ErrorOccurred THEN Error(StringBuild("The %1 file was not saved.", sFileName), ErrorInfo()) RETURN END Remarks: - The content of Word Processing control can also be:
- DocToText is used to retrieve the document text.
- DocToImage is used to convert a document page into image.
Performing a search in a document and modifying the search result To perform a search in a Word Processing document, all you have to do is use DocFind. Example: // Find "BEAUTIFUL" in the text // Selects the first one found to change its color arrFragments is array of docFragment = DocFind(WP_NoName1, "BEAUTIFUL") IF arrFragments.Count >= 1 THEN // Position the cursor at the beginning of the first word found WP_NoName1.Cursor = arrFragments[1].StartPosition // Calculate the selection length WP_NoName1.SelectionLength = ... arrFragments[1].EndPosition - arrFragments[1].StartPosition+1 // Modify the text color arrFragments[1].Formatting.TextColor = PastelRed BREAK END In this code, the docFragment variable corresponds to the text section containing the searched word, as well as its position in the entire text. Then, you have the ability to handle the fragment found. Handling the selection To handle the selection currently performed in a Word Processing control, you have the ability to retrieve this selection in a docFragment variable. The following code is used to change the color of selected text. Example: // Change the selection color // Get the current selection MySelection is docFragment(WP_MyDocument, WP_MyDocument.Cursor, ... WP_MyDocument.SelectionLength) // Change the color MySelection.Formatting.TextColor = PastelRed The docFragment variable owns a specific constructor used to easily retrieve the selection. Adding text into a document Several methods can be used to add text into an existing document: - Inserting a fragment at the desired position. Example:
// Insère un texte à la fin du document frag2 is docFragment(TT_MonDoc, -1 , 0) frag2.Text += "Texte de fin" - Inserting a text from a given position with DocInsert.
DocInsert(TT_Document, TT_Document.Curseur, "Mon texte inséré") - Adding a text at the end of document with DocAdd.
To add text into an empty document, you can: - use the direct assignment of control:
<Champ Traitement de texte> = "Texte à insérer" Example:
TT_Mondocument = "Je suis la première phrase de ce document." - add the text with DocAdd.
- insert a fragment in first position. Example:
// sTexteOriginal contient le texte à insérer frag is docFragment(TT_MonDoc, TT_MonDoc.Curseur, 0) frag.Text += sTexteOriginal - insert a text from position 1 with DocInsert.
DocInsert(TT_Document, 1, "Mon texte inséré")
Adding a page break into a document The following code is used to add a page break into a document: // Ajoute un saut de page DocAdd(TT_SansNom1, Charact(12)) You can also use the following constants to add the desired type of break to a document or fragment: - docColumnBreak: Adds a column break in a multicolumn section. If the section is not multicolumn, a page break is added.
- docLineBreak: Adds a line break.
- docPageBreak: Adds a page break.
- docParagraphBreak: Adds a paragraph break.
Example: // Ajoute un saut de page DocAdd(TT_SansNom1, docPageBreak + "Texte sur la page suivante") Management of bookmarks You have the ability to manage bookmarks in a Word Processing document. For example: - Add a bookmark: Adds a bookmark named MyBookmark with the selected text.
doc is Document doc <- TT_MonDocument fragSélection is docFragment = TT_MonDocument.Sélection.Fragment doc.Bookmark["MonSignet"] = fragSélection - Deleting a bookmark: Deletes the bookmark named Bookmark1:
doc is Document doc <- TT_MonDocument Delete(doc.Bookmark, "Signet1") - Listing the bookmarks: Lists the document bookmarks:
doc is Document doc <- TT_MonDocument FOR EACH uUnSignet,NomSignet of doc.Bookmark Trace(NomSignet) // Nom du signet Trace(uUnSignet.Texte) // Texte du signet END - Inserting a text at the end of bookmark:
doc is Document <- TT_MonDocument.Valeur // Recherche de la position du signet fragmentSignet is docFragment = doc.Bookmark["Signet 1"] IF fragmentSignet <> Null THEN // Insertion du texte à la fin du signet let nPositionInsertion = fragmentSignet.EndPosition // Insertion à proprement dite DocInsert(TT_MonDocument, nPositionInsertion, "Texte à insérer à la position du signet") ELSE Error("Signet 'Signet 1' non trouvé dans le document") END
Sequencing reports and Word Processing documents You now have the ability to sequence reports and Word Processing documents. To do so, iSequencingAddDoc must be used in addition to the standard functions for creating sequences of reports. For example:
MyDocument is Document = "c:\temp\generalconditions.docx" // Print in the report viewer iDestination(iViewer) // Add reports into the sequence iSequencingAdd(RPT_Report1) iSequencingAdd(RPT_Report2, 3) // Add the general conditions in document format iSequencingAddDoc(MyDocument) iSequencingPrint() Word Processing control properties The following properties are specific to the Word Processing control. They are mainly used to handle the control characteristics:
| | | The AutoCorrect property allows you to define a set of automatic corrections to be applied in a Word Processing control as the user types. | AutomaticLink | The AutomaticLink property is used to: - determine if the automatic link detection mode is enabled,
- enable or disable the automatic link detection mode.
| DisplayBookmarks | The DisplayBookmarks property is used to: - identify the display mode of bookmarks in a Word Processing control.
- show or hide bookmarks in a Word Processing control.
| DisplayFormattingMarks | The DisplayFormattingMarks property is used to: - Determine if formatting marks are shown or hidden in a Word Processing control.
- Show or hide formatting marks in a Word Processing control.
| DisplayMode | The DisplayMode property gets and changes: - the display mode in a Word Processing control,
- the display mode in a PDF Reader control
- the display mode in an HTML Editor control,
- the display mode in a Kanban control
| FilePath | The FilePath property is used to identify: - the name of the xlsx file associated with a Spreadsheet control.
- the name of the file associated with an Image Editor control.
- the name of the PDF file associated with a PDF Reader control.
- the name of the DOCX file associated with a Word Processing control.
- the name of the wddiag file associated with a Diagram Editor control.
| FormattingMarksColor | The FormattingMarksColor property is used to: - get the color of the formatting marks in a Word Processing control.
- change the color of the formatting marks in a Word Processing control.
| HTMLEdit | The HTMLEdit property is used to: - find out whether a Word Processing control is displayed in optimized mode for HTML editing,
- modify a Word Processing control to display it (or not) in optimized mode for HTML editing.
| NumberAccessiblePages | The NumberAccessiblePages property is used to get the number of pages currently loaded in a PDF Reader or Word Processing control. | NumberDisplayedPage | The NumberDisplayedPage property is used to identify and change the number of the page currently displayed in the PDF Reader or Word Processing control. | NumberPage | The NumberPage property is used to get: - the number of pages in a "multi-page" image file. This image is displayed in an Image control or in the background of a Chart control.
- the number of pages in a PDF file displayed in an Image control.
- the number of pages found in a PDF file displayed in a PDF Reader control.
- the number of pages found in a DOCX file displayed in a Word Processing control.
| RuleVisible | The RuleVisible property is used to: - Find out whether the rulers are visible or invisible in a Word Processing control.
- Make the rulers visible or invisible in a Word Processing control.
| Selection | The Selection property is used to get the characteristics of the selection (or cursor): - in a Word Processing control.
Remark: This selection is in the section being edited in the control (body, header or footer). - in a Spreadsheet control.
- in an HTML Editor control.
- in a Diagram Editor control.
| SelectionLength | The SelectionLength property is used to get and change the length of the selection made in a Word Processing control. | ToolbarVisible | The ToolbarVisible property is used to: - know if the toolbar or the ribbon is displayed in a control.
- show or hide the toolbar or the ribbon in a control.
|
Related Examples:
|
Unit examples (WINDEV): The Word Processing control
[ + ] This example explains how to handle a Word Processing control by programming. You have the ability to add text and to format it by programming.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|