Results 1 to 12 of 12

Thread: Office Automation

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2003
    Location
    INDIA
    Posts
    22

    Office Automation

    Hi!
    I am writing an application that will identify Save and Close all editable applications running on the PC like Word, Excel, PowerPoint etc. on the click of a command button. followinf is the code for identifying and saving MS Word:-

    Do While (FindWindow("OpusApp", vbNullString)) <> 0
    WinWnd1 = FindWindow("OpusApp", vbNullString)
    ShowWindow WinWnd1, SW_SHOWNORMAL
    Dim x As Word.Application
    Set x = GetObject(, "Word.Application")
    AppActivate "Microsoft Word"
    x.ActiveDocument.SaveAs "C:\Word" & i & ""
    x.Quit
    Set x = Nothing
    i = i + 1 'i is declared before as a static variable
    Loop

    This codes works fine if one or any number of Word Application (SDI) is opened. However if only one Word is opened and more than one document (MDI) is opened on that Word application then one document is saved and than the dialog box asking for saving the application popup and asks for file name. I want that it the dialog box shouldn't appear and it should save those MDI documents with different file names.

    Anyone please help me .....
    raju

  2. #2
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Look up the "Documents" collection in the word vba help file:
    VB Code:
    1. For Each aDoc In Documents
    2.    If aDoc.Saved = False Then aDoc.Save
    3. Next aDoc

    For the second part, the following will supress any messageboxes from being shown to the user whilst the code excecutes, just remember to set this back to true at the end of the procedure!
    VB Code:
    1. word.Application.DisplayAlerts = false

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2003
    Location
    INDIA
    Posts
    22
    Hi!
    Thankyou for the help. Now I want to save PowerPoint, Excel etc. in the similar way. What "For.... Each.." should I use for Power Point and Excel? I have used the following:-

    For Excel I used : For Each Workbook In x.Workbooks , which is working fine but looses one excel document if I open more than one document.

    Power Point I used : For Each Presentation In x.Presentations
    This code is not working atall.

    Please suggest me how to proceed.

    Regds,

    Raju Deka
    raju

  4. #4
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    The workbooks item is a collection, this means you can take the count of the collection items and loop through them this way...
    VB Code:
    1. Dim intCounter As Integer
    2.  
    3. For intCounter = 1 To Workbooks.Count
    4.     Workbooks(intCounter).Save
    5.     Workbooks(intCounter).Close
    6. Next intCounter

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  5. #5
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    In powerpoint, it looks as though there's a presentations collection so this code should work also:
    VB Code:
    1. Dim intCounter As Integer
    2.  
    3. For intCounter = 1 To Presentations.Count
    4.     Presentations(intCounter).Save
    5.     Presentations(intCounter).Close
    6. Next intCounter

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jan 2003
    Location
    INDIA
    Posts
    22
    Hi! Thanks for the codes. It is working fine except if you open more documents(>3), it saves only the odd numbered documents and looses even numbered documents. So I removed the
    "Presentations(intCounter).Close" part and putting another while loop at the outside. Now it is working perfectly fine.

    Now I want to save "Outlook" , "Binder" etc. What should I use in "For.. Each".
    Waiting for your reply...
    raju

  7. #7
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    To be honest I have no idea!

    If you go into the code window/ide and type the following:

    Outlook.application.

    or Binder.application

    you'll have access to all the properties & methods the application object contains. When you type that last dot after the word application above, scroll down the list until you see something which looks like a collection (such as documents or worksheets etc.) & try this in a for....each loop.

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jan 2003
    Location
    INDIA
    Posts
    22
    Hi!
    First I included the reference and did it like that only, but couldn't find anything favourable so that I can activate and save a document like Word or excel. I don't have any books so that I can refer automation or MSDN also doesn't have the examples I am looking for. If you can please suggest me something....
    raju

  9. #9
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    For Outlook it looks as though the windows are termed "Explorers" & are held in an Explorers collection, try this one:
    VB Code:
    1. Dim intCounter As Integer
    2.  
    3. For intCounter = 1 To Explorers.Count
    4.     Explorers(intCounter).Save
    5.     Explorers(intCounter).Close
    6. Next intCounter

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  10. #10
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    For the Binder application, it looks as though the collection is termed "Section". I'm not sure how to code this one so I'd advise you to perform a search on the www.msdn.microsoft.com site for binder and section collection together...

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Jan 2003
    Location
    INDIA
    Posts
    22

    disablle keyboard

    Hi!
    Thanks for the codes. I have solved most of my office automation problems with your help. Now I want to save non office documents like Notepad, Wordpad, Paint etc. I have done all this through APIs and "Sendkey" function and are working fine. Now I want to disable the keyboard(Physical) so that when my software is saving documents with "Sendkeys" function user shouldnot be able to edit any document by keyboard. Otherwise my software fails to save some documents. Can you suggest me some API or function to disable the physical keyboard.
    raju

  12. #12
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Hiya raju,

    I'm not sure off the top of my head on this one, sorry.

    If you could you post this as a new question on the forums, someone else should be able to help you for this part ...

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width