Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. (* Read this into a new notebook using Get["X:\mathematica\WordInsert.m"] *)
  2. BeginWordInsert[path_] := Module[{},
  3. mmamacro = OpenWrite[path <> "\mma_insert.bas"];
  4. WriteString[mmamacro,
  5. "Attribute VB_Name = "NewMacros"nn", "Sub mma_insert()n",
  6. "Attribute Macro1.VB_ProcData.VB_Invoke_Func =
  7. "Normal.NewMacros.mmaInsert"n",
  8. "'n", "' ", "mmaInsert1 Macron", "'n", "'n"];
  9. mmaInsertN = 1;
  10. pathMI = path;];
  11. (* *)
  12. EndWordInsert[] :=
  13. Module[{}, WriteString[mmamacro,"End Sub"]; Close[mmamacro]];
  14. (* *)
  15. WordInsertP[graphic_, title_:" Caption", IMres_:300,
  16. fileType_: "png"] := Module[{},
  17. fileN =
  18. pathMI <> "\mmaEP" <> ToString[mmaInsertN] <> "." <> fileType;
  19. Export[fileN, graphic, ImageResolution -> IMres];
  20. WriteString[mmamacro, "Selection.TypeParagraphn",
  21. If[mmaInsertN > 1,
  22. "Selection.TypeParagraphnSelection.TypeParagraphn", ""],
  23. "Selection.InlineShapes.AddPicture FileName:= _n",
  24. """, fileN, "", ", "LinkToFile:=False, _n",
  25. "SaveWithDocument:=Truen",
  26. "Selection.ParagraphFormat.Alignment=wdAlignParagraphCentern",
  27. "Selection.Start = Selection.Start-1n",
  28. "Selection.InsertCaption Label:="Figure" , _n",
  29. "Title:=" ", title,
  30. "", Position:=wdCaptionPositionBelow,ExcludeLabel:=0n",
  31. "Selection.ParagraphFormat.Alignment=wdAlignParagraphCentern",
  32. "'n"];
  33. mmaInsertN = mmaInsertN + 1;
  34. ];
  35. (* *)
  36. WordInsertT[graphic_, title_: " Caption", IMres_: 300,
  37. fileType_: "png"] := Module[{},
  38. fileN =
  39. pathMI <> "\mmaEP" <> ToString[mmaInsertN] <> "." <> fileType;
  40. Export[fileN, graphic, ImageResolution -> IMres];
  41. WriteString[mmamacro, "Selection.TypeParagraphn",
  42. If[mmaInsertN > 1,
  43. "Selection.TypeParagraphnSelection.TypeParagraphn", ""],
  44. "Selection.InlineShapes.AddPicture FileName:= _n",
  45. """, fileN, "", ", "LinkToFile:=False, _n",
  46. "SaveWithDocument:=Truen",
  47. "Selection.ParagraphFormat.Alignment=wdAlignParagraphCentern",
  48. "Selection.Start = Selection.Start-1n",
  49. "Selection.InsertCaption Label:="Table" , _n",
  50. "Title:=" ", title,
  51. "", Position:=wdCaptionPositionAbove,ExcludeLabel:=0n",
  52. "Selection.ParagraphFormat.Alignment=wdAlignParagraphCentern",
  53. "'n"];
  54. mmaInsertN = mmaInsertN + 1;
  55. ];
  56. Print["n**You've loaded the WordInsert Package**nnTo start your
  57. session, run "BeginWordInsert[path]", where the path string points
  58. to your working directory.nnNote escape characters need to be used
  59. in the path definition n(IE. "X:\\mathematica").nnWrapers
  60. WordInsertP and WordInsertT are for inserting (P)lots and (T)ables
  61. respectively.n The insert functions are of the form
  62. WordInsert[graphic,title,ext], nwhere graphic is any Mathematica
  63. graphic object and the title is the string used in Word for the
  64. Figure or Table caption (needs to be in quotes).n ext is the file
  65. extension defining the type of graphic output to use (IE. "png").n
  66. nThis package writes a ".bas" file that can be inported into MS
  67. Word.nAll Plots and Tables will be inserted with captions when
  68. "mmainsert" macro is run in Word.nnTo end your WordInsert session and finalize the .bas file creation,n you
  69. must execute the "EndWordInsert[]" function.nn
  70. A typical run sequence:nnBeginWordInsert["X:\mathematica"]nWordInsertP[g1,title1]nWordInsertP[g2,title2]
  71. WordInsertT[g3,title3]nEndWordInsert[]nn"]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement