Re: Set datasource for template in code w/no user intervention

From: Mark Hammer (Mark_at_NoSpamNone-AccessAdvantage.com)
Date: 03/14/04


Date: Sun, 14 Mar 2004 13:42:42 -0800

Peter and Doug,

The command you suggested, ActiveDocument.MailMerge.OpenDataSource, is the
one I am using, as seen in my original post.

I tried shortening it to leave off the optional parameters, but the outcome
is the same.

My problem remains: this code works as intended when running against Word
2003 under Windows XP Professional; however, with Word 97 under Windows 98,
the same code causes an "Open Data Source" dialog to open.

Here is the code I've just tested:

    With objWord
        .Application.DisplayAlerts = 0 ' wdAlertsNone = 0
        ' open template file
        .Documents.Open FileName:=strTemplatePath, _
            ConfirmConversions:=False, ReadOnly:=False,
AddToRecentFiles:=False, _
            PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
            WritePasswordDocument:="", WritePasswordTemplate:="", Format:=0

         .ActiveDocument.MailMerge.OpenDataSource _
                strTempDataPath, LinkToSource:=True, AddToRecentFiles:=False

         ' original use, substituted above for test. Results are the same
         '.ActiveDocument.MailMerge.OpenDataSource Name:=strTempDataPath, _
            ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True,
_
            AddToRecentFiles:=False, PasswordDocument:="",
PasswordTemplate:="", _
            WritePasswordDocument:="", WritePasswordTemplate:="",
Revert:=False, _
            Format:=3, Connection:="", SQLStatement:="" '
Format=3="wdOpenFormatRTF"

            With .ActiveDocument.MailMerge
                .Destination = 0 ' wdSendToNewDocument
                .SuppressBlankLines = True
                With .DataSource
                    .FirstRecord = 1
                    .LastRecord = -16
                End With
                .Execute Pause:=True
            End With

        ' Close the template file without saving (.Close (0)
        ' or, Close the template file and prompt for save (.Close (-2)
        .Windows(strTemplateFileName).Activate
        .ActiveWindow.Close (0)
   End With

--Mark Hammer

