| 1. |
Solve : batch file to open a word file and goto a special bookmark? |
|
Answer» How can I write a batch file to open a word file and goto a special bookmark? no you can't , not in batch. but, you could do the whole thing with a single VBS script, couldn't you, using Word.Application and directly accessing the bookmarks collection?Quote from: shelam on March 22, 2009, 10:17:01 PM How can I write a batch file to open a word file and goto a special bookmark? hmmm, because he is looking for a batch solution?? batch+macro <-- and this one has the batch part Quote from: Reno on March 23, 2009, 08:34:36 AM hmmm, because he is looking for a batch solution??depends write a vbscript, execute from the cmd.exe as Code: [Select]cscript <my_vbscript.vbs> and save it as a .bat file. would you call that batch? Quote from: ghostdog74 on March 23, 2009, 08:37:34 AM depends write a vbscript, execute from the cmd.exe as wsh+batch??I don't see any reason to even involve batch here except as a stub to execute the script. Now if I wrote the script in perl I could see it- since not EVERYBODY has a Perl interpreter; but VBScript is included in all versions of WINDOWS from W2k up (I believe it's in win98, but I'm not sure) we are exploring script because: Quote How can I write a batch file to open a word file and goto a special bookmark?is impossible without modifying the Document (which kind of limits the documents one can use it on to ones that contain this macro), thus my solution which works with almost any Doc file containing bookmarks. you took the "liberty" of using a macro within the document- I basically conglomerated all the logic from both the macro and the batch into a single abstract solution. Basically use the best tool for the job- it's kind of silly to go dancing around in a Word document to add macros for use with a batch when a script can perform the same thing in a FAR more abstracted manner to any word document that contains bookmarks. Anyway- does GetObject exist in VBScript? my original idea was to try to open the document specified in an existing word window- But GetObject doesn't Seem to work. I haven't used it in a while, and cannot remember if getobject err's out if the CLASSNAME isn't in the ROT or wether it will implicitly create the object and return it... Set wordapp = GetObject(,"Word.Application") adding getobject to BC_Programmer code: Code: [Select]if wsh.arguments.count < 2 then WScript.echo "Not enough arguments. Required syntax:" wscript.echo CHR(13) + CHR(10) wscript.echo "wordmark <filename> <bookmark>" Else on error resume next Set wordapp = GetObject(,"Word.Application") if err <> 0 then err.clear on error resume next set wordapp = createObject("Word.Application") end if if err <> 0 then wscript.echo "failed to acquire Word COM server object" Else on error resume next set newdoc = wordapp.Documents.Open(wsh.arguments(0)) if err <> 0 then wscript.echo "Failed to open word document, " + wsh.arguments(0) wordapp.quit else wordapp.Visible=true newdoc.Bookmarks.Item(wsh.arguments(1)).select end if end if 'wordapp.quit set wordapp=nothing end if with proper indention fix Quote from: Reno on March 23, 2009, 09:03:56 AM Set wordapp = GetObject(,"Word.Application") thanks reno, I suspected it was some convoluted parameter list. and with that the line: Code: [Select]set wordapp = createObject("Word.Application") can become Code: [Select]Set wordapp = GetObject(,"Word.Application") Quote from: BC_Programmer on March 23, 2009, 09:09:10 AM thanks reno, I suspected it was some convoluted parameter list. THANK you too for the vbs code. i dont think createobject line should be replaced by getobject.Quote from: Reno on March 23, 2009, 10:00:15 AM thank you too for the vbs code. well... not the way it is now. Code: [Select]GetObject("",classname) will do the implicit creation if the program isn't running, whereas not supplying the first argument will cause it to err if the program isn't running. Also if CreateObject succeeds but we fail to open the file, we should also call Quit(). But if Getobject succeeds and we can't open it we should leave it open since the user had word open. Right now I have it quit if it cannot open the file, regardless.Quote from: BC_Programmer on March 23, 2009, 10:05:43 AM well... not the way it is now.i don't see any difference between GetObject("",classname) and CreateObject(classname). i can't get a working code with just GetObject(), it still created 2 instance of WinWord.exe. i am curious if this can be done, whether getobject & createobject can be shortened to GetObject("",classname). if it works, this can save few lines of code. it might be specific to VB6, since that's how I tested it. Word wasn't running, and "GetObject(,"Word.Application") just errored with "ActiveX Component Cannot create Object" but using GetObject("","Word.Application").Visible=True started word- even though it wasn't running. Looked in the docs- this explains it: Quote If PathName is a zero-length string (""), GetObject returns a new object instance of the specified class type. If the PathName argument is omitted, GetObject returns a currently active object of the class type specified in Class. If no object of the specified type exists, an error occurs. I've always thought they had kind of silly semantics if you ask me. but it's better then trying to enumerate the ROT ourselves... |
|