Wrox Home  
Search P2P Archive for: Go

  Return to Index  

access thread: Sending bookmark info to Word


Message #1 by "Tammy Tappan" <GRTappan@e...> on Thu, 29 Nov 2001 23:50:50
Me again!



Okay, here's the code to define bookmarks:



General declarations:

Public m_objWord As Word.Application

Public m_objDoc As Word.Document



Bookmark Sub:

Public Sub InsertTextAtBookmark(strBkmk As String, varText As Variant)



    m_objDoc.Bookmarks(strBkmk).Select

    m_objWord.Selection.Text = varText & ""



End Sub





My code (I just did one line for the Bookmark; when I can get 1 to work, 

they'll all work!):

Public Sub CreateSCIDRequestDoc(strUserName As String)



    Static WordObj As Word.Application

    Dim strSaveDir As String

    Dim strFileName As String

    Dim frmNewEmplSecurity6 As Form

          

    InsertTextAtBookmark "Participant", frmNewEmplSecurity6![txtUsersName]

    

          

    strSaveDir = "K:\DATA\PRIVATE\Iscs\ServiceCenter\SCUserIDRequests\"

    strFileName = "User ID Request for " & strUserName & ".doc"



    Set WordObj = Nothing

    Set WordObj = CreateObject("Word.Application")

    

    WordObj.Documents.Open (m_strDIR & m_strTEMPLATE)

    

    WordObj.Visible = True

    

    ActiveDocument.SaveAs FileName:=strSaveDir & strFileName

    



End Sub



It should be taking the info from that field ( [txtUsersName] ) on the 

open form, and sending it to the Word document & putting it into the 

bookmark labeled Participant. The bookmarks are all set up in Word.



It doesn't like how I've done the insert. ?? I'm following the example in 

my book - gonna have to talk to those authors!!



+Tammy
Message #2 by "John Ruff" <papparuff@c...> on Thu, 29 Nov 2001 17:13:19 -0800
Tammy,



Here is the documentation from auto97.exe that talks about referencing

bookmarks



Finding a Specific Location in a Word Document



Using Automation, it is possible to find a specific location in a

Microsoft Word 97 document.  This example demonstrates how to select a

specific bookmark in a Word document, and then use the InsertAfter

method to place some text after it.



Sub FindBookMark()

    'This example assumes that you have created a document named

    '"autFindLoc.Doc" in the C:\My Documents folder.  In this document

    'type: "The shipment will arrive in  within three (3) days."



    'Note there are two spaces following the word "arrive". Insert a 

    'bookmark named "City" between these two spaces.

    

    Dim WordObj As Word.Application

    Set WordObj = CreateObject("Word.Application")

    With WordObj

        'Visible is set True in order to view the operation.

        .Visible = True

        .Documents.Open ("C:\My Documents\autFindLoc.doc")

        'Goes to the bookmark named "City", and inserts "Charleston"



        .ActiveDocument.Bookmarks("City").Select

        .Selection.InsertAfter "Charleston"

        .Quit wdSaveChanges

    End With

    Set WordObj = Nothing

End Sub



John Ruff - The Eternal Optimist :-)







-----Original Message-----

From: Tammy Tappan [mailto:GRTappan@e...] 

Sent: Thursday, November 29, 2001 11:51 PM

To: Access

Subject: [access] Sending bookmark info to Word





Me again!



Okay, here's the code to define bookmarks:



General declarations:

Public m_objWord As Word.Application

Public m_objDoc As Word.Document



Bookmark Sub:

Public Sub InsertTextAtBookmark(strBkmk As String, varText As Variant)



    m_objDoc.Bookmarks(strBkmk).Select

    m_objWord.Selection.Text = varText & ""



End Sub





