How to get "No Markup" text with Word COM (ComObj)

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
JoeWinograd
Posts: 2200
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

How to get "No Markup" text with Word COM (ComObj)

26 Sep 2016, 17:46

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:

Image

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
User avatar
jmeneses
Posts: 524
Joined: 28 Oct 2014, 11:09
Location: Catalan Republic

Re: How to get "No Markup" text with Word COM (ComObj)

27 Sep 2016, 05:08

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
User avatar
boiler
Posts: 16955
Joined: 21 Dec 2014, 02:44

Re: How to get "No Markup" text with Word COM (ComObj)

27 Sep 2016, 06:21

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.
User avatar
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)

27 Sep 2016, 08:54

change the Display for Review mode to "Final" instead of "Final: Show Markup", at least temporarily
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, Joe
User avatar
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)

27 Sep 2016, 09:44

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:

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()
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:

Code: Select all

oDoc.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
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
User avatar
jmeneses
Posts: 524
Joined: 28 Oct 2014, 11:09
Location: Catalan Republic

Re: How to get "No Markup" text with Word COM (ComObj)

27 Sep 2016, 10:33

Try this

Code: Select all

oDoc.Close(0)
Donec Perficiam
User avatar
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)

27 Sep 2016, 10:48

oDoc.Close(0) does it! Thanks to boiler and jmeneses, it now works! Much appreciated, Joe
User avatar
boiler
Posts: 16955
Joined: 21 Dec 2014, 02:44

Re: How to get "No Markup" text with Word COM (ComObj)

27 Sep 2016, 12:45

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.
User avatar
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)

27 Sep 2016, 15:08

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
JJohnston2
Posts: 204
Joined: 24 Jun 2015, 23:38

Re: How to get "No Markup" text with Word COM (ComObj)

28 Sep 2016, 01:18

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
User avatar
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)

28 Sep 2016, 08:11

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
JJohnston2
Posts: 204
Joined: 24 Jun 2015, 23:38

Re: How to get "No Markup" text with Word COM (ComObj)

29 Sep 2016, 23:54

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.
User avatar
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)

30 Sep 2016, 03:20

If you don't have an immediate window, go to Menu > View > Immediate Window.
That's the ticket!
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.
Perfect! Didn't have the immediate window. Thanks much, Joe

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 386 guests