A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Word » Mailmerge
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Filename of Data Source



 
 
Thread Tools Display Modes
  #1  
Old June 25th, 2009, 09:20 AM posted to microsoft.public.word.mailmerge.fields
SSwan
external usenet poster
 
Posts: 10
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  
Old June 25th, 2009, 10:19 AM posted to microsoft.public.word.mailmerge.fields
Graham Mayor
external usenet poster
 
Posts: 18,297
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  
Old June 25th, 2009, 11:15 AM posted to microsoft.public.word.mailmerge.fields
SSwan
external usenet poster
 
Posts: 10
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




 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 07:57 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.