Page 1 of 3 123 LastLast
Results 1 to 15 of 35
  1. #1
    Join Date
    Jan 2008
    Posts
    4

    Unanswered: Automating Word & Locating Bookmarks (Was "Please Help!")

    Hello,
    I was wondering if someone could please help me, this has been driving me mad for days! I am trying to automate word using access. Basically when a form is opened in add mode the user can enter in details, click a button and the data is sent to the word document with bookmarks indicating where certain data should go. My problem is that if the user then goes and adds another persons details and clicks the button, it adds them in to the same bookmark as before. I tried a few different things to no avail. So im wondering if anyone knows how to check if the document is open and if so go to the next bookmark? Here is my code if this helps:

    Private Sub Command52_Click()

    Dim No As Integer
    No = 1
    Dim WordObj As Word.Application
    On Error Resume Next
    Set WordObj = GetObject(, "Word.Application")


    If WordObj Is Nothing Then
    Set WordObj = CreateObject("Word.Application")
    End If

    Dim WordDoc As Word.Document
    Set WordDoc = WordObj.Documents.Open _
    ("C:\EnquiriesNew.doc")

    With WordObj.ActiveDocument.Bookmarks
    ActiveDocument.Bookmarks("bmk" & No).Range.Text = Date
    No = No + 1
    ActiveDocument.Bookmarks("bmk" & No).Range.Text = (CStr(Forms!frmTutor!TU_DATE_APPOINTED))
    No = No + 1
    ActiveDocument.Bookmarks("bmk" & No).Range.Text = (CStr(Forms!frmTutor!TU_TITLE)) & " " & (CStr(Forms!frmTutor!TU_FORENAME)) & " " & (CStr(Forms!frmTutor!TU_SURNAME))
    No = No + 1
    ActiveDocument.Bookmarks("bmk" & No).Range.Text = (CStr(Forms!frmTutor!TU_CODE))
    No = No + 1
    End With

    WordObj.Visible = True
    Set WordObj = Nothing

    End Sub

    Thanks!

  2. #2
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Hi

    A while since I automated word so you will either have to be patient with me or someone a bit more clued up will jump in and help.

    First things first - you want to know if the word document is open already - if it is open, would it have been opened (automated...)by your application?

  3. #3
    Join Date
    Jan 2008
    Posts
    4
    Yes when the person clicks the button on the first new person form it would have been opened, not before

  4. #4
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Ok - in that case this bit is easy. Your current code has the word object declared locally and destroyed at the end of the procedure. Instead have the object declared globally (if you want the scope to be your entire application) and simply reference that object. If the object is nothing then you need to do the create object stuff. This saves you having to do the tricky "is word opened already and, if it is, is it the right document and, if it isn't, then I need to start looping through some collections using APIs and check each open document" stuff.

    Make sense?

  5. #5
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Actually the solution to the bookmark problem is similar. Change the scope of the No variable to global. Set it to 1 when the application starts. Remove the " Dim No As Integer
    No = 1" code from above. Now, each time your procedure is called the value of No will be retained between each call, incrementing each time.

  6. #6
    Join Date
    Jan 2008
    Posts
    4
    I know this is a stupid question but how do i set the No variable to 1 when the application starts, do you mean when the database is opened, if so where do i put that code (sorry)

  7. #7
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    No problem

    Yes - when the database opens. This is how I do this sort of thing:
    I have a form that opens when the database opens (you have one of these right?).
    My form calls a procedure in a module called something like "StartUpCode" via the OnOpen event (i.e. the database opens ->My form opens -> The form OnOpen event is triggered -> The StartUpCode procedure is called).
    StartUpCode has all the stuff I want to happen as soon as the application starts. For you, this will include (and perhaps, for now, only be) setting No = 1.

    How's that?

  8. #8
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    BTW - when I talk about scope do you know what I am talking about?

  9. #9
    Join Date
    Jan 2008
    Posts
    4
    Yeah im pretty sure i know what you mean by scope (maybe i should have listened more in lectures)

    Im trying to call the module i created from the OnOpen event but its not working??

  10. #10
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Ok - so wotya got (module code and OnOpen event code - all in full please!)

  11. #11
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,442
    Provided Answers: 12
    Correct me I'm wrong here; but instead of assigning a value on startup; why not just test for a NULL value?
    George
    Home | Blog

  12. #12
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Coz it will be 0 - no nulls for VBA integer variables

    You can do that - you are missing nothing - and it was something I considered suggesting but thought easiest to stick with one idea. To be fair your way would require less lines of code.

  13. #13
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,442
    Provided Answers: 12
    Quote Originally Posted by pootle flump
    Coz it will be 0 - no nulls for VBA integer variables
    Access is stupid
    George
    Home | Blog

  14. #14
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Quote Originally Posted by georgev
    Access is stupid
    That's one way of looking at it George

  15. #15
    Join Date
    Jan 2008
    Posts
    37
    I'm a disaster, had to sign up again! Anyway whats happening now is that it the thing thats supposed to be in the first bookmark isnt there and the thing thats supposed to be in the second bookmark is in the first bookmark. Then when a second person is entered its still writing in info to the same bookmarks, like its not retaining the value of No. Here's my code:

    Private Sub Form_Open(Cancel As Integer)
    ' Minimize the database window and initialize the form.

    ' Move to the switchboard page that is marked as the default.
    Me.Filter = "[ItemNumber] = 0 AND [Argument] = 'Default' "
    Me.FilterOn = True

    startup 'Calling the function in module 1 called startup

    End Sub

    This is in module 1:
    Public Function startup()
    No = 1
    End Function

    The initial declaration of No is in the general declarations part above the switchboard form is this right?

    Thanks in advance

Posting Permissions

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