"Doug Robbins - Word MVP - DELETE UPPERCASE CHARACTERS FROM EMAIL ADDRESS"
<dkr@mOSTvALUABLEpROFESSIONALs.org> wrote in message
news:uX332fNCEHA.2888@TK2MSFTNGP09.phx.gbl...
> Mark,
>
> The command you need is
>
> ActiveDocument.MailMerge.OpenDataSource
>
> For the details of how to use it, check the VBA help file.
>
> --
> Please post any further questions or followup to the newsgroups for the
> benefit of others who may be interested. Unsolicited questions forwarded
> directly to me will only be answered on a paid consulting basis.
>
> Hope this helps
> Doug Robbins - Word MVP
> "Mark Hammer" <Mark@AccessAdvantage.com> wrote in message
> news:eO424lMCEHA.1236@TK2MSFTNGP11.phx.gbl...
> > Thanks Peter.
> >
> > I don't think I understand. When I try to set
> > ActiveDocument.MailMerge.DataSource.Name I get an error that it's
> > read-only.
> >
> > Is there a difference between the ActiveDocument and the Main document?
> > I'm
> > not familiar with Main document.
> >
> > --Mark
> >
> >
> >
> > "Peter Hewett" <nospam@xtra.co.nz> wrote in message
> > news:79u450p6531t6tkigi360dl4d1oqr04gs2@4ax.com...
> >> Hi Mark Hammer
> >>
> >> Check out the following:
> >> ActiveDocument.MailMerge.DataSource.Name
> >>
> >> When using the DataSource property ensure that the document you are
using
> > (the
> >> active document in my example) is the Main document.
> >>
> >> HTH + Cheers - Peter
> >>
> >>
> >> "Mark Hammer" <Mark@NoSpamNone-AccessAdvantage.com>, said:
> >>
> >> >More info:
> >> >
> >> >The code I posted works when running from Access 2002 under Windows XP
> > Pro
> >> >and Word 2003.
> >> >
> >> >However, it doesn't work (Open Data Source Dialog box opens) when
> >> >running
> >> >from Access 2002 runtime under Win98 and Word97.
> >> >
> >> >--Mark Hammer
> >> >
> >> >
> >> >
> >> >"Mark Hammer" <Mark@NoSpamNone-AccessAdvantage.com> wrote in message
> >> >news:uN2zykJCEHA.2740@TK2MSFTNGP12.phx.gbl...
> >> >> Greetings,
> >> >>
> >> >> I need to open a template in code (from an Access 2002 front end
> > database)
> >> >> and set its datasource without user intervention.
> >> >>
> >> >> Some users have Word 97, some have later versions.
> >> >>
> >> >> My current code (below) requires user intervention ("Open Data
Source"
> >> >> dialog box) when the template's original data source does not exist.
> >> >>
> >> >> How can I modify this to avoid the user having to set it?
> >> >>
> >> >> TIA,
> >> >>
> >> >> Mark Hammer
> >> >> Lake Oswego, Oregon, U.S.
> >> >>
> >> >>
> >> >> code below from Access 2002 vba routine:
> >> >> ============
> >> >> On Error Resume Next
> >> >>
> >> >> ' Attempt to reference Word if it is already running.
> >> >> Set objWord = GetObject(, "Word.Application")
> >> >>
> >> >> ' If true, Word is not running.
> >> >> If objWord Is Nothing Then
> >> >> ' Create a new instance of Word.
> >> >> Set objWord = CreateObject("Word.Application")
> >> >> End If
> >> >>
> >> >> ' If true, MS Word is not installed.
> >> >> If objWord Is Nothing Then
> >> >> fMsgBox "Word is not installed on your computer"
> >> >> GoTo ProcExit
> >> >> End If
> >> >>
> >> >> On Error GoTo ProcErr
> >> >>
> >> >> With objWord
> >> >> ' open template file
> >> >> .Documents.Open FileName:=strTemplatePath, _
> >> >> ConfirmConversions:=False, ReadOnly:=False,
> >> >> AddToRecentFiles:=False, _
> >> >> PasswordDocument:="", PasswordTemplate:="",
Revert:=False,
> > _
> >> >> WritePasswordDocument:="", WritePasswordTemplate:="",
> >> >Format:=0
> >> >>
> >> >> .ActiveDocument.MailMerge.OpenDataSource
> > Name:=strTempDataPath, _
> >> >> ConfirmConversions:=False, ReadOnly:=False,
> >> >LinkToSource:=True,
> >> >> _
> >> >> AddToRecentFiles:=False, PasswordDocument:="",
> >> >> PasswordTemplate:="", _
> >> >> WritePasswordDocument:="", WritePasswordTemplate:="",
> >> >> Revert:=False, _
> >> >> Format:=3, Connection:="", SQLStatement:="" '
> >> >> Format=3="wdOpenFormatRTF"
> >> >>
> >> >> With .ActiveDocument.MailMerge
> >> >> .Destination = 0 ' wdSendToNewDocument
> >> >> .SuppressBlankLines = True
> >> >> With .DataSource
> >> >> .FirstRecord = 1
> >> >> .LastRecord = -16
> >> >> End With
> >> >> .Execute Pause:=True
> >> >> End With
> >> >>
> >> >> ' Close the template file without saving (.Close (0)
> >> >> ' or, Close the template file and prompt for save (.Close
(-2)
> >> >> .Windows(strTemplateFileName).Activate
> >> >> .ActiveWindow.Close (0)
> >> >>
> >> >> ' save the merge document created (having multiple letters)
> >> >> .ChangeFileOpenDirectory strWordDocsPath
> >> >> .ActiveDocument.SaveAs FileName:=strSaveAsFileName,
FileFormat
> > _
> >> >> :=0, LockComments:=False, Password:="",
AddToRecentFiles:=
> > _
> >> >> True, WritePassword:="", ReadOnlyRecommended:=False,
> >> >> EmbedTrueTypeFonts:= _
> >> >> False, SaveNativePictureFormat:=False,
> > SaveFormsData:=False, _
> >> >> SaveAsAOCELetter:=False
> >> >>
> >> >> ' Show Word, and maximize it.
> >> >> ' Note: maximization must happen after making visible!
> >> >> On Error GoTo ProcErr
> >> >> .Visible = True
> >> >> .Application.WindowState = 1 ' 1 = wdWindowStateMaximize
> >> >> End With
> >> >>
> >> >> ================
> >> >>
> >> >>
> >> >>
> >> >>
> >> >>
> >> >
> >>
> >
> >
>
>



Relevant Pages

  • Re: make buttons invisible
    ... I right clicked on my command ... Peter, thank for writing again. ... Dim intHeight As Integer ... .FirstRecord = wdDefaultFirstRecord ...
    (microsoft.public.word.mailmerge.fields)
  • Re: Command button continues to be visible!
    ... As I mentioned I have unprotected the sheet. ... "Peter T" wrote: ... But I tried the same by placing a new command button and tried and the ...
    (microsoft.public.excel.programming)
  • Re: Pci.sys missing or corrupt...(Ive tried everything)
    ... Slight correction, peter. ... For security purposes the default setings for the Recovery Console do not allow users to browse folders other than the root and system folders. ... ALLOWALLPATHS = TRUE removes this restriction and permits the user to browse the entire drive and permits access to all files and folders on the drive. ... For the allowallpaths = true to work the SET command for the Recovery Console must be already enabled, the SET command is enabled in the Security Policy. ...
    (microsoft.public.windowsxp.help_and_support)
  • Re: Command button continues to be visible!
    ... Are your sure the button is named "alfa", I don't mean as seen in the names ... "Peter T" wrote: ... But I tried the same by placing a new command button and tried and the ...
    (microsoft.public.excel.programming)
  • Re: Error sync-ing internet time
    ... Doug, Followed your instrux, all went well through the line w32tmregister, ... Once the Command Prompt window opens up type NET START ... W32TIME This will start the Windows Time service, ... Procedure Call which was cited in my error message, ...
    (microsoft.public.windowsxp.general)