My code (I just did one line for the Bookmark; when I can get 1 to work,



they'll all work!):

Public Sub CreateSCIDRequestDoc(strUserName As String)



    Static WordObj As Word.Application

    Dim strSaveDir As String

    Dim strFileName As String

    Dim frmNewEmplSecurity6 As Form

          

    InsertTextAtBookmark "Participant",

frmNewEmplSecurity6![txtUsersName]

    

          

    strSaveDir = "K:\DATA\PRIVATE\Iscs\ServiceCenter\SCUserIDRequests\"

    strFileName = "User ID Request for " & strUserName & ".doc"



    Set WordObj = Nothing

    Set WordObj = CreateObject("Word.Application")

    

    WordObj.Documents.Open (m_strDIR & m_strTEMPLATE)

    

    WordObj.Visible = True

    

    ActiveDocument.SaveAs FileName:=strSaveDir & strFileName

    



End Sub



It should be taking the info from that field ( [txtUsersName] ) on the 

open form, and sending it to the Word document & putting it into the 

bookmark labeled Participant. The bookmarks are all set up in Word.



It doesn't like how I've done the insert. ?? I'm following the example

in 

my book - gonna have to talk to those authors!!



+Tammy

---

You are currently subscribed to access as: papparuff@c... To

unsubscribe send a blank email to $subst('Email.Unsub')



Read the future with ebooks at B&N

http://service.bfast.com/bfast/click?bfmid=2181&sourceid=38934667&catego

ryid=rn_ebooks







Message #3 by "Tammy Tappan" <GRTappan@e...> on Fri, 30 Nov 2001 15:29:39
Interesting! Just like Microsoft - at least 2 ways to do everything! 

However, all that code for each bookmark seems like a lot, especially 

since I have 15 bookmarks to do! Also, it doesn't address pulling the info 

from the current form.



What's your take on the code I have already? I got it from Beginning 

Access 2000 VBA, page 546. Here's their explanation:



"To use the bookmarks in the document we use the Bookmarks collection. 

This is just like the Controls collection on a form, and for a document, 

contains one entry for each bookmark in the document. We use the name of 

the bookmark to index into the collection, and use the Select method to 

make the bookmark the current selection. This would be the equivalent of 

just clicking on the bookmark in the Word document.



Private Sub InsertTextAtBookMark (strBkmk as String, varText as Variant)



       m_objDoc.Bookmark(strBkmk).Select



Once this is done the Selection object is activated, containg the 

currently selected items. When typing documents the selection is visible 

as a highlight, for example when you select a word or sentence. The 

Selection object has a property called Text, which contains the text in 

the selection. We then set the text of the selection to the text we want 

to insert. (Tammy's note: this would have to be coded to pull the info 

from the field on the active form.) We've appended an empty string onto 

the text because the text comes from the databse, and might contain a null 

value. Adding this empty string just prevents any errors about null 

values. (Tammy's note: since all fields on the form are required, would I 

need this?)



       m_objWord.Selection.Text = varText & ""



End Sub





So, is this not useful for me? In the actual coding for the procedure to 

do the send, they have defined the items containing the info as 

Recordsets; my info isn't a Recordset, though, as I understand them, since 

I'm pulling from a form, not a table (most of the data on the form will 

never write to a table).



Then they simply add the following code for each bookmark:



InsertTextAtBookMark "ContactName", recSupp("ContactName")



What do you think of the coding this way?



+Tammy
Message #4 by "John Ruff" <papparuff@c...> on Fri, 30 Nov 2001 09:28:29 -0800
I say give it a try and see how it works.  Instructions such as this are

written for the ideal situation, which rarely occurs.  But it is a good

starting point.  Of course code will have to be modified to fit your

specifics.



John Ruff - The Eternal Optimist :-)







-----Original Message-----

From: Tammy Tappan [mailto:GRTappan@e...] 

Sent: Friday, November 30, 2001 3:30 PM

To: Access

Subject: [access] RE: Sending bookmark info to Word





Interesting! Just like Microsoft - at least 2 ways to do everything! 

However, all that code for each bookmark seems like a lot, especially 

since I have 15 bookmarks to do! Also, it doesn't address pulling the

info 

from the current form.



What's your take on the code I have already? I got it from Beginning 

Access 2000 VBA, page 546. Here's their explanation:



"To use the bookmarks in the document we use the Bookmarks collection. 

This is just like the Controls collection on a form, and for a document,



contains one entry for each bookmark in the document. We use the name of



the bookmark to index into the collection, and use the Select method to 

make the bookmark the current selection. This would be the equivalent of



just clicking on the bookmark in the Word document.



Private Sub InsertTextAtBookMark (strBkmk as String, varText as Variant)



       m_objDoc.Bookmark(strBkmk).Select



Once this is done the Selection object is activated, containg the 

currently selected items. When typing documents the selection is visible



as a highlight, for example when you select a word or sentence. The 

Selection object has a property called Text, which contains the text in 

the selection. We then set the text of the selection to the text we want



to insert. (Tammy's note: this would have to be coded to pull the info 

from the field on the active form.) We've appended an empty string onto 

the text because the text comes from the databse, and might contain a

null 

value. Adding this empty string just prevents any errors about null 

values. (Tammy's note: since all fields on the form are required, would

I 

need this?)



       m_objWord.Selection.Text = varText & ""



End Sub





So, is this not useful for me? In the actual coding for the procedure to



do the send, they have defined the items containing the info as 

Recordsets; my info isn't a Recordset, though, as I understand them,

since 

I'm pulling from a form, not a table (most of the data on the form will 

never write to a table).



Then they simply add the following code for each bookmark:



