Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Mailmerge and Fax / April 2006

Tip: Looking for answers? Try searching our database.

Open mail merge doc as read only

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
darren - 18 Apr 2006 17:34 GMT
Hi

Totally new to word vba, I have put together the following code in Access but
wish the word doc to opened as read only to protect the original text.

Function fnMergeIt(strDoc As String, strTbl As String)

 
  Dim objWord As Word.Document
  Dim strConnection As String
  Dim path As String
  Dim strDataDir As String
 
   strConnection = "DSN=MS Access Database;DBQ=" & CurrentProject.FullName &
" ;FIL=MS Access;"
 
   strDoc = Chr$(34) & CurrentProject.path & "\" & strDoc & Chr$(34)
 
   Set objWord = GetObject(strDoc, "Word.Document")

   ' Set the mail merge data source
   objWord.MailMerge.OpenDataSource _
   Name:=CurrentProject.FullName, _
   LinkToSource:=True, _
   Connection:=strConnection, _
   ReadOnly:=True, _
   SQLStatement:="SELECT * FROM [" & strTbl & "]"
     
   objWord.MailMerge.ViewMailMergeFieldCodes = False
   
  ' Make Word visible.
   objWord.Application.Visible = True
 
Set objWord = Nothing

End Function

Thanks
darren - 18 Apr 2006 18:56 GMT
OK, got that sorted but now get error 462, can anyone please advise what I am
doing wrong?

Thanks
Peter Jamieson - 18 Apr 2006 19:10 GMT
At what point is it going wrong?

If you are using Word 2002/2003, you may need to apply the following
Knowledgebase article:

"You receive the "Opening this will run the following SQL command" message
when you open a Word mail merge main document that is linked to a data
source"

http://support.microsoft.com/kb/825765

Peter Jamieson
> OK, got that sorted but now get error 462, can anyone please advise what I
> am
> doing wrong?
>
> Thanks
darren - 19 Apr 2006 09:32 GMT
Hi Peter, thanks for your ongoing help.

error 462 "Remote server does not exists or it is unvailable"

Reading the ms support page I believe it is to do with my referencing of
variables, but can't work out exactly what. Code is now as follows:

Function fnMergeIt(strDoc As String, strTbl As String)
On Error GoTo Err_fnMergeIt

 
  Dim objApp As Object
  Dim objWord As Word.Document
  Dim strConnection As String
  Dim path As String
  Dim strDataDir As String
 
   strConnection = "DSN=MS Access Database;DBQ=" & CurrentProject.FullName &
" ;FIL=MS Access;"
 
   strDoc = Chr$(34) & CurrentProject.path & "\" & strDoc & Chr$(34)
   'Debug.Print strDoc
   
   Set objApp = CreateObject("Word.Document")

   Set objWord = Word.Documents.Open(strDoc, , True)

   ' Set the mail merge data source
   objWord.MailMerge.OpenDataSource _
   Name:=CurrentProject.FullName, _
   LinkToSource:=True, _
   Connection:=strConnection, _
   ReadOnly:=True, _
   SQLStatement:="SELECT * FROM [" & strTbl & "]"
     
   objWord.MailMerge.ViewMailMergeFieldCodes = False
   
  ' Make Word visible.
   objWord.application.Visible = True

Exit_fnMergeIt:
   Set objWord = Nothing
   Set objApp = Nothing
   Exit Function
   
Err_fnMergeIt:
   MsgBox "fnMergeIt: " & Err.Number & " - " & Err.Description
   Resume Exit_fnMergeIt

End Function

I believe it is to do with my use of the 'word' object/variables but my lack
of familiarity with word vba isn't helping.

>At what point is it going wrong?
Peter Jamieson - 19 Apr 2006 09:49 GMT
Hi Darren,

I would assume that one of the following two lines is not working:

>    Set objApp = CreateObject("Word.Document")
>    Set objWord = Word.Documents.Open(strDoc, , True)

Try either

Set objApp = CreateObject("Word.Document")
Set objWord = objApp.Documents.Open(strDoc, , True)

or do a bit of renaming and use
Dim objApp As Word.Application ' (you might need to change this)
Dim objDoc As Word.Document
Set objApp = CreateObject("Word.Application")
'objApp.Visible = True
set objDoc =  objApp.Documents.Open strDoc

NB, if you are merging to a new document, keep using objDoc to reference the
Mail Merge Main Document, and use ActiveDocument to reference the newly
created document (or set a reference to ActiveDocument and use that).

Peter Jamieson

> Hi Peter, thanks for your ongoing help.
>
[quoted text clipped - 52 lines]
>
>>At what point is it going wrong?
darren - 19 Apr 2006 11:05 GMT
Thanks Peter, the second suggestion sorted it. I'd tried something similar
yesterday but forgot to change the CreateObject to 'Word.Application'.

Very grateful.

:o)

Signature

Darren

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.