Guest User

Untitled

a guest
Jun 25th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.66 KB | None | 0 0
  1. Application.OpenUndoTransaction "Create 6 tasks"
  2. Dim i As Integer
  3. For i = 1 To 6
  4. ActiveProject.Tasks.Add "UndoMe " & i
  5. Next
  6. Application.CloseUndoTransaction
  7.  
  8. Option Explicit
  9.  
  10. ' string constants for Undo mechanism
  11. Public Const BM_IN_MACRO As String = "_InMacro_"
  12.  
  13. Public Const BM_DOC_PROP_CHANGE As String = "_DocPropChange_"
  14. Public Const BM_DOC_PROP_NAME As String = "_DocPropName_"
  15. Public Const BM_DOC_PROP_OLD_VALUE As String = "_DocPropOldValue_"
  16. Public Const BM_DOC_PROP_NEW_VALUE As String = "_DocPropNewValue_"
  17.  
  18. '-----------------------------------------------------------------------------------
  19. ' Procedure : EditUndo
  20. ' Purpose : Atomic undo of macros
  21. ' Note: This macro only catches the menu command and the keyboard shortcut,
  22. ' not the toolbar command
  23. '-----------------------------------------------------------------------------------
  24. Public Sub EditUndo() ' Catches Ctrl-Z
  25.  
  26. 'On Error Resume Next
  27. Dim bRefresh As Boolean
  28. bRefresh = Application.ScreenUpdating
  29. Application.ScreenUpdating = False
  30.  
  31. Do
  32. If ActiveDocument.Bookmarks.Exists(BM_DOC_PROP_CHANGE) Then
  33. Dim strPropName As String
  34. Dim strOldValue As String
  35.  
  36. strPropName = ActiveDocument.Bookmarks(BM_DOC_PROP_NAME).Range.Text
  37. strOldValue = ActiveDocument.Bookmarks(BM_DOC_PROP_OLD_VALUE).Range.Text
  38. ActiveDocument.CustomDocumentProperties(strPropName).Value = strOldValue
  39. End If
  40.  
  41. Loop While (ActiveDocument.Undo = True) _
  42. And ActiveDocument.Bookmarks.Exists(BM_IN_MACRO)
  43.  
  44. Application.ScreenUpdating = bRefresh
  45. End Sub
  46.  
  47. '-----------------------------------------------------------------------------------
  48. ' Procedure : EditRedo
  49. ' Purpose : Atomic redo of macros
  50. ' Note: This macro only catches the menu command and the keyboard shortcut,
  51. ' not the toolbar command
  52. '-----------------------------------------------------------------------------------
  53. Public Sub EditRedo() ' Catches Ctrl-Y
  54.  
  55. Dim bRefresh As Boolean
  56. bRefresh = Application.ScreenUpdating
  57. Application.ScreenUpdating = False
  58.  
  59. Do
  60. If ActiveDocument.Bookmarks.Exists(BM_DOC_PROP_CHANGE) Then
  61. Dim strPropName As String
  62. Dim strNewValue As String
  63.  
  64. strPropName = ActiveDocument.Bookmarks(BM_DOC_PROP_NAME).Range.Text
  65. strNewValue = ActiveDocument.Bookmarks(BM_DOC_PROP_NEW_VALUE).Range.Text
  66. ActiveDocument.CustomDocumentProperties(strPropName).Value = strNewValue
  67. End If
  68.  
  69. Loop While (ActiveDocument.Redo = True) _
  70. And ActiveDocument.Bookmarks.Exists(BM_IN_MACRO)
  71.  
  72. Application.ScreenUpdating = bRefresh
  73.  
  74. End Sub
  75.  
  76. '-----------------------------------------------------------------------------------
  77. ' Procedure : SetCustomProp
  78. ' Purpose : Sets a custom document property
  79. '-----------------------------------------------------------------------------------
  80. Public Function SetCustomProp(oDoc As Document, strName As String, strValue As String)
  81.  
  82. Dim strOldValue As String
  83.  
  84. On Error GoTo existsAlready
  85. strOldValue = ""
  86. oDoc.CustomDocumentProperties.Add _
  87. Name:=strName, LinkToContent:=False, Value:=Trim(strValue), _
  88. Type:=msoPropertyTypeString
  89. GoTo exitHere
  90.  
  91. existsAlready:
  92. strOldValue = oDoc.CustomDocumentProperties(strName).Value
  93. oDoc.CustomDocumentProperties(strName).Value = strValue
  94.  
  95. exitHere:
  96. ' support undo / redo of changes to the document properties
  97. 'On Error Resume Next
  98. Dim bCalledWithoutUndoSupport As Boolean
  99.  
  100. If Not ActiveDocument.Bookmarks.Exists(BM_IN_MACRO) Then
  101. ActiveDocument.Range.Bookmarks.Add BM_IN_MACRO, ActiveDocument.Range
  102. bCalledWithoutUndoSupport = True
  103. End If
  104.  
  105. Dim oRange As Range
  106. Set oRange = ActiveDocument.Range
  107.  
  108. oRange.Collapse wdCollapseEnd
  109. oRange.Text = " "
  110. oRange.Bookmarks.Add "DocPropDummy_", oRange
  111.  
  112. oRange.Collapse wdCollapseEnd
  113. oRange.Text = strName
  114. oRange.Bookmarks.Add BM_DOC_PROP_NAME, oRange
  115.  
  116. oRange.Collapse wdCollapseEnd
  117. oRange.Text = strOldValue
  118. oRange.Bookmarks.Add BM_DOC_PROP_OLD_VALUE, oRange
  119.  
  120. oRange.Collapse wdCollapseEnd
  121. oRange.Text = strValue
  122. oRange.Bookmarks.Add BM_DOC_PROP_NEW_VALUE, oRange
  123.  
  124. oRange.Bookmarks.Add BM_DOC_PROP_CHANGE
  125. ActiveDocument.Bookmarks(BM_DOC_PROP_CHANGE).Delete
  126.  
  127. Set oRange = ActiveDocument.Bookmarks(BM_DOC_PROP_NEW_VALUE).Range
  128. ActiveDocument.Bookmarks(BM_DOC_PROP_NEW_VALUE).Delete
  129. If Len(oRange.Text) > 0 Then oRange.Delete
  130.  
  131. Set oRange = ActiveDocument.Bookmarks(BM_DOC_PROP_OLD_VALUE).Range
  132. ActiveDocument.Bookmarks(BM_DOC_PROP_OLD_VALUE).Delete
  133. If Len(oRange.Text) > 0 Then oRange.Delete
  134.  
  135. Set oRange = ActiveDocument.Bookmarks(BM_DOC_PROP_NAME).Range
  136. ActiveDocument.Bookmarks(BM_DOC_PROP_NAME).Delete
  137. If Len(oRange.Text) > 0 Then oRange.Delete
  138.  
  139. Set oRange = ActiveDocument.Bookmarks("DocPropDummy_").Range
  140. ActiveDocument.Bookmarks("DocPropDummy_").Delete
  141. If Len(oRange.Text) > 0 Then oRange.Delete
  142.  
  143. If bCalledWithoutUndoSupport And ActiveDocument.Bookmarks.Exists(BM_IN_MACRO) Then
  144. ActiveDocument.Bookmarks(BM_IN_MACRO).Delete
  145. End If
  146.  
  147. End Function
  148.  
  149. '-----------------------------------------------------------------------------------
  150. ' Procedure : SampleUsage
  151. ' Purpose : Demonstrates a transaction
  152. '-----------------------------------------------------------------------------------
  153. Private Sub SampleUsage()
  154.  
  155. On Error Resume Next
  156.  
  157. ' mark begin of transaction
  158. ActiveDocument.Range.Bookmarks.Add BM_IN_MACRO
  159.  
  160. Selection.Text = "Hello World"
  161. ' do other stuff
  162.  
  163. ' mark end of transaction
  164. ActiveDocument.Bookmarks(BM_IN_MACRO).Delete
  165.  
  166. End Sub
  167.  
  168. //Usage from ThisDocument VSTO Document level project
  169. public partial class ThisDocument
  170. {
  171. //Used to buffer writing text & formatting to document (to save undo stack)
  172. public static DocBuffer buffer;
  173.  
  174. //Attached Template
  175. public static Word.Template template;
  176.  
  177. private void ThisDocument_Startup(object sender, System.EventArgs e)
  178. {
  179. //Ignore changes to template (removes prompt to save changes to template)
  180. template = (Word.Template)this.Application.ActiveDocument.get_AttachedTemplate();
  181. template.Saved = true;
  182.  
  183. //Document buffer
  184. buffer = new DocBuffer();
  185.  
  186. //Start buffer
  187. ThisDocument.buffer.Start();
  188.  
  189. //This becomes one "undo"
  190. Word.Selection curSel = Globals.ThisDocument.Application.Selection;
  191. curSel.TypeText(" ");
  192. curSel.TypeBackspace();
  193. curSel.Font.Bold = 1;
  194. curSel.TypeText("Hello, world!");
  195. curSel.Font.Bold = 0;
  196. curSel.TypeText(" ");
  197.  
  198. //end buffer, print out text
  199. ThisDocument.buffer.End();
  200. }
  201.  
  202. void Application_DocumentBeforeClose(Microsoft.Office.Interop.Word.Document Doc, ref bool Cancel)
  203. {
  204. buffer.Close();
  205. }
  206.  
  207. private void ThisDocument_Shutdown(object sender, System.EventArgs e)
  208. {
  209. buffer.Close();
  210. }
  211. }
  212.  
  213. public class DocBuffer
  214. {
  215. //Word API Objects
  216. Word._Document HiddenDoc;
  217. Word.Selection curSel;
  218. Word.Template template;
  219.  
  220. //ref parameters
  221. object missing = System.Type.Missing;
  222. object FalseObj = false; //flip this for docbuffer troubleshooting
  223. object templateObj;
  224.  
  225. //Is docbuffer running?
  226. public Boolean started{ get; private set; }
  227.  
  228. //Open document on new object
  229. public DocBuffer()
  230. {
  231. //Clear out unused buffer bookmarks
  232. Word.Bookmarks bookmarks = Globals.ThisDocument.Application.ActiveDocument.Bookmarks;
  233. bookmarks.ShowHidden = true;
  234.  
  235. foreach (Word.Bookmark mark in bookmarks)
  236. {
  237. if (mark.Name.Contains("_buf"))
  238. {
  239. mark.Delete();
  240. }
  241. }
  242.  
  243. //Remove trail of undo's for clearing out the bookmarks
  244. Globals.ThisDocument.UndoClear();
  245.  
  246. //Set up template
  247. template = ThisDocument.template;
  248. templateObj = template;
  249.  
  250. //Open Blank document, then attach styles *and update
  251. HiddenDoc = Globals.ThisDocument.Application.Documents.Add(ref missing, ref missing, ref missing, ref FalseObj);
  252. HiddenDoc.set_AttachedTemplate(ref templateObj);
  253. HiddenDoc.UpdateStyles();
  254.  
  255. //Tell hidden document it has been saved to remove rare prompt to save document
  256. HiddenDoc.Saved = true;
  257.  
  258. //Make primary document active
  259. Globals.ThisDocument.Activate();
  260.  
  261. }
  262.  
  263. ~DocBuffer()
  264. {
  265. try
  266. {
  267. HiddenDoc.Close(ref FalseObj, ref missing, ref missing);
  268. }
  269. catch { }
  270. }
  271.  
  272. public void Close()
  273. {
  274. try
  275. {
  276. HiddenDoc.Close(ref FalseObj, ref missing, ref missing);
  277. }
  278. catch { }
  279. }
  280.  
  281. public void Start()
  282. {
  283. try
  284. {
  285. //Make hidden document active to receive selection
  286. HiddenDoc.Activate(); //results in a slight application focus loss
  287. }
  288. catch (System.Runtime.InteropServices.COMException ex)
  289. {
  290. if (ex.Message == "Object has been deleted.")
  291. {
  292. //Open Blank document, then attach styles *and update
  293. HiddenDoc = Globals.ThisDocument.Application.Documents.Add(ref missing, ref missing, ref missing, ref FalseObj);
  294. HiddenDoc.set_AttachedTemplate(ref templateObj);
  295. HiddenDoc.UpdateStyles();
  296. HiddenDoc.Activate();
  297. }
  298. else
  299. throw;
  300. }
  301.  
  302. //Remove Continue Bookmark, if exists
  303. Word.Bookmarks hiddenDocBookmarks = Globals.ThisDocument.Application.ActiveDocument.Bookmarks;
  304. if (hiddenDocBookmarks.Exists("Continue"))
  305. {
  306. object deleteMarkObj = "Continue";
  307. Word.Bookmark deleteMark = hiddenDocBookmarks.get_Item(ref deleteMarkObj);
  308. deleteMark.Select();
  309. deleteMark.Delete();
  310. }
  311.  
  312. //Tell hidden document it has been saved to remove rare prompt to save document
  313. HiddenDoc.Saved = true;
  314.  
  315. //Keep track when started
  316. started = true;
  317. }
  318.  
  319. //Used for non-modal dialogs to bring active document back up between text insertion
  320. public void Continue()
  321. {
  322. //Exit quietly if buffer hasn't started
  323. if (!started) return;
  324.  
  325. //Verify hidden document is active
  326. if ((HiddenDoc as Word.Document) != Globals.ThisDocument.Application.ActiveDocument)
  327. {
  328. HiddenDoc.Activate();
  329. }
  330.  
  331. //Hidden doc selection
  332. curSel = Globals.ThisDocument.Application.Selection;
  333.  
  334. //Hidden doc range
  335. Word.Range bufDocRange;
  336.  
  337. //Select entire doc, save range
  338. curSel.WholeStory();
  339. bufDocRange = curSel.Range;
  340.  
  341. //Find end, put a bookmark there
  342. bufDocRange.SetRange(curSel.End, curSel.End);
  343. object bookmarkObj = bufDocRange;
  344.  
  345. //Generate "Continue" hidden bookmark
  346. Word.Bookmark mark = Globals.ThisDocument.Application.ActiveDocument.Bookmarks.Add("Continue", ref bookmarkObj);
  347. mark.Select();
  348.  
  349. //Tell hidden document it has been saved to remove rare prompt to save document
  350. HiddenDoc.Saved = true;
  351.  
  352. //Make primary document active
  353. Globals.ThisDocument.Activate();
  354. }
  355.  
  356. public void End()
  357. {
  358. //Exit quietly if buffer hasn't started
  359. if (!started) return;
  360.  
  361. //Turn off buffer started flag
  362. started = false;
  363.  
  364. //Verify hidden document is active
  365. if ((HiddenDoc as Word.Document) != Globals.ThisDocument.Application.ActiveDocument)
  366. {
  367. HiddenDoc.Activate();
  368. }
  369.  
  370. //Remove Continue Bookmark, if exists
  371. Word.Bookmarks hiddenDocBookmarks = Globals.ThisDocument.Application.ActiveDocument.Bookmarks;
  372. hiddenDocBookmarks.ShowHidden = true;
  373. if (hiddenDocBookmarks.Exists("Continue"))
  374. {
  375. object deleteMarkObj = "Continue";
  376. Word.Bookmark deleteMark = hiddenDocBookmarks.get_Item(ref deleteMarkObj);
  377. deleteMark.Delete();
  378. }
  379.  
  380. //Hidden doc selection
  381. curSel = Globals.ThisDocument.Application.Selection;
  382.  
  383. //Hidden doc range
  384. Word.Range hiddenDocRange;
  385. Word.Range bufDocRange;
  386.  
  387. //Select entire doc, save range
  388. curSel.WholeStory();
  389. bufDocRange = curSel.Range;
  390.  
  391. //If cursor bookmark placed in, move there, else find end of text, put a bookmark there
  392. Boolean cursorFound = false;
  393. if (hiddenDocBookmarks.Exists("_cursor"))
  394. {
  395. object cursorBookmarkObj = "_cursor";
  396. Word.Bookmark cursorBookmark = hiddenDocBookmarks.get_Item(ref cursorBookmarkObj);
  397. bufDocRange.SetRange(cursorBookmark.Range.End, cursorBookmark.Range.End);
  398. cursorBookmark.Delete();
  399. cursorFound = true;
  400. }
  401. else
  402. {
  403. //The -2 is done because [range object].WordOpenXML likes to drop bookmarks at the end of the range
  404. bufDocRange.SetRange(curSel.End - 2, curSel.End - 2);
  405. }
  406.  
  407. object bookmarkObj = bufDocRange;
  408.  
  409. //Generate GUID for hidden bookmark
  410. System.Guid guid = System.Guid.NewGuid();
  411. String id = "_buf" + guid.ToString().Replace("-", string.Empty);
  412. Word.Bookmark mark = Globals.ThisDocument.Application.ActiveDocument.Bookmarks.Add(id, ref bookmarkObj);
  413.  
  414. //Get OpenXML Text (Text with formatting)
  415. curSel.WholeStory();
  416. hiddenDocRange = curSel.Range;
  417. string XMLText = hiddenDocRange.WordOpenXML;
  418.  
  419. //Clear out contents of buffer
  420. hiddenDocRange.Delete(ref missing, ref missing); //comment this for docbuffer troubleshooting
  421.  
  422. //Tell hidden document it has been saved to remove rare prompt to save document
  423. HiddenDoc.Saved = true;
  424.  
  425. //Make primary document active
  426. Globals.ThisDocument.Activate();
  427.  
  428. //Get selection from new active document
  429. curSel = Globals.ThisDocument.Application.Selection;
  430.  
  431. //insert buffered formatted text into main document
  432. curSel.InsertXML(XMLText, ref missing);
  433.  
  434. //Place cursor at bookmark+1 (this is done due to WordOpenXML ignoring bookmarks at the end of the selection)
  435. Word.Bookmarks bookmarks = Globals.ThisDocument.Application.ActiveDocument.Bookmarks;
  436. bookmarks.ShowHidden = true;
  437.  
  438. object stringObj = id;
  439. Word.Bookmark get_mark = bookmarks.get_Item(ref stringObj);
  440. bufDocRange = get_mark.Range;
  441.  
  442. if (cursorFound) //Canned language actively placed cursor
  443. bufDocRange.SetRange(get_mark.Range.End, get_mark.Range.End);
  444. else //default cursor at the end of text
  445. bufDocRange.SetRange(get_mark.Range.End + 1, get_mark.Range.End + 1);
  446. bufDocRange.Select();
  447. }
Add Comment
Please, Sign In to add comment