InsertTextAtBookMark "ContactName", recSupp("ContactName")



What do you think of the coding this way?



+Tammy

---

You are currently subscribed to access as: papparuff@c... To

unsubscribe send a blank email to $subst('Email.Unsub')



Read the future with ebooks at B&N

http://service.bfast.com/bfast/click?bfmid=2181&sourceid=38934667&catego

ryid=rn_ebooks







Message #5 by "Tammy Tappan" <GRTappan@e...> on Fri, 30 Nov 2001 17:52:11
Okay, so now I'm back to my original problem - I tried it, & it doesn't 

work! I get the Run-time error '91' - Object variable or With block 

variable not set. It points to the line



InsertTextAtBookmark "Participant", frmNewEmplSecurity6![txtUsersName]



"Participant" is the Word bookmark name; frmNewEmplSecurity6! refers to 

the active form, & [txtUsersName] refers (I think??) to the field on the 

form where the info to insert into the bookmark is found.





The code:



Public Const m_strDIR As String = "K:\DATA\PRIVATE\Iscs\Templates\"

Public Const m_strTEMPLATE As String = "AcessSCIDRequest.dot"

Public m_objWord As Word.Application

Public m_objDoc As Word.Document



Public Sub CreateSCIDRequestDoc(strUserName As String)



    Static WordObj As Word.Application

    Dim strSaveDir As String

    Dim strFileName As String

    Dim frmNewEmplSecurity6 As Access.Form

           

    InsertTextAtBookmark "Participant", frmNewEmplSecurity6

    

    

    strSaveDir = "K:\DATA\PRIVATE\Iscs\ServiceCenter\SCUserIDRequests\"

    strFileName = "User ID Request for " & strUserName & ".doc"



    Set WordObj = Nothing

    Set WordObj = CreateObject("Word.Application")

    

    WordObj.Documents.Open (m_strDIR & m_strTEMPLATE)

    

    WordObj.Visible = True

    

    ActiveDocument.SaveAs FileName:=strSaveDir & strFileName

    



End Sub



Public Sub InsertTextAtBookmark(strBkmk As String, varText As Variant)



    m_objDoc.Bookmarks(strBkmk).Select

    m_objWord.Selection.Text = varText & ""



End Sub



+Tammy





> I say give it a try and see how it works.  Instructions such as this are

> written for the ideal situation, which rarely occurs.  But it is a good

> starting point.  Of course code will have to be modified to fit your

> specifics.

> 

> John Ruff - The Eternal Optimist :-)

> 

Message #6 by aland@a... on Fri, 30 Nov 2001 22:58:13
Am I missing something?  Nowhere do I see you set m_objDoc or m_objWord 

before referencing them in InsertTextAtBookmark.

... AL





> Okay, so now I'm back to my original problem - I tried it, & it doesn't 

> work! I get the Run-time error '91' - Object variable or With block 

> variable not set. It points to the line

> 

> InsertTextAtBookmark "Participant", frmNewEmplSecurity6![txtUsersName]

> 

> "Participant" is the Word bookmark name; frmNewEmplSecurity6! refers to 

> the active form, & [txtUsersName] refers (I think??) to the field on the 

> form where the info to insert into the bookmark is found.

> 

> 

> The code:

> 

> Public Const m_strDIR As String = "K:\DATA\PRIVATE\Iscs\Templates\"

> Public Const m_strTEMPLATE As String = "AcessSCIDRequest.dot"

> Public m_objWord As Word.Application

> Public m_objDoc As Word.Document

> 

> Public Sub CreateSCIDRequestDoc(strUserName As String)

> 

>     Static WordObj As Word.Application

>     Dim strSaveDir As String

>     Dim strFileName As String

>     Dim frmNewEmplSecurity6 As Access.Form

>            

>     InsertTextAtBookmark "Participant", frmNewEmplSecurity6

>     

>     

>     strSaveDir = "K:\DATA\PRIVATE\Iscs\ServiceCenter\SCUserIDRequests\"

>     strFileName = "User ID Request for " & strUserName & ".doc"

> 

>     Set WordObj = Nothing

>     Set WordObj = CreateObject("Word.Application")

>     

>     WordObj.Documents.Open (m_strDIR & m_strTEMPLATE)

>     

>     WordObj.Visible = True

>     

>     ActiveDocument.SaveAs FileName:=strSaveDir & strFileName

>     

> 

> End Sub

> 

> Public Sub InsertTextAtBookmark(strBkmk As String, varText As Variant)

> 

>     m_objDoc.Bookmarks(strBkmk).Select

>     m_objWord.Selection.Text = varText & ""

> 

> End Sub

> 

> +Tammy


  Return to Index