Results 1 to 3 of 3
  1. #1
    cfljanet is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2013
    Posts
    58

    Smile Opening Word with VBA

    I would very much appreciate if someone could help me.
    On my database I have placed a button on the Main Menu for users to access a manual on how to use the database. Essentially, I want to open a Word document and bring it into focus ON TOP of the database. In addition, as I regularly transfer (copy) the database onto the C drive of my laptop whenever I need to use it in the field, the code specifies that if there is an error in trying to open the Word document from one drive, it should open it from another.
    I know nothing about VBA but managed to put together something that mostly works really well (thank you Internet!!!!). On the laptop it works perfectly: it opens Word, transfers focus to it, and opens the document from the correct pathway. However, on my desktop, where the hyperlink is to a document on the office server, it works fine except that it does NOT bring Word to the fore, just flashes on the taskbar at the bottom.
    The odd thing is, that if I open the VBA window and close it again (without making any changes), it works just fine, activating the Word document in the foreground.
    Below is the code I am using. Is anyone able to spot why it does this?

    Private Sub DataManualcmd_Click()
    Dim LWordDoc As String
    Dim LTopWordDoc As String


    Dim oApp As Object

    'Path to the word document
    LWordDoc = "P:\Marketing\Sales & Stocks\Transhipment Templates\Manual - Transhipment and Data Analysis.docx"
    LTopWordDoc = "C:\Documents and Settings\CFL\My Documents\Sales programmes & manuals\Manual - Transhipment and Data Analysis.docx"

    'Create an instance of MS Word
    Set oApp = CreateObject(Class:="Word.Application")
    oApp.Visible = True
    oApp.Activate

    'Open the Document
    On Error Resume Next
    oApp.Documents.Open FileName:=LTopWordDoc
    If Error <> 0 Then
    oApp.Documents.Open FileName:=LWordDoc
    End If
    End Sub

    Just a note which I am sure you will be aware of: without the oApp.Activate, Word doesn't come to the fore at all
    Thank you for your time.

  2. #2
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Just to verify -
    1) does it fail on your desktop when the VBA window is NOT open, open, or both?
    2) when you already have Word open underneath the Access window, how does it behave?
    3) when you do not have Word open underneath the Access window, how does it behave?

    Try this on each machine, and before step 3, verify using the task manager that no Word processes are loaded.

  3. #3
    cfljanet is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2013
    Posts
    58
    Quote Originally Posted by Dal Jeanis View Post
    Just to verify -
    1) does it fail on your desktop when the VBA window is NOT open, open, or both?
    2) when you already have Word open underneath the Access window, how does it behave?
    3) when you do not have Word open underneath the Access window, how does it behave?

    Try this on each machine, and before step 3, verify using the task manager that no Word processes are loaded.
    1) On the desktop it fails until I have opened VBA. Once I have opened the VBA window, even if I close it straight away or even if I leave it open, it works perfectly. Word opens, the focus changes from Access to Word, and the documents loads. Even if I then close Word, it carries on working perfectly if I try it again, it just seems to fail if I have exited Access and then load the database again.
    2) If Word is already open, it ignores that and opens Word again. As you've no doubt realized I haven't specified in the code to check if Word is open first, before opening it which I guess I should do.
    3) As for 2). It is irrelevant if Word is already open. Of course, if the document in question is already loaded I get a "locked for editing" pop-up which you would expect, but otherwise it behaves exactly the same whether Word was already loaded or not.

    On the laptop it works exactly the same way, the only difference being that the focus switches to Word first, always, and therefore is working exactly how I want it to

    On both I checked that Word had no active processes loaded before trying step 3.

    I realize this is a small annoyance in that it is probably not a big deal if focus doesn't switch to Word, it's just that I am conscious that people may be using the database who don't know what to expect when loading the manual, may be very tired and working in poor light (although that would apply to the laptop rather than the desktop) and I just want to make it all as obvious as possible. Also, I just want to KNOW why it behaves like that!

    Thanks a lot for your time DalJeanis, maybe you can spot something?

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 6
    Last Post: 08-13-2014, 10:32 PM
  2. Replies: 15
    Last Post: 04-17-2014, 02:21 PM
  3. Replies: 8
    Last Post: 04-14-2013, 01:33 PM
  4. Call word object and import word fields
    By silverspr in forum Programming
    Replies: 3
    Last Post: 12-10-2012, 11:32 PM
  5. Access to Word - Multiple Word Templates?
    By alpinegroove in forum Programming
    Replies: 11
    Last Post: 06-12-2012, 04:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums