none
changing datasource path in mergemail template RRS feed

  • Question

  • Hi,

    we offer a .NET/C# developed application, which interacts with word via Com Interop.

    We also deliver some word templates, prepared for the users needs. Among them, there are also templates with mergemail  functionality, and they are already bound to an empty datasource, so that they can be distinguished from other normal templates.

    Unfortunatley, Word stores an absolute path to the datasource, which becomes of course invalid in our delivered templates, for they are stored for sure in a location differently to the one, where we prepared them. (Or is it possible somehow possible to tell word a relative path, which I don't know so far ?)

    In this case, Word asks the user for a new datasource path during opening the template or creating a new doucment based on the template, because the datasource path is invalid. Unfortunately this happens also, if the documentcreation happens silently, with an invisible started Wordprocess. This is a pitty, because I would change the datasource for the newly created document anyway.

    So my question is, whether there is a possiblity, to create a document programmatically based on a mergemail template with an invalid datasourcepath, without showing the user the dialog for entering a new datasourcepath, because this change I would do  programmatically as well.

    regards Fritz

     

    Thursday, March 17, 2011 11:22 AM

Answers

  • > Unfortunately, Word stores an absolute path to the datasource,
    > which becomes of course invalid in our delivered templates,
    > for they are stored for sure in a location differently to the one,
    > where we prepared them.

    In the above, you say the problem is caused by a change of template location.  But I'm wondering if that is right. 

    As the templates store the absolute path to the datasource, it seems it shouldn't matter where the templates are stored.  If the datasource path does not change, the templates will find the datasource.

    However, if the datasource path changes, then the templates won't find the datasource, wherever the templates are stored.

     

    In outline, I think the solution is as follows:

    1.     Distribute your templates as normal Word documents (as has already been suggested). 

    2.     When your application installs on a client machine, store the new path to the datasource in a text file in a known location.  (As the Word Object Model has a method that returns the path to the Word templates folder, consider whether the text file could be stored in the Word templates folder, as that location will be "known" on all client machines.)

    3.     In each template, write code for the Document_New event procedure (which will run whenever a new document is created from a template).  The code should perform the following tasks:

    (a)    Determine if the text file exists.

    (b)    If the text file exists, read the path to the datasource file (stored in the text file); determine if the datasource file exists; if the datasource file exists, programmatically change the document to a mailmerge document.

    (c)    If the text file or datasource file do not exist, prompt the user for the new location of the datasource file (using, for example, the File-Open dialog box); store the new location in the text file; and proceed as in subparagraph (b) above.

     

    Saturday, March 26, 2011 11:38 PM

All replies

  • Hello Fritz,

    Thanks for your question. I am trying to involve someone familiar with this topic to further look at this issue. There might be some time delay. Appreciate your patience. Thank you for your understanding and have a nice weekend.


    Bessie Zhao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, March 18, 2011 9:32 AM
  • Hello Fritz,

    I think the best way to do that would be to set the Mail Merge as a Normal Word document.  This can be done in VBA with this command:
    ActiveDocument.MailMerge.MainDocumentType=wdNotAMergeDocument

    I'm not certain of the code differences in C# or .NET as I typically work within VBA only, but I'm sure it's very similar in VB.NET

    This removes the data source from the file but retains all of the other properties including the merge fields.  When you are ready to switch it back to a mail merge document, you can simply add the data source and it will be ready.


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Thanks!
    Adrian Microsoft Online Community Support

    Friday, March 18, 2011 5:31 PM
    Moderator
  • Hi Adrian,

    I'm not sure whether you've completely understood the problem. To be able to run this command the ActiveDocument must be loaded. This can be done by an Open() or a Create() call for the Documents collection of the Application object. And already here the unwanted dialog is displayed which I want to avoid, although the Wordprocess is started invisible.

    Or is there another possiblity, to get an ActiveDocument object without opening the document ?

     

    Wednesday, March 23, 2011 2:50 PM
  • 1. I am not sure, but suspect that Adrian Pa is suggesting that you make your document "Not a Merge Document" then distribute /that/ document, i.e. it is not a Mail merge main document and you should not see the dialog box. However, in that case you will in fact lose any sorts/filters and you will probably lose the information about the type of merge (email/letter/labels etc.). You may need to store that kind of info. somewhere else in the document (e.g. in some Document Variables or Properties) and write code that gets those values and uses OpenDataSource to re-open the source.

    2. For a data source such as a Word document, as long as the data source is in the same folder as the mail merge main document, Word will probably find it, despite the fact that the absolute path is recorded in the XML. However, I am not sure whether that applies to all recent versions of Windows and/or Word, and whether it will work if the documents are in a network folder rather than on a local drive. Worse, if there is a file at the location specified in the absolute path name, I am reasonably sure that Word will open that data source. You may be able to avoid that last problem by fixing the SELECT statement in the settings.xml so that it is "SELECT * FROM mydatasource.docx", and the Target of the relationship specified in settings to "mydatasource.docx" rather than using the absolute paths.

    However, that may not work for all types of data source, and if the data source is in a folder relative to the main document, I do not know how you can specify a relative path name in this case - e.g. SELECT * FROM ..\ds\mydatasource.docx does not work because Word prepends a "\\", looks for \\..\ds\mydatasourcedocx, and does not find it. Perhaps it is worth looking a little bit harder for some syntax that works.

     


    Peter Jamieson
    Wednesday, March 23, 2011 9:46 PM
  • > Unfortunately, Word stores an absolute path to the datasource,
    > which becomes of course invalid in our delivered templates,
    > for they are stored for sure in a location differently to the one,
    > where we prepared them.

    In the above, you say the problem is caused by a change of template location.  But I'm wondering if that is right. 

    As the templates store the absolute path to the datasource, it seems it shouldn't matter where the templates are stored.  If the datasource path does not change, the templates will find the datasource.

    However, if the datasource path changes, then the templates won't find the datasource, wherever the templates are stored.

     

    In outline, I think the solution is as follows:

    1.     Distribute your templates as normal Word documents (as has already been suggested). 

    2.     When your application installs on a client machine, store the new path to the datasource in a text file in a known location.  (As the Word Object Model has a method that returns the path to the Word templates folder, consider whether the text file could be stored in the Word templates folder, as that location will be "known" on all client machines.)

    3.     In each template, write code for the Document_New event procedure (which will run whenever a new document is created from a template).  The code should perform the following tasks:

    (a)    Determine if the text file exists.

    (b)    If the text file exists, read the path to the datasource file (stored in the text file); determine if the datasource file exists; if the datasource file exists, programmatically change the document to a mailmerge document.

    (c)    If the text file or datasource file do not exist, prompt the user for the new location of the datasource file (using, for example, the File-Open dialog box); store the new location in the text file; and proceed as in subparagraph (b) above.

     

    Saturday, March 26, 2011 11:38 PM