1.

Solve : Command Line command for manipulating word file?????

Answer»

Dear reader,
Is there any command line command to manipulate the word document.
For example: Modifying the Header content in the word document or adding some reference no on top of each and every page.

Thank you,
Ramesh Shrestha

P.S.:Forgive me if this is not right place for this question. Quote

Is there any command line command to manipulate the word document.

Batch code can open a document. End of STORY. Any of the scripting languages that are COM aware can manipulate the document.

Example to add header to first page of document:
Code: [Select]Const wdHeaderFirstPage = 2

Set objWord = CreateObject("Word.Application")
objWord.Visible = True

Set objDoc = objWord.Documents.Add()

With objDoc
    .PageSetup.DifferentFirstPageHeaderFooter = True
    .Sections(1).Headers(wdHeaderFirstPage) _
        .Range.InsertBefore _
        "Written By Sidewinder"
End With

objDoc.SaveAs "C:\Temp\Test.doc"
objWord.Quit

Save SCRIPT with a VBS extension and run from a command prompt as cscript scriptname.vbs

More information can be found here.

Good luck.  Thank you Sidewinder

It does what i want to do. But sometimes it replaces all header if there is the header in all three (left,center and right) alignment.

Is it also possible in case of footer having page number in it? Should maintain the alignment of the page number.

Thank you
Ramesh Shrestha
Quote
It does what i want to do. But sometimes it replaces all header if there is the header in all three (left,center and right) alignment.

If there is already text in the header, you'll need to capture it, align your new text with the existing text, then push everything back into the header.

I added to the existing snippet for a page number footer:

Code: [Select]Set objWord = CreateObject("Word.Application")
objWord.Visible = True

Set objDoc = objWord.Documents.Add()

With objDoc
  .PageSetup.DifferentFirstPageHeaderFooter = True
  .Sections(1).Headers(2).Range.InsertBefore "Written By Sidewinder"
End With

Set objRange = objDoc.Sections(1).Footers(2).Range
Set objTemplate = objDoc.AttachedTemplate
objTemplate.AutoTextEntries("Page X of Y").Insert objRange

objDoc.SaveAs "C:\Temp\Test.doc"
'objWord.Quit

Page number is formatted 1 of N.

Hint: Scripting Word can be difficult; try using the Word GUI and record a macro of what you're attempting to do. Viewing the resulting VBA can usually point you in the right direction.

Good luck 

thank you Sidewinder for the reply

It will be helpful if you provide me the vbscript for capturing the header and footer content.

Thank you
Ramesh Shrestha Quote
It will be helpful if you provide me the vbscript for capturing the header and footer content.

I'm sure it would be.

What have you DONE to capture the header and footer? Checking Google might have turned up this article.

Have you played with the posted code? Most of the logic was there, a tweak here, a tweak there, and you'd have found a solution:

Code: [Select]Set objWord = CreateObject("Word.Application")
objWord.Visible = True

Set objDoc = objWord.Documents.Open("c:\temp\test.doc")
sHeader = objDoc.Sections(1).Headers(2).Range.Text
sFooter = objDoc.Sections(1).Footers(2).Range.Text
WScript.Echo sheader, vbCrLf, sfooter

objWord.Quit

Did you even try using the Word GUI? Like I mentioned, recorded macros can be somewhat dense, but with a little effort they can be very educational.

  Quote from: Sidewinder on July 21, 2008, 03:54:09 AM
Quote
It will be helpful if you provide me the vbscript for capturing the header and footer content.

I'm sure it would be.

What have you done to capture the header and footer? Checking Google might have turned up this article.

Have you played with the posted code? Most of the logic was there, a tweak here, a tweak there, and you'd have found a solution:

Code: [Select]Set objWord = CreateObject("Word.Application")
objWord.Visible = True

Set objDoc = objWord.Documents.Open("c:\temp\test.doc")
sHeader = objDoc.Sections(1).Headers(2).Range.Text
sFooter = objDoc.Sections(1).Footers(2).Range.Text
WScript.Echo sheader, vbCrLf, sfooter

objWord.Quit

Did you even try using the Word GUI? Like I mentioned, recorded macros can be somewhat dense, but with a little effort they can be very educational.

 

Hi Sidewinder,
Is there a way to manipulate the Excel file through this just like word?
Yes. Any program product that comes with a COM server can be scripted with a COM aware script language (REXX, VBScript, Python, ETC). Just about any Microsoft application from Access to the Zune can be scripted.

Surprisingly (or maybe not), simple Microsoft APPS like notepad cannot be scripted other than sending keystrokes to the window.

There is even a ActiveX (COM) object for Mozilla

Much of the documentation can be found on MSDN and having a editor with intellisense can be very helpful.



Discussion

No Comment Found