RichText.WriteFile
------------------

Writes current rich text to a file.

| Component | Version | macOS | Windows | Linux | Server | iOS SDK |
|---|---|---|---|---|---|---|
| [RichText](component_RichText.md) | [4.4](newinversion44.md) | ✅ Yes | ❌ No | ❌ No | ✅ Yes, on macOS | ✅ Yes |

MBS( "RichText.WriteFile"; Path; Format ) 

**MBS**( **"RichText.WriteFile";** /\* Writes current rich text to a file. \*/  
**$Path**; /\* The native file path.e.g. "/Users/cs/Desktop/test.doc" \*/   
**$Format**) /\* The file format to use. Can be DOC, DOCX or RTF.e.g. "DOC" \*/ 

### Parameters

| Parameter | Description | Example |
|---|---|---|
| Path | The native file path. | "/Users/cs/Desktop/test.doc" |
| Format | The file format to use. Can be DOC, DOCX or RTF. | "DOC" |

### Result

Returns OK or error.

### Description

Writes current rich text to a file.  
Please pass format type:   
- doc: creates old style Word document
- rtf: created rich text file.
- docx: creates newer Word document.
- html: creates html file.
- webarchive: creates html and renders web archive.
- opendocument: creates open office document.

Warning: The plugin can only read/write the styles/formats which Apple supports for their cocoa framework. If the styles don't come through to Word formats, maybe try RTF instead and let Word open that?  
  
See [FileDialog](component_FileDialog.md) functions if you like to show a save dialog to pick a destination for the file path.  
  
This function requires a native path. Use [Path.FileMakerPathToNativePath](PathFileMakerPathToNativePath.md) to convert a FileMaker path to a native path if required. If you like to have the user choose the path, you can use [FileDialog](component_FileDialog.md) functions.  
For Server be aware that server has limited permissions and may not be able to access all files on a computer.  
### Examples

Read a file, apply replaces and save it:

 # load from container   
Set Variable \[$r ; Value:MBS ("[RichText.ReadContainer](RichTextReadContainer.md)"; Replace Text in Word File::InputContainer )\]  
If \[MBS ("IsError")\]  
 Show Custom Dialog \["Error"; "Failed to read file."\]  
 Exit Script \[\]  
End If  
\# now loop through records to do replaces   
Go to Record/Request/Page \[First\]  
Loop  
 Set Variable \[$r ; Value:MBS ("[RichText.ReplaceText](RichTextReplaceText.md)"; Replace Text in Word File::Old Text; Replace Text in Word File::New Text)\]  
 Go to Record/Request/Page \[Next; Exit after last\]  
End Loop  
\# write file   
Set Variable \[$r ; MBS ("RichText.WriteFile"; Replace Text in Word File::path ; Replace Text in Word File::format )\]  
\# cleanup   
Set Variable \[$r ; Value:MBS ("[RichText.Release](RichTextRelease.md)")\]  
Save as docx, the newer XML based Word file format:

 # Word, new format   
Set Variable \[ $path ; Value: $folder &amp; "/test.docx" \]   
Set Variable \[ $r ; Value: MBS ( "RichText.WriteFile"; $path ; "docx" ) \]  
Save an old doc:

 # Word, old format   
Set Variable \[ $path ; Value: $folder &amp; "/test.doc" \]   
Set Variable \[ $r ; Value: MBS ( "RichText.WriteFile"; $path ; "doc" ) \]  
Save as RTF:

 # RTF   
Set Variable \[ $path ; Value: $folder &amp; "/test.rtf" \]   
Set Variable \[ $r ; Value: MBS ( "RichText.WriteFile"; $path ; "rtf" ) \]  
### See also

- [IsError](IsError.md)
- [Path.FileMakerPathToNativePath](PathFileMakerPathToNativePath.md)
- [RichText.ReadContainer](RichTextReadContainer.md)
- [RichText.ReadFile](RichTextReadFile.md)
- [RichText.Release](RichTextRelease.md)
- [RichText.ReplaceText](RichTextReplaceText.md)
- [RichText.WriteDOC](RichTextWriteDOC.md)
- [RichText.WritePDF](RichTextWritePDF.md)
- [RichText.WriteRTF](RichTextWriteRTF.md)

### Example Databases

- [Mac only/RichText/Write Document](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Mac%20only/RichText/Write%20Document.shtml#1ScriptAnchor_)

This function checks for a license.

Created 16th October 2014 , last changed 22nd December 2022

  
[RichText.WriteDOC](RichTextWriteDOC.md) - [RichText.WritePDF](RichTextWritePDF.md)

[HTML Version](RichTextWriteFile.shtml)