Reply
 
Thread Tools Display Modes
  #1   Report Post  
Posted to microsoft.public.word.mailmerge.fields
SSwan SSwan is offline
external usenet poster
 
Posts: 5
Default Filename of Data Source

Hello,

I am merging from multiple files and need to output in a field, the name of
the data source file. Is this possible?

Many thanks.

SSwan
  #2   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Graham Mayor Graham Mayor is offline
external usenet poster
 
Posts: 19,312
Default Filename of Data Source

The short answer is no. This information is not available to fields.

A workaround would be to create a macro to store the datasource in a
variable then use a field to display the variable. Something along the lines
of

Dim aVar As Variable
Dim oVars As Variables
Dim iNum As Integer
Dim sSource As String
Dim oDoc As Document
Set oDoc = ActiveDocument
Set oVars = oDoc.Variables
If oDoc.MailMerge.MainDocumentType wdNotAMergeDocument Then
For Each aVar In oVars
If aVar.name = "varDataSource" Then iNum = aVar.Index
Next aVar
If iNum = 0 Then
'If variable does not exist - create it and set its value to 1
oVars.Add name:="varDataSource",
Value:=oDoc.MailMerge.DataSource.name
Else
oVars("varDataSource").Value = oDoc.MailMerge.DataSource.name
End If
oDoc.Fields.Update
End If

Which you would have to remember to run before merging, or you could
incorporate the code in a macro used to run the merge.
The information would be displayed in the merge document using the following
field construction.

{ IF { DocVariable varDataSource } "Error!*" "{ DocVariable
varDataSource }" }

which traps the error message that occurs if the macro is not run. The
information is stored with the document until the macro is run again. Note
that in Word 2007 there is a bug attributable to Update KB969604 that acrews
up docvariables. This should be fixed soon (?).

An alternative would be to use a macro to write the datasource name to a
bookmark placed in the document eg

Dim sSource As String
Dim oDoc As Document
Dim oRng As Range
Set oDoc = ActiveDocument
If oDoc.MailMerge.MainDocumentType wdNotAMergeDocument Then
Set oRng = ActiveDocument.Bookmarks("DataSourceName").Range
oRng = oDoc.MailMerge.DataSource.name
oDoc.Bookmarks.Add "DataSourceName", oRng
End If

would write the name to a bookmark called "DataSourceName"

http://www.gmayor.com/installing_macro.htm
--

Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org



SSwan wrote:
Hello,

I am merging from multiple files and need to output in a field, the
name of the data source file. Is this possible?

Many thanks.

SSwan



  #3   Report Post  
Posted to microsoft.public.word.mailmerge.fields
SSwan SSwan is offline
external usenet poster
 
Posts: 5
Default Filename of Data Source

Thanks Graham. I shall give that a go.

I often refer to your website when I need to tweak a mergefield!

"Graham Mayor" wrote:

The short answer is no. This information is not available to fields.

A workaround would be to create a macro to store the datasource in a
variable then use a field to display the variable. Something along the lines
of

Dim aVar As Variable
Dim oVars As Variables
Dim iNum As Integer
Dim sSource As String
Dim oDoc As Document
Set oDoc = ActiveDocument
Set oVars = oDoc.Variables
If oDoc.MailMerge.MainDocumentType wdNotAMergeDocument Then
For Each aVar In oVars
If aVar.name = "varDataSource" Then iNum = aVar.Index
Next aVar
If iNum = 0 Then
'If variable does not exist - create it and set its value to 1
oVars.Add name:="varDataSource",
Value:=oDoc.MailMerge.DataSource.name
Else
oVars("varDataSource").Value = oDoc.MailMerge.DataSource.name
End If
oDoc.Fields.Update
End If

Which you would have to remember to run before merging, or you could
incorporate the code in a macro used to run the merge.
The information would be displayed in the merge document using the following
field construction.

{ IF { DocVariable varDataSource } "Error!*" "{ DocVariable
varDataSource }" }

which traps the error message that occurs if the macro is not run. The
information is stored with the document until the macro is run again. Note
that in Word 2007 there is a bug attributable to Update KB969604 that acrews
up docvariables. This should be fixed soon (?).

An alternative would be to use a macro to write the datasource name to a
bookmark placed in the document eg

Dim sSource As String
Dim oDoc As Document
Dim oRng As Range
Set oDoc = ActiveDocument
If oDoc.MailMerge.MainDocumentType wdNotAMergeDocument Then
Set oRng = ActiveDocument.Bookmarks("DataSourceName").Range
oRng = oDoc.MailMerge.DataSource.name
oDoc.Bookmarks.Add "DataSourceName", oRng
End If

would write the name to a bookmark called "DataSourceName"

http://www.gmayor.com/installing_macro.htm
--

Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org



SSwan wrote:
Hello,

I am merging from multiple files and need to output in a field, the
name of the data source file. Is this possible?

Many thanks.

SSwan




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
How to link a different Mailmerge header source and data source? Eleni_Malli Mailmerge 1 May 3rd 06 02:08 PM
data form button greyed out in word 97 data source Busylady Mailmerge 0 September 15th 05 10:00 PM
DotNet Data Table as Mail Merge Data Source goraya Mailmerge 1 July 7th 05 09:51 AM
How do I move data Colums within the data source of a mail merge hf Microsoft Word Help 1 July 5th 05 09:57 PM
data in mail merge letter being cut off - data source field size? tgone Mailmerge 1 May 20th 05 02:10 PM


All times are GMT +1. The time now is 03:44 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"