Platforms to show: All Mac Windows Linux Cross-Platform
/Tools/WordFile/WordFile filter paragraphs
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /Tools/WordFile/WordFile filter paragraphs
This example is the version from Sun, 20th Jan 2018.
Project "Wordfile filter paragraphs.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class MainWindow Inherits Window
Control List Inherits Listbox
ControlInstance List Inherits Listbox
EventHandler Function CellClick(row as Integer, column as Integer, x as Integer, y as Integer) As Boolean
myColumn = column
End EventHandler
EventHandler Sub DoubleClick()
MsgBox List.Cell(List.ListIndex, myColumn)
End EventHandler
EventHandler Sub Open()
me.ColumnType(0) = me.TypeCheckbox
End EventHandler
End Control
Control LoadButton Inherits PushButton
ControlInstance LoadButton Inherits PushButton
EventHandler Sub Action()
dim f as FolderItem = GetOpenFolderItem(FileTypes1.DOCX)
if f = nil then return
load f
End EventHandler
End Control
Control SaveButton Inherits PushButton
ControlInstance SaveButton Inherits PushButton
EventHandler Sub Action()
dim f as FolderItem = GetSaveFolderItem(FileTypes1.DOCX, "save.docx")
if f = nil then Return
save f
End EventHandler
End Control
Control Label1 Inherits Label
ControlInstance Label1 Inherits Label
End Control
Function FindNode(x as XmlNode, name as string) As XmlNode
if x <> nil then
dim c as XmlNode = x.FirstChild
while c <> nil
if c.LocalName = name then
Return c
end if
c = c.NextSibling
wend
end if
End Function
Function FindTexts(x as XmlNode) As string
dim s as string
if x <> nil then
dim c as XmlNode = x.FirstChild
while c <> nil
if c isa XmlTextNode then
dim t as string = c.Value
if len(t) > 0 then
s = s + t + EndOfLine
end if
else
s = s + FindTexts(c)
end if
c = c.NextSibling
wend
Return s
end if
End Function
Sub Load(f as FolderItem)
List.DeleteAllRows
dim b as BinaryStream = BinaryStream.Open(f)
dim d as string = b.Read(b.Length)
WordFile = WordFileMBS.OpenData(d)
dim x as string = WordFile.XML
dim doc as new XmlDocument(x)
dim documentNode as XmlNode = FindNode(doc, "document")
dim bodyNode as XmlNode = FindNode(documentNode, "body")
dim i as integer = 0
dim p as XmlNode = bodyNode.FirstChild
while p <> nil
if p.LocalName = "p" then
// Paragraph
dim s as string = FindTexts(p)
List.AddRow s
list.RowTag(List.LastIndex) = i // save index in nodes
List.CellCheck(List.LastIndex,0) = true
end if
i = i + 1
p = p.NextSibling
wend
End Sub
Sub Save(f as FolderItem)
dim x as string = WordFile.XML
dim doc as new XmlDocument(x)
dim documentNode as XmlNode = FindNode(doc, "document")
dim bodyNode as XmlNode = FindNode(documentNode, "body")
dim NodesToRemove() as XmlNode
dim u as integer = list.ListCount-1
for i as integer = 0 to u
if List.CellCheck(i, 0) then
// include
else
// exclude
dim index as integer = List.RowTag(i)
NodesToRemove.Append bodyNode.Child(index)
end if
next
for each n as XmlNode in NodesToRemove
bodyNode.RemoveChild n
next
wordfile.XML = doc.ToString
if not wordfile.WriteFile(f) then
MsgBox "Failed to write file."
else
f.Launch
end if
End Sub
Property WordFile As WordFileMBS
Property myColumn As Integer
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
FileTypes1
Filetype application/vnd.openxmlformats-officedocument.wordprocessingml.document
End FileTypes1
End Project
See also:
The items on this page are in the following plugins: MBS Tools Plugin.