File size: 21,269 Bytes
10b2298 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 |
' ========================================================================
' VBA code for Plantuml
' ========================================================================
'
' This file is distributed under the GNU All-Permissive License
' http://www.gnu.org/prep/maintain/html_node/License-Notices-for-Other-Files.html
'
' Copying and distribution of this file, with or without modification,
' are permitted in any medium without royalty provided the copyright
' notice and this notice are preserved. This file is offered as-is,
' without any warranty.
'
' Original Author: Arnaud Roques
' Word Macro: Alain Bertucat / Matthieu Sabatier
' Improved error management : Christopher Fuhrman
' http://vbadud.blogspot.fr/2008/12/how-to-set-and-reset-track-changes.html
' Version 007
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Const startuml = "@start"
Const enduml = "@end"
' =========================================================
' This function returns the path for plantuml.jar
Function getJarPath() As String
Set fs = CreateObject("Scripting.FileSystemObject")
nbTemplates = ActiveDocument.Parent.Templates.Count
mainPath = ActiveDocument.Path
try = ActiveDocument.Path & "\"
nb = InStrRev(mainPath, "\")
Do While nb > 1 And fs.FileExists(mainPath + "\plantuml.jar") = False
mainPath = Left(mainPath, nb - 1)
try = try & vbCrLf & mainPath & "\"
nb = InStrRev(mainPath, "\")
Loop
For i = 1 To nbTemplates
If fs.FileExists(mainPath + "\plantuml.jar") = False Then
mainPath = ActiveDocument.Parent.Templates.Item(i).Path
try = try & vbCrLf & ActiveDocument.Parent.Templates.Item(i).Path & "\"
nb = InStrRev(mainPath, "\")
Do While nb > 1 And fs.FileExists(mainPath + "\plantuml.jar") = False
mainPath = Left(mainPath, nb - 1)
try = try & vbCrLf & mainPath & "\"
nb = InStrRev(mainPath, "\")
Loop
End If
Next i
If fs.FileExists(mainPath + "\plantuml.jar") Then
getJarPath = mainPath
Else
getJarPath = "Error : Cannot find plantuml.jar in :" & vbCrLf & try
End If
End Function
' =========================================================
' Print out the used plantuml.jar
Sub ShowPlantumlJarPath()
Set fs = CreateObject("Scripting.FileSystemObject")
jarPath = getJarPath()
If fs.FileExists(jarPath & "\plantuml.jar") Then
MsgBox "OK : " & jarPath & "\plantuml.jar"
Else
MsgBox jarPath
End If
End Sub
' =========================================================
' Used to migrate from previous PlantUML macro version
Sub RemoveOldVersionPlantUMLSyles()
On Error GoTo DeleteEnd
ActiveDocument.Styles("PlantUML").Delete
On Error GoTo 0
DeleteEnd:
On Error GoTo 0
Call Macro_UML_all
End Sub
' =========================================================
' Called when the user click on "UML.*"
Sub Macro_UML_all()
Macro_UML ("all")
End Sub
' =========================================================
' Called when the user click on "UML.1"
Function Macro_UML_parg()
Macro_UML ("parg")
End Function
' =========================================================
Function Macro_UML(scope)
' Generate diagrams image from a PlantUML source textual description in the Word Document
' Scope can be "parg" or "all"
'
' - Initialisations
'
Call ToolbarInit
Set statusButton = CommandBars("UML").Controls(6)
Call CreateStyle
Call CreateStyleImg
Call ShowPlantuml
Call ShowHiddenText
Selection.Range.Select
'
' documentId is the filename with its path, without extension
'
documentId = ActiveDocument.Name
documentId = Left(documentId, Len(documentId) - 4)
' Check for the presente of plantuml.jar
Set fs = CreateObject("Scripting.FileSystemObject")
jarPath = getJarPath()
If fs.FileExists(jarPath & "\plantuml.jar") = False Then
MsgBox jarPath
GoTo Macro_UML_exit
End If
' - Phase 1
' We create a file text per bloc of diagrams
' We look for @startuml
' We open the textfile in background (visible:=false)
' We add to the name a number on 4 digit
' The text bloc is put on "PlantUML" style
' Then the bloc is copied into the text file
statusButton.Caption = "Extract"
statusButton.Visible = False
statusButton.Visible = True
If scope = "all" Then
Set parsedText = ActiveDocument.Content
isForward = True
Else
Set parsedText = Selection.Range
parsedText.Collapse
isForward = False
End If
parsedText.Find.Execute FindText:=startuml, Forward:=isForward
If parsedText.Find.Found = True Then
'We keep the the first line only "@startuml" with the carriage return
Set singleParagraph = parsedText.Paragraphs(1).Range
singleParagraph.Collapse
Else
GoTo Macro_UML_exit
End If
Do While parsedText.Find.Found = True And _
(scope = "all" Or currentIndex < 1)
statusButton.Caption = "Extract." & currentIndex + 1
statusButton.Visible = False
statusButton.Visible = True
Set currentParagraph = parsedText.Paragraphs(1)
Set paragraphRange = currentParagraph.Range
paragraphRange.Collapse
jobDone = False
Do Until jobDone
If Left(currentParagraph.Range.Text, Len(startuml)) = startuml Then
Set paragraphRange = currentParagraph.Range
paragraphRange.Collapse
End If
paragraphRange.MoveEnd Unit:=wdParagraph
If Left(currentParagraph.Range.Text, Len(enduml)) = enduml Then
paragraphRange.Style = "PlantUML"
paragraphRange.Copy
Set textFile = Documents.Add(Visible:=False)
textFile.Content.Paste
currentIndex = currentIndex + 1
textFileId = documentId & "_extr" & Right("000" & currentIndex, 4) & ".txt"
textFile.SaveAs FileName:=jarPath & "\" & textFileId, FileFormat:=wdFormatText, Encoding:=65001
textFile.Close
jobDone = True
End If
Set currentParagraph = currentParagraph.Next
If currentParagraph Is Nothing Then
jobDone = True
End If
Loop
parsedText.Collapse Direction:=wdCollapseEnd
If scope = "all" Then
parsedText.Find.Execute FindText:=startuml, Forward:=True
End If
Loop
'
' We create a lock file that will be deleted by the Java program to indicate the end of Java process
'
statusButton.Caption = "Gener"
statusButton.Visible = False
statusButton.Visible = True
Set lockFile = Documents.Add(Visible:=False)
lockFile.SaveAs FileName:=jarPath & "\javaumllock.tmp", FileFormat:=wdFormatText
lockFile.Close
'
' Call to PlantUML to generate images from text descriptions
'
javaCommand = "java -classpath """ & jarPath & "\plantuml.jar;" & _
jarPath & "\plantumlskins.jar"" net.sourceforge.plantuml.Run -charset UTF8 -word """ & jarPath & "/"""
Shell (javaCommand)
' This sleep is needed, but we don't know why...
Sleep 500
'
' Phase 2 :
' Insertion of images into the word document
' We insert the image after the textual block that describe the diagram
'
jobDone = False
currentIndex = 0
' We wait for the file javaumllock.tmp to be deleted by Java
' which means that the process is ended
'
Do
currentIndex = currentIndex + 1
statusButton.Caption = "Gener." & currentIndex
statusButton.Visible = False
statusButton.Visible = True
DoEvents
Sleep 1000
If fs.FileExists(jarPath & "\javaumllock.tmp") = False Then
jobDone = True
Exit Do
End If
If currentIndex > 30 Then
statusButton.Visible = False
MsgBox ("Java Timeout. Aborted.")
Exit Do
End If
Loop
If jobDone = False Then
End
End If
statusButton.Caption = "Inser"
statusButton.Visible = False
statusButton.Visible = True
If scope = "all" Then
Set parsedText = ActiveDocument.Content
isForward = True
Else
Set parsedText = singleParagraph
isForward = True
End If
parsedText.Find.Execute FindText:=enduml, Forward:=isForward
currentIndex = 0
bTrackRevFlag = ActiveDocument.TrackRevisions
ActiveDocument.TrackRevisions = False
Do While parsedText.Find.Found = True And (scope = "all" Or currentIndex < 1)
currentIndex = currentIndex + 1
statusButton.Caption = "Inser." & currentIndex
statusButton.Visible = False
statusButton.Visible = True
On Error GoTo LastParagraph
Set currentParagraph = parsedText.Paragraphs(1).Next.Range
Do While currentParagraph.InlineShapes.Count > 0 And currentParagraph.Style = "PlantUMLImg"
currentParagraph.Delete
Set currentParagraph = parsedText.Paragraphs(1).Next.Range
Loop
On Error GoTo 0
Set currentRange = currentParagraph
imagesDirectory = jarPath & "\" & documentId & "_extr" & Right("000" & currentIndex, 4) & "*.png"
image = Dir(imagesDirectory)
While image <> ""
' Contain the text of the error
errorTextFile = jarPath & "\" & Left(image, Len(image) - 4) & ".err"
Set currentParagraph = ActiveDocument.Paragraphs.Add(Range:=currentRange).Range
Set currentRange = currentParagraph.Paragraphs(1).Next.Range
currentParagraph.Style = "PlantUMLImg"
currentParagraph.Collapse
Set image = currentParagraph.InlineShapes.AddPicture _
(FileName:=jarPath & "\" & image _
, LinkToFile:=False, SaveWithDocument:=True)
If fs.FileExists(errorTextFile) Then
image.AlternativeText = LoadTextFile(errorTextFile)
Beep
Else
image.AlternativeText = "Generated by PlantUML"
End If
If image.ScaleHeight > 100 Or image.ScaleWidth > 100 Then
image.Reset
End If
image = Dir()
Wend
parsedText.Collapse Direction:=wdCollapseEnd
parsedText.Find.Execute FindText:=enduml, Forward:=True
Loop
ActiveDocument.TrackRevisions = bTrackRevFlag
'
' Phase 3 : suppression of temporary files (text and PNG)
'
Phase3:
statusButton.Caption = "Delete"
statusButton.Visible = False
statusButton.Visible = True
On Error Resume Next
Kill (jarPath & "\" & documentId & "_extr*.*")
On Error GoTo 0
Macro_UML_exit:
statusButton.Visible = False
'We show the hidden description text
Call ShowHiddenText
DoubleCheckStyle
Exit Function
' This is need when the very last line of the Word document is @enduml
LastParagraph:
Selection.EndKey Unit:=wdStory
Selection.TypeParagraph
Selection.ClearFormatting
imagesDirectory = jarPath & "\" & documentId & "_extr" & Right("000" & currentIndex, 4) & "*.png"
image = Dir(imagesDirectory)
While image <> ""
' Contain the text of the error
errorTextFile = jarPath & "\" & Left(image, Len(image) - 4) & ".err"
Set currentParagraph = ActiveDocument.Paragraphs.Add.Range
Set currentRange = currentParagraph.Paragraphs(1).Next.Range
currentParagraph.Style = "PlantUMLImg"
currentParagraph.Collapse
Set image = currentParagraph.InlineShapes.AddPicture _
(FileName:=jarPath & "\" & image _
, LinkToFile:=False, SaveWithDocument:=True)
If fs.FileExists(errorTextFile) Then
image.AlternativeText = LoadTextFile(errorTextFile)
Beep
Else
image.AlternativeText = "Generated by PlantUML"
End If
If image.ScaleHeight > 100 Or image.ScaleWidth > 100 Then
image.Reset
End If
image = Dir()
Wend
'Resume Next
GoTo Phase3
End Function
' =========================================================
' Initialize the plantuml ToolBar
Function ToolbarInit()
On Error GoTo ToolbarCreation
Set toolBar = ActiveDocument.CommandBars("UML")
On Error GoTo 0
toolBar.Visible = True
On Error GoTo ButtonAdd
Set currentButton = toolBar.Controls(1)
On Error GoTo 0
currentButton.OnAction = "Module1.SwitchP"
currentButton.Style = msoButtonCaption
currentButton.Caption = Chr(182)
currentButton.Visible = True
On Error GoTo ButtonAdd
Set currentButton = toolBar.Controls(2)
On Error GoTo 0
currentButton.OnAction = "Module1.ShowPlantuml"
currentButton.Style = msoButtonCaption
currentButton.Caption = "Show PlantUML"
currentButton.Visible = True
On Error GoTo ButtonAdd
Set currentButton = toolBar.Controls(3)
On Error GoTo 0
currentButton.OnAction = "Module1.HidePlantuml"
currentButton.Style = msoButtonCaption
currentButton.Caption = "Hide PlantUML"
currentButton.Visible = True
On Error GoTo ButtonAdd
Set currentButton = toolBar.Controls(4)
On Error GoTo 0
currentButton.OnAction = "Module1.Macro_UML_all"
currentButton.Style = msoButtonCaption
currentButton.Caption = "UML.*"
currentButton.Visible = True
On Error GoTo ButtonAdd
Set currentButton = toolBar.Controls(5)
On Error GoTo 0
currentButton.OnAction = "Module1.Macro_UML_parg"
currentButton.Style = msoButtonCaption
currentButton.Caption = "UML.1"
currentButton.Visible = True
On Error GoTo ButtonAdd
Set currentButton = toolBar.Controls(6)
On Error GoTo 0
currentButton.OnAction = ""
currentButton.Style = msoButtonCaption
currentButton.Caption = "Trace"
currentButton.Visible = True
Exit Function
ToolbarCreation:
Set toolBar = ActiveDocument.CommandBars.Add(Name:="UML")
Resume Next
ButtonAdd:
Set currentButton = toolBar.Controls.Add(Type:=msoControlButton, Before:=toolBar.Controls.Count + 1)
Resume Next
End Function
' =========================================================
' We need to double check that the style is present in the document
Function DoubleCheckStyle()
CreateStyle
CreateStyleImg
Set myStyle = ActiveDocument.Styles("PlantUML")
myStyle.BaseStyle = ActiveDocument.Styles.Item(1).BaseStyle
myStyle.AutomaticallyUpdate = True
With myStyle.Font
.Name = "Courier New"
.Size = 9
.Hidden = False
.Hidden = True
.Color = wdColorGreen
End With
End Function
' =========================================================
Function CreateStyle()
On Error GoTo CreateStyleAdding
Set myStyle = ActiveDocument.Styles("PlantUML")
Exit Function
CreateStyleAdding:
Set myStyle = ActiveDocument.Styles.Add(Name:="PlantUML", Type:=wdStyleTypeParagraph)
myStyle.BaseStyle = ActiveDocument.Styles.Item(1).BaseStyle
myStyle.AutomaticallyUpdate = True
With myStyle.Font
.Name = "Courier New"
.Size = 9
.Hidden = False
.Hidden = True
.Color = wdColorGreen
End With
With myStyle.ParagraphFormat
With .Shading
.Texture = wdTextureNone
.ForegroundPatternColor = wdColorAutomatic
.BackgroundPatternColor = wdColorLightGreen
End With
.LeftIndent = CentimetersToPoints(0)
With .Shading
.Texture = wdTextureNone
.ForegroundPatternColor = wdColorAutomatic
.BackgroundPatternColor = 12254650
End With
With .Borders(wdBorderLeft)
.LineStyle = wdLineStyleDashLargeGap
.LineWidth = wdLineWidth050pt
.Color = 3910491
End With
With .Borders(wdBorderRight)
.LineStyle = wdLineStyleDashLargeGap
.LineWidth = wdLineWidth050pt
.Color = 3910491
End With
With .Borders(wdBorderTop)
.LineStyle = wdLineStyleDashLargeGap
.LineWidth = wdLineWidth050pt
.Color = 3910491
End With
With .Borders(wdBorderBottom)
.LineStyle = wdLineStyleDashLargeGap
.LineWidth = wdLineWidth050pt
.Color = 3910491
End With
With .Borders
.DistanceFromTop = 1
.DistanceFromLeft = 4
.DistanceFromBottom = 1
.DistanceFromRight = 4
.Shadow = False
End With
End With
' ajout des tabulations
myStyle.NoSpaceBetweenParagraphsOfSameStyle = False
myStyle.ParagraphFormat.TabStops.ClearAll
myStyle.ParagraphFormat.TabStops.Add Position:= _
CentimetersToPoints(1), Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces
myStyle.ParagraphFormat.TabStops.Add Position:= _
CentimetersToPoints(2), Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces
myStyle.ParagraphFormat.TabStops.Add Position:= _
CentimetersToPoints(3), Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces
myStyle.ParagraphFormat.TabStops.Add Position:= _
CentimetersToPoints(4), Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces
End Function
' =========================================================
Function CreateStyleImg()
On Error GoTo CreateStyleImgAdding
Set myStyle = ActiveDocument.Styles("PlantUMLImg")
myStyle.BaseStyle = ActiveDocument.Styles.Item(1).BaseStyle
On Error GoTo 0
Exit Function
CreateStyleImgAdding:
Set myStyle = ActiveDocument.Styles.Add(Name:="PlantUMLImg", Type:=wdStyleTypeParagraph)
myStyle.AutomaticallyUpdate = True
End Function
' =========================================================
' We show the hidden text
Function ShowPlantuml()
DoubleCheckStyle
'WordBasic.ShowComments
' We put a bookmark to retrieve position after showing the text
ActiveDocument.Bookmarks.Add Name:="Position", Range:=Selection.Range
Set myStyle = ActiveDocument.Styles("PlantUML")
Set toolBar = ActiveDocument.CommandBars("UML")
toolBar.Controls(2).Visible = False
toolBar.Controls(3).Visible = True
toolBar.Controls(4).Visible = True
toolBar.Controls(5).Visible = True
Call ShowHiddenText
'We go back to the bookmark and we delete it
Selection.GoTo What:=wdGoToBookmark, Name:="Position"
ActiveDocument.Bookmarks(Index:="Position").Delete
End Function
' =========================================================
' MSR - gestion de l'option d'affichage des textes masques du style : "PlantUML"
Function HidePlantuml()
DoubleCheckStyle
'WordBasic.ShowComments
' We put a bookmark to retrieve position after showing the text
ActiveDocument.Bookmarks.Add Name:="Position", Range:=Selection.Range
Set myStyle = ActiveDocument.Styles("PlantUML")
Set toolBar = ActiveDocument.CommandBars("UML")
toolBar.Controls(2).Visible = True
toolBar.Controls(3).Visible = False
toolBar.Controls(4).Visible = False
toolBar.Controls(5).Visible = False
Call HideHiddenText
'We go back to the bookmark and we delete it
Selection.GoTo What:=wdGoToBookmark, Name:="Position"
ActiveDocument.Bookmarks(Index:="Position").Delete
End Function
' =========================================================
Function HideHiddenText()
ActiveDocument.ActiveWindow.View.ShowAll = False
ActiveDocument.ActiveWindow.View.ShowHiddenText = False
End Function
' =========================================================
Function ShowHiddenText()
ActiveDocument.ActiveWindow.View.ShowAll = False
ActiveDocument.ActiveWindow.View.ShowHiddenText = True
End Function
' =========================================================
Function SwitchP()
flag = Not (ActiveDocument.ActiveWindow.View.ShowTabs)
ActiveDocument.ActiveWindow.View.ShowParagraphs = flag
ActiveDocument.ActiveWindow.View.ShowTabs = flag
ActiveDocument.ActiveWindow.View.ShowSpaces = flag
ActiveDocument.ActiveWindow.View.ShowHyphens = flag
ActiveDocument.ActiveWindow.View.ShowAll = False
End Function
' =========================================================
' \\ Function to return the full content of a text file as a string
'from http://www.vbaexpress.com/kb/getarticle.php?kb_id=699
Function LoadTextFile(sFile) As String
Dim iFile As Integer
On Local Error Resume Next
' \\ Use FreeFile to supply a file number that is not already in use
iFile = FreeFile
' \\ ' Open file for input.
Open sFile For Input As #iFile
' \\ Return (Read) the whole content of the file to the function
LoadTextFile = Input$(LOF(iFile), iFile)
Close #iFile
End Function
|