Thanks to the experts in this group, I now have a script that loads the text from a Word file into a variable using COM calls. The code snippet that does it is this:
oDoc:=ComObjGet(InputFile)
InputDoc:=oDoc.Content.Text
oDoc.close()
It works fine, but there's one issue. If the Word doc has Track Changes on and there are changes that have not been accepted, the content retrieved includes text with strikethroughs, i.e., deleted text. For example, consider a doc with these Track Changes edits:
The code above places this in InputDoc:
Hello worldWorld. Hello hello again. And now colour.
What I'd like to get is this:
Hello World. hello again. And now color.
That is, I want to get the text as if "Accept All Changes" had been done, or as it would appear with "No Markup" selected, but I don't want to change the source file in any way or have to create a temp file. Is there a Word COM call that will return the "No Markup" text? Thanks, Joe
How to get "No Markup" text with Word COM (ComObj)
- JoeWinograd
- Posts: 2200
- Joined: 10 Feb 2014, 20:00
- Location: U.S. Central Time Zone
Re: How to get "No Markup" text with Word COM (ComObj)
Hi JoeWinograd try this
Code: Select all
oWord := ComObjActive("Word.Application").ActiveDocument
Loop % oWord.Characters.Count
If !oWord.Characters(A_index).Font.StrikeThrough
texto .= oWord.Characters(A_index).Text
Msgbox 0x40000,, % texto
ExitApp
Donec Perficiam
Re: How to get "No Markup" text with Word COM (ComObj)
That didn't work for me. The text isn't actually in a strike-through font. It's just displayed that way. Seems like the way to do it is to change the Display for Review mode to "Final" instead of "Final: Show Markup", at least temporarily, but I can't figure out how to do that yet. The Document.ShowRevisions property should do it, but I'm not able to get the syntax right to get or set it properly.
- JoeWinograd
- Posts: 2200
- Joined: 10 Feb 2014, 20:00
- Location: U.S. Central Time Zone
Re: How to get "No Markup" text with Word COM (ComObj)
Exactly! Word 2007 calls it "Final" (default is "Final Showing Markup"); Word 2010 calls it "Final" (default is "Final: Show Markup"); Word 2013 and 2016 call it "No Markup" (default is "All Markup"). Even though the menu choices for this feature are different in different releases of Word, I hope that the COM call for getting the final/no-markup text is the same — if, of course, it even exists. If it doesn't exist, is there a COM call to "Accept All Changes"? The script could do that, then select the text, and then close the doc without doing a save. Thanks to both of you for your efforts. Regards, Joechange the Display for Review mode to "Final" instead of "Final: Show Markup", at least temporarily
- JoeWinograd
- Posts: 2200
- Joined: 10 Feb 2014, 20:00
- Location: U.S. Central Time Zone
Re: How to get "No Markup" text with Word COM (ComObj)
Thanks to the link provided by boiler (and the VT_Bool trick from lexicos a while ago), I can now retrieve the "Final/No Markup" text:
Works great (and, for the record, using VT_Bool_True in the call correctly retrieves the marked up text). But there's one huge problem — it saves the file. I need to exit without changing/saving the source doc. I found the MSDN doc for it and attempted to translate it into AHK as follows:
But that fails with Error 0x800A107A. Can anyone help with this last piece of the puzzle, i.e., the AHK call to close the Word doc without saving changes? Thanks, Joe
Code: Select all
InputFile:=A_ScriptDir . "\test.docx"
VT_Bool_True:=ComObject(0xB,-1)
VT_Bool_False:=ComObject(0xB,0)
oDoc:=ComObjGet(InputFile)
oDoc.ShowRevisions:=VT_Bool_False
InputDoc:=oDoc.Content.Text
oDoc.Close()
Code: Select all
oDoc.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
Re: How to get "No Markup" text with Word COM (ComObj)
Try this
Code: Select all
oDoc.Close(0)
Donec Perficiam
- JoeWinograd
- Posts: 2200
- Joined: 10 Feb 2014, 20:00
- Location: U.S. Central Time Zone
Re: How to get "No Markup" text with Word COM (ComObj)
oDoc.Close(0) does it! Thanks to boiler and jmeneses, it now works! Much appreciated, Joe
Re: How to get "No Markup" text with Word COM (ComObj)
Thank you, JoeW, for pointing out the VT_Bool thing. I was trying with 1, 0, True, False, and couldn't get it to work. Nice to know.
Re: How to get "No Markup" text with Word COM (ComObj)
FYI, this question was also addressed here: https://autohotkey.com/boards/viewtopic ... 40#p111440
- JoeWinograd
- Posts: 2200
- Joined: 10 Feb 2014, 20:00
- Location: U.S. Central Time Zone
Re: How to get "No Markup" text with Word COM (ComObj)
boiler,
You're welcome. Drove me crazy until I picked that up from a lexicos post.
kon,
Thanks for the detailed explanation at your tutorial thread.
Regards, Joe
You're welcome. Drove me crazy until I picked that up from a lexicos post.
kon,
Thanks for the detailed explanation at your tutorial thread.
Regards, Joe
-
- Posts: 204
- Joined: 24 Jun 2015, 23:38
Re: How to get "No Markup" text with Word COM (ComObj)
Thanks for posting a copy of your routine... looks like that might come in handy.
For future ref, if you run into MSDN documentation for VBA with enum constansts such as Word.WdSaveOptions.wdDoNotSaveChanges, open an immediate window in VBA and print them using debug.print or "?" to get the const value,
i.e., ? Word.WdSaveOptions.wdDoNotSaveChanges
or ? WdSaveOptions.wdDoNotSaveChanges
For future ref, if you run into MSDN documentation for VBA with enum constansts such as Word.WdSaveOptions.wdDoNotSaveChanges, open an immediate window in VBA and print them using debug.print or "?" to get the const value,
i.e., ? Word.WdSaveOptions.wdDoNotSaveChanges
or ? WdSaveOptions.wdDoNotSaveChanges
- JoeWinograd
- Posts: 2200
- Joined: 10 Feb 2014, 20:00
- Location: U.S. Central Time Zone
Re: How to get "No Markup" text with Word COM (ComObj)
Hi JJ,
Thanks for the tip. Keep in mind that I know zero about VBA.
> open an immediate window in VBA
I'm guessing you mean a code window, as described by kon in his tutorial, i.e., Alt-F11 followed by F7. If you mean something else by "an immediate window", let me know what that is.
So I'm in the "Normal - NewMacros (Code)" window and there are a few other macros already in there. I created a new one called test (via Insert>Procedure) and entered this one line between the Public Sub test() and End Sub lines:
? Word.WdSaveOptions.wdDoNotSaveChanges
It auto-converted that to:
Print Word.WdSaveOptions.wdDoNotSaveChanges
I saved it and ran it (F5). It gave a "Compile error" that says "Method not valid without suitable object".
I then changed Print to Debug.Print, saved it and ran it. No error message this time, but also no output. What now? Thanks, Joe
Thanks for the tip. Keep in mind that I know zero about VBA.
> open an immediate window in VBA
I'm guessing you mean a code window, as described by kon in his tutorial, i.e., Alt-F11 followed by F7. If you mean something else by "an immediate window", let me know what that is.
So I'm in the "Normal - NewMacros (Code)" window and there are a few other macros already in there. I created a new one called test (via Insert>Procedure) and entered this one line between the Public Sub test() and End Sub lines:
? Word.WdSaveOptions.wdDoNotSaveChanges
It auto-converted that to:
Print Word.WdSaveOptions.wdDoNotSaveChanges
I saved it and ran it (F5). It gave a "Compile error" that says "Method not valid without suitable object".
I then changed Print to Debug.Print, saved it and ran it. No error message this time, but also no output. What now? Thanks, Joe
-
- Posts: 204
- Joined: 24 Jun 2015, 23:38
Re: How to get "No Markup" text with Word COM (ComObj)
In VBA you should have different window sections... usually project tree on the top left, properties on the bottom left, and then on the right, an Immediate window on the bottom, and code window on the top. If you don't have an immediate window, go to Menu > View > Immediate Window.
Immediate window is where debug.print messages will print. If you put the cursor in that window and do a debug.print or '?', you can print variables or enum constants without needing to make a routine just to print a value for debugging.
Do the debug.print in the immediate window instead of the code window and you'll get the enum value instead of whatever error you got.
Immediate window is where debug.print messages will print. If you put the cursor in that window and do a debug.print or '?', you can print variables or enum constants without needing to make a routine just to print a value for debugging.
Do the debug.print in the immediate window instead of the code window and you'll get the enum value instead of whatever error you got.
- JoeWinograd
- Posts: 2200
- Joined: 10 Feb 2014, 20:00
- Location: U.S. Central Time Zone
Re: How to get "No Markup" text with Word COM (ComObj)
That's the ticket!If you don't have an immediate window, go to Menu > View > Immediate Window.
Perfect! Didn't have the immediate window. Thanks much, JoeDo the debug.print in the immediate window instead of the code window and you'll get the enum value instead of whatever error you got.
Who is online
Users browsing this forum: No registered users and 386 guests