using %temp% as value in a mail merge datasource RRS feed

  • Question

  • As a normal C# developer I am a little bit on weak ground when it comes to .dot files.

    I have a word template, and right now it uses a text file as datasource, and this datasource file location lies in a very fixed path.

    What I would like was that the datasource should lie in the %temp% folder.

    I can see two things.

    If I in the intermediate window in VBA writes:

    ? ActiveDocument.MailMerge.DataSource.Name

    Then I can see the very hardcoded datasource, but I cannot change it because the property don't have a setter.

    Then I added the "mail merge helper..." to my ribbon, and inthere I can open a specific file. And if I in the file picker writes "%temp%\myfile.txt" then that works, but the hardcoded temp path for my user is inserted.

    If someone knows it isn't possible that would also be very helpful for me to know. Since I don't know Word, or VBA for that matter, very well.

    Monday, February 27, 2012 8:39 AM


All replies

  • You could use

    Dim strTempPath As String
    strTempPath = Environ("Temp")

    to get the path of the user's Temp folder.

    Regards, Hans Vogelaar

    Monday, February 27, 2012 9:10 PM
  • Ok,

    That would take me part of the way. But I would still need to set the path itself.

    Setting it to %temp% or Environ("Temp") is more or less the same for me, I still don't know how to set it programatically.

    Tuesday, February 28, 2012 8:29 AM
  • I'm not so sure that it's a good idea to change the user's temp folder!

    Regards, Hans Vogelaar

    Tuesday, February 28, 2012 4:22 PM
  • I am not changing the users temp folder. In the company all users have different temp folders.

    Normally their temp folder is \users\<username>\temp

    A file is then stored in their temp folder as they browse a website.

    And then the dot file will need to fetch it based on that folder.

    Hence, I would like to have a dot file, whos mail merge source is %temp%\whatever.txt or Environ("Temp") + \\whatever.txt if it is set programatically or in some kind of setting.

    But when i set it in the mail merge helper... the %temp% is evaluated on the fly, and I don't know how to set it programatically, because ActiveDocument.MailMerge.DataSource.Name don't have a setter.

    I could ofcourse fire up a Hex editor, and change it directly in the binary .dot file. I wonder if that would work.

    Wednesday, February 29, 2012 1:32 PM
  • To set the data source for mailmerge, use the MailMerge.OpenDataSource method of the document - see (includes examples).

    Regards, Hans Vogelaar

    • Marked as answer by Thomas Koelle Thursday, March 1, 2012 8:19 AM
    Wednesday, February 29, 2012 3:31 PM
  • Thanks!
    Thursday, March 1, 2012 8:19 AM