PC SOFT

ONLINE HELP
FOR WINDEV, WEBDEV AND WINDEV MOBILE

Home | Sign in | English US
  • Overview
  • Handling the Word Processing control by 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
  • Properties associated with the Word Processing control
WINDEV
WindowsLinuxUniversal Windows 10 AppJavaReports and QueriesUser code (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Browser code
WINDEV Mobile
AndroidAndroid Widget iPhone/iPadApple WatchUniversal Windows 10 AppWindows Mobile
Others
Stored procedures
Overview
A Word Processing control can be handled by programming.
To handle the Word Processing control, WLanguage proposes:
This help page explains how to handle a Word Processing control by programming.
Caution: This documentation presents the last features of the Word Processing control. Make sure that all the necessary modules are updated.
Handling the Word Processing control by programming

Opening and creating a docx document in a Word Processing control

To open an existing docx document by programming in a Word Processing control, you can:
  • assign the Word Processing control with the docx file. For example:
    // Selects a .docx file in the current directory
    sFileName is string
    sFileName = fSelect("", "presentation.docx", ...
    "Selecting DocX files", ...
    "Docx file (*.docx)" + TAB + "*.docx", "*.docx", fselOpen)

    IF sFileName ~= "" THEN
    // No selected file
    RETURN
    END

    // Load the docx file
    WP_MyDocument = sFileName
  • use DocOpen. For example:
    WP_MyDocument = DocOpen("C:\Users\test\Documents\file.docx")

    IF ErrorOccurred() THEN
    Error(ErrorInfo())
    RETURN
  • use a Document variable and assign it to the Word Processing control. For example:
    MyDocument is Document
    MyDocument = "C:\temp\presentation.docx"
    WP_MyDocument = MyDocument

    or:
    MyDocument is Document
    MyDocument = DocOpen("C:\Users\test\Documents\file.docx")
    IF ErrorOccurred() THEN
    Error(ErrorInfo())
    ELSE
    WP_MyDocument = MyDocument
    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:
// Create a blank document
WP_MyDocument = ""

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
Notes:
  • 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.
    This function is available from version 220052s (update module of Word Processing control applicable to WINDEV Update 1).

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 DocSeek.
Example:
// Seeks "BEAUTIFUL" in the text
// Selects the first one found to change its color
arrFragments is array of docFragments = DocSeek(WP_NoName1, "BEAUTIFUL")

IF arrFragments..Occurrence >= 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 = abFragments[1]..EndPosition - abFragments[1]..StartPosition+1
// Modify the text color
abFragments[1]..Style..TextColor = PastelRed
BREAK
END
// Seeks "BEAUTIFUL" in the text
// Selects the first one found to change its color
arrFragments is array of docFragments = DocSeek(WP_NoName1, "BEAUTIFUL")

IF arrFragments..Occurrence >= 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 sought 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..Style..TextColor = PastelRed
// 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 requested position. Example:
    // Inserts a text at the end of document
    frag2 is DocFragment(WP_MyDoc, (Length(DocToText(WP_MyDoc) + 1)), 0)
    frag..Text += "End text"
  • Inserting a text from a given position with DocInsert.
    DocInsert(WP_Document, WP_Document..Cursor, "My inserted text")
    This feature is available from version 220052s (update module of Word Processing control applicable to WINDEV Update 1).
To add text into an empty document, you can:
  • use the direct assignment of control:
    <Word Processing control> = "Text to insert"
    Example:
    WP_MyDocument = "I am the first sentence of this document."
  • insert a fragment in first position. Example:
    // sInitialText contains the text to insert
    frag is DocFragment(WP_MyDoc, WP_MyDoc..Cursor, 0)
    frag..Text += sInitialText
  • insert a text from position 1 with DocInsert.
    DocInsert(WP_Document, 1, "My inserted text")
    This feature is available from version 220052s (update module of Word Processing control applicable to WINDEV Update 1).
Several methods can be used to add text into an existing document:
  • Inserting a fragment at requested position. Example:
    // Inserts a text at the end of document
    frag2 is DocFragment(WP_MyDoc, -1 , 0)
    frag..Text += "End text"
  • Inserting a text from a given position with DocInsert.
    DocInsert(WP_Document, WP_Document..Cursor, "My inserted text")
    This feature is available from version 220052s (update module of Word Processing control applicable to WINDEV Update 1).
  • 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:
    <Word Processing control> = "Text to insert"
    Example:
    WP_MyDocument = "I am the first sentence of this document."
  • add the text with DocAdd.
  • insert a fragment in first position. Example:
    // sInitialText contains the text to insert
    frag is DocFragment(WP_MyDoc, WP_MyDoc..Cursor, 0)
    frag..Text += sInitialText
  • insert a text from position 1 with DocInsert.
    DocInsert(WP_Document, 1, "My inserted text")
    This feature is available from version 220052s (update module of Word Processing control applicable to WINDEV Update 1).

Adding a page break into a document

The following code is used to add a page break into a document:
// Adds a page break
DocAdd(WP_NoName, Charact(12))
Versions 23 and later

Management of bookmarks

You have the ability to manage bookmarks in a Word Processing document. Some examples:
  • Adding a bookmark: Adds a bookmark named MyBookmark with the selected text.
    doc is Document
    doc <- WP_MyDocument
    fragSelection is DocFragment = WP_MyDocument..Selection..Fragment
    doc..Bookmark["MyBookmark"] = fragSelection
  • Deleting a bookmark: Deletes the bookmark named Bookmark1:
    doc is Document
    doc <- WP_MyDocument
    Delete(doc.Bookmark, "Bookmark1")
  • Listing the bookmarks: Lists the document bookmarks:
    doc is Document
    doc <- WP_MyDocument
    FOR EACH uABookmark,BookmarkName of doc..Bookmark
    Trace(BookmarkName) // Bookmark name
    Trace(uABookmark..Text) // Bookmark text
    END
  • Inserting a text at the end of bookmark:
    doc is Document <- WP_MyDocument..Value
    // Find the bookmark position
    fragmentBookmark is docFragment = doc..Bookmark["Bookmark 1"]
    IF fragmentBookmark <> Null THEN
    // Insert text at the end of bookmark
    let nInsertionPosition = fragmentBookmark..EndPosition
    // Actual insertion
    DocInsert(WP_MyDocument, nInsertionPosition, "Text to insert at bookmark position")
    ELSE
    Erreur("'Bookmark 1' bookmark not found in the document")
    END
New in version 23

Management of bookmarks

You have the ability to manage bookmarks in a Word Processing document. Some examples:
  • Adding a bookmark: Adds a bookmark named MyBookmark with the selected text.
    doc is Document
    doc <- WP_MyDocument
    fragSelection is DocFragment = WP_MyDocument..Selection..Fragment
    doc..Bookmark["MyBookmark"] = fragSelection
  • Deleting a bookmark: Deletes the bookmark named Bookmark1:
    doc is Document
    doc <- WP_MyDocument
    Delete(doc.Bookmark, "Bookmark1")
  • Listing the bookmarks: Lists the document bookmarks:
    doc is Document
    doc <- WP_MyDocument
    FOR EACH uABookmark,BookmarkName of doc..Bookmark
    Trace(BookmarkName) // Bookmark name
    Trace(uABookmark..Text) // Bookmark text
    END
  • Inserting a text at the end of bookmark:
    doc is Document <- WP_MyDocument..Value
    // Find the bookmark position
    fragmentBookmark is docFragment = doc..Bookmark["Bookmark 1"]
    IF fragmentBookmark <> Null THEN
    // Insert text at the end of bookmark
    let nInsertionPosition = fragmentBookmark..EndPosition
    // Actual insertion
    DocInsert(WP_MyDocument, nInsertionPosition, "Text to insert at bookmark position")
    ELSE
    Erreur("'Bookmark 1' bookmark not found in the document")
    END

Management of bookmarks

You have the ability to manage bookmarks in a Word Processing document. Some examples:
  • Adding a bookmark: Adds a bookmark named MyBookmark with the selected text.
    doc is Document
    doc <- WP_MyDocument
    fragSelection is DocFragment = WP_MyDocument..Selection..Fragment
    doc..Bookmark["MyBookmark"] = fragSelection
  • Deleting a bookmark: Deletes the bookmark named Bookmark1:
    doc is Document
    doc <- WP_MyDocument
    Delete(doc.Bookmark, "Bookmark1")
  • Listing the bookmarks: Lists the document bookmarks:
    doc is Document
    doc <- WP_MyDocument
    FOR EACH uABookmark,BookmarkName of doc..Bookmark
    Trace(BookmarkName) // Bookmark name
    Trace(uABookmark..Text) // Bookmark text
    END
  • Inserting a text at the end of bookmark:
    doc is Document <- WP_MyDocument..Value
    // Find the bookmark position
    fragmentBookmark is docFragment = doc..Bookmark["Bookmark 1"]
    IF fragmentBookmark <> Null THEN
    // Insert text at the end of bookmark
    let nInsertionPosition = fragmentBookmark..EndPosition
    // Actual insertion
    DocInsert(WP_MyDocument, nInsertionPosition, "Text to insert at bookmark position")
    ELSE
    Erreur("'Bookmark 1' bookmark not found in the 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"

iPreview()
// Add reports into the sequence
iSequencingAdd(RPT_Report1)
iSequencingAdd(RPT_Report2, 3)
// Add the general conditions in document format
iSequencingAddDoc(MyDocument)
iSequencingPrint()
See Sequencing reports for more details.

Properties associated with the Word Processing control

The following properties are specific to the Word Processing control. They are mainly used to handle the control characteristics:
Versions 22 and later
DisplayControlCharacters
New in version 22
DisplayControlCharacters
DisplayControlCharacters
..DisplayControlCharacters is used to
  • Find out whether the display of control characters is enabled in a Word Processing control
  • Enable (or not) the display of control characters in a Word Processing control.
Versions 23 and later
DisplayMode
New in version 23
DisplayMode
DisplayMode
..DisplayMode is used to find out and modify the display mode in a Word Processing control or PDF Reader control.
Versions 23 and later
FilePath
New in version 23
FilePath
FilePath
..FilePath is used to find out:
  • the name of xlsx file associated with a Spreadsheet control. This name is initialized by SpreadsheetLoad and SpreadsheetSave.
  • the name of file associated with an Image Editor control. This name is initialized by PicOpen and PicSave.
  • the name of PDF file associated with a PDF Reader control. This name is initialized by PDFReaderOpen.
  • the name of DOCX file associated with a Word Processing control. This name is initialized by DocOpen and DocSave.
Versions 23 and later
HTMLEdit
New in version 23
HTMLEdit
HTMLEdit
..HTMLEdit is used to:
  • find out whether a Word Processing control is displayed in optimized mode for the HTML edit,
  • modify a Word Processing control to display it (or not) in optimized mode for the HTML edit.
Versions 23 and later
NumberAccessiblePages
New in version 23
NumberAccessiblePages
NumberAccessiblePages
..NumberAccessiblePages is used to find out the number of pages currently loaded in the PDF Reader control or in the Word Processing control.
Versions 23 and later
NumberDisplayedPage
New in version 23
NumberDisplayedPage
NumberDisplayedPage
..NumberDisplayedPage is used to find out and modify the number of the page currently displayed in the PDF Reader control or in the Word Processing control.
Versions 09 and later
NumberPage
New in version 09
NumberPage
NumberPage
..NumberPage is used to find out:
  • the number of pages found 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 found in a PDF file displayed in an Image control.
  • Versions 23 and later
    WINDEV the number of pages found in a PDF file displayed in a PDF Reader control.
    New in version 23
    WINDEV the number of pages found in a PDF file displayed in a PDF Reader control.
    WINDEV the number of pages found in a PDF file displayed in a PDF Reader control.
  • Versions 23 and later
    WINDEV the number of pages found in a DOCX file displayed in a Word Processing control.
    New in version 23
    WINDEV the number of pages found in a DOCX file displayed in a Word Processing control.
    WINDEV the number of pages found in a DOCX file displayed in a Word Processing control.
Versions 22 and later
RuleVisible
New in version 22
RuleVisible
RuleVisible
..RuleVisible 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.
Versions 22 and later
Selection
New in version 22
Selection
Selection
..Selection is used to find out and modify the characteristics of selection (or cursor) in the Word Processing control.
Versions 22 and later
SelectionLength
New in version 22
SelectionLength
SelectionLength
..SelectionLength is used to find out and modify the length of the selection performed in a Word Processing control.
Versions 20 and later
ToolbarVisible
New in version 20
ToolbarVisible
ToolbarVisible
..ToolbarVisible is used to
  • in a Spreadsheet control
    • Find out whether the toolbar is displayed
    • Display (or not) the toolbar.
  • in a Word Processing control
    • Find out whether the ribbon is displayed
    • Display (or not) a ribbon.
To find out the entire list of WLanguage properties that can be used with a Word Processing control, see Properties associated with the Word Processing control.
Related Examples:
The Word Processing control 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.
Minimum required version
  • Version 22
Comments
Click [Add] to post a comment