Reply
 
Thread Tools Display Modes
  #1   Report Post  
Posted to microsoft.public.word.mailmerge.fields
darren via OfficeKB.com
 
Posts: n/a
Default Open mail merge doc as read only

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

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...merge/200604/1
  #2   Report Post  
Posted to microsoft.public.word.mailmerge.fields
darren via OfficeKB.com
 
Posts: n/a
Default Open mail merge doc as read only

OK, got that sorted but now get error 462, can anyone please advise what I am
doing wrong?

Thanks

darren wrote:

--
Message posted via http://www.officekb.com
  #3   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Peter Jamieson
 
Posts: n/a
Default Open mail merge doc as read only

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
"darren via OfficeKB.com" u11419@uwe wrote in message
news:5ef8baae20b62@uwe...
OK, got that sorted but now get error 462, can anyone please advise what I
am
doing wrong?

Thanks

darren wrote:

--
Message posted via http://www.officekb.com



  #4   Report Post  
Posted to microsoft.public.word.mailmerge.fields
darren via OfficeKB.com
 
Posts: n/a
Default Open mail merge doc as read only

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.

Peter Jamieson wrote:
At what point is it going wrong?


--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...merge/200604/1
  #5   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Peter Jamieson
 
Posts: n/a
Default Open mail merge doc as read only

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

"darren via OfficeKB.com" u11419@uwe wrote in message
news:5f0061498e159@uwe...
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.

Peter Jamieson wrote:
At what point is it going wrong?


--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...merge/200604/1





  #6   Report Post  
Posted to microsoft.public.word.mailmerge.fields
darren via OfficeKB.com
 
Posts: n/a
Default Open mail merge doc as read only

Thanks Peter, the second suggestion sorted it. I'd tried something similar
yesterday but forgot to change the CreateObject to 'Word.Application'.

Very grateful.

)

Peter Jamieson wrote:

--
Darren

Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...merge/200604/1
Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Word Mail Merge feature for custom templates S.Sanghani Mailmerge 3 January 11th 06 10:22 AM
word docs open as mail merge main page-how to reset as a word doc. lyger01 Mailmerge 1 April 21st 05 08:04 AM
mail merge with attachments AS Mailmerge 5 April 9th 05 09:49 AM
Mail Merge Issue With Office 97 - Excel Data Source Matt Thorley Mailmerge 1 February 15th 05 11:38 PM
no DDE connection to open data for mail merge LH1844 Mailmerge 1 December 14th 04 10:23 AM


All times are GMT +1. The time now is 05:42 PM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"