10 Replies - 779 Views - Last Post: 02 October 2015 - 12:07 PM Rate Topic: -----

#1 jcborland   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 137
  • Joined: 16-December 13

Opening a Word mail merge document.

Posted 01 October 2015 - 11:57 AM

Hi Everyone,

I wonder can anyone put me in the right direction for something. I've got client who has a mail merge document containing 10,000 letters to employees. Inside the document there is a reference number which identifies each employee.

I want to open the document find the reference number and create a new document for that employee and I want to use the reference number in the name of the new document. So if the employee reference is W1076, I might call the new document "LetterName(W1076)".

So basically I'll start with 1 document containing 10,000 letters and end up with 10,000 documents each containing one letter.

I looked at the xml file and it looks very complex. Is there a method for identifying each letter within a mail merged document.

Regards,
Jim.

Is This A Good Question/Topic? 0
  • +

Replies To: Opening a Word mail merge document.

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6818
  • View blog
  • Posts: 28,231
  • Joined: 12-December 12

Re: Opening a Word mail merge document.

Posted 01 October 2015 - 12:16 PM

Quote

I looked at the xml file and it looks very complex.

Will you be doing this with Office Open XML, rather than Word Interop (VBA)?

Quote

Is there a method for identifying each letter within a mail merged document.

If you examine the document in Word you'll see that there is a section break between each letter, so the letters are different sections. I don't know how that translates to XML, but you should be able to discover this by examining the XML.



I'm curious why you need to extract these 10,000 numbers from the merged document, aren't they stored somewhere already? (Even if you need to write to the 10k people who received this letter, one would hope that this list is documented somewhere.)

This post has been edited by andrewsw: 01 October 2015 - 12:18 PM

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6818
  • View blog
  • Posts: 28,231
  • Joined: 12-December 12

Re: Opening a Word mail merge document.

Posted 01 October 2015 - 12:46 PM

You should only need to work with the existing merged document once (if at all), to extract the code numbers and store them somewhere sensible. I wouldn't write C# code for this. I would work with Word VBA, probably using Find (with wildcards ?[0-9]{4}).

Find and replace text by using regular expressions

This post has been edited by andrewsw: 01 October 2015 - 12:47 PM

Was This Post Helpful? 0
  • +
  • -

#4 jcborland   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 137
  • Joined: 16-December 13

Re: Opening a Word mail merge document.

Posted 02 October 2015 - 02:05 AM

Hi Andrewsw,

The numbers are held somewhere else probably in a .csv file along with some other data that is used in the letter. That's how they use mail merge to produce the 10,000 letters.

My problem is, and maybe I'm being a bit thick here, that I need 10,000 individual documents each with this reference number in the name of the file.

They've created a data source with a few fields in it (one of which is this number), they've written a letter and placed a few mail merge fields into it and they've completed the merge and created a single document containing 10,000 letters.

I need 10,000 individual documents but I need the reference number inside brackets in the file name.

The big document with 10,000 letters in it might be called "PensionAdviceLetter" I want 10,000 small documents with names like "PensionAdviceLetter(IT100001)", "PensionAdviceLetter(OF100011)" etc. etc.

We managed to produce 10,000 individual documents OK but the names they have been given just use a sequential number starting at 1 and incrementing by 1 for each document.

I've never used Word VBA so if you have any examples of splitting a mail merged document into individual documents I'd be glad to see it.

Regards,
Jim.
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6818
  • View blog
  • Posts: 28,231
  • Joined: 12-December 12

Re: Opening a Word mail merge document.

Posted 02 October 2015 - 03:44 AM

You are presenting a moving target. Originally you said you wanted to create 10k new documents, now you are suggesting that you just want to split the existing merged document into individual documents. The format of the code numbers has also changed.

Anyway, splitting a merged document is a not infrequent request and you can find code samples quite easily:

How to Programmatically Save Each Page or Section of Document As Separate File

As well as splitting them though, you also need to pause to find the code number within the section, to use as part of the filename. To be able to do this successfully you'll need to have some understanding of the VBA code. You could start by recording a few macros and studying the resulting code. I've also mentioned using Advanced Find to locate a character followed by four digits.

This post has been edited by andrewsw: 02 October 2015 - 03:46 AM

Was This Post Helpful? 0
  • +
  • -

#6 jcborland   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 137
  • Joined: 16-December 13

Re: Opening a Word mail merge document.

Posted 02 October 2015 - 05:08 AM

I do want to create 10K new documents! I want to create them by splitting an original single document containing 10K letters. I don't even know what the format for the code is going to be or if it will be consistent. I'm thinking it'll just be everything after the word "Reference:".

For example if the lst letter says "Reference:ABC12345678" my new document will be created with the name "PensionLetter(ABC12345678)" if the 2nd letter says "Reference:W0176" my new document will be called "PensionLetter(W0176)" and so on.

I don't know VBA well enough I've been looking at c# and there is some stuff on the web but it not working as yet. I'll look at VBA if I'm stuck but I don't fancy it.

Thanks,
Jim.
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6818
  • View blog
  • Posts: 28,231
  • Joined: 12-December 12

Re: Opening a Word mail merge document.

Posted 02 October 2015 - 05:23 AM

Quote

I do want to create 10K new documents! I want to create them by splitting an original single document containing 10K letters.

I must be missing something then because, given this, I revert to my original suggestion just to extract the 10k reference numbers that you need, and then create 10k letters using this data source. I don't know why you would split the original merged document if you don't need to.

Even if you don't want to extract the numbers as a data source, you could iterate the merged document creating the new documents, without having to split the merged document into separate files. Anyway, it's up-to you ;)

Writing this from C# adds an extra level, and you would still be automating Word (navigating sections, paragraphs, etc.). However, maybe there is a nice .NET Word/Office library that makes this task a little easier.

Good luck.
Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Opening a Word mail merge document.

Posted 02 October 2015 - 06:37 AM

Quote

However, maybe there is a nice .NET Word/Office library that makes this task a little easier.


http://docx.codeplex.com/

Without having used it, I can't say if it's good or not. The documentation is nonexistent, at least on their codeplex site, but the source code does have examples.
Was This Post Helpful? 1
  • +
  • -

#9 jcborland   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 137
  • Joined: 16-December 13

Re: Opening a Word mail merge document.

Posted 02 October 2015 - 09:31 AM

Hi Everyone,

I've actually got a solution. I saved the Word document as a pdf document and I'm using iTextSharp with a little bit of C# code and I'm off and running.

Thanks all for your input.

I will probably take a look at docx.codeplex.com thanks Curtis.

Regards,
Jim.
Was This Post Helpful? 0
  • +
  • -

#10 jcborland   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 137
  • Joined: 16-December 13

Re: Opening a Word mail merge document.

Posted 02 October 2015 - 12:04 PM

I not sure what your missing.
I don't need to extract the numbers as a data source I already have the numbers in a data source. That data source was used by mail merge to create the original document.
I do need 10K individual documents with the reference number in the name.

Regards,
Jim.
Was This Post Helpful? 0
  • +
  • -

#11 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6818
  • View blog
  • Posts: 28,231
  • Joined: 12-December 12

Re: Opening a Word mail merge document.

Posted 02 October 2015 - 12:07 PM

Okay. Glad you got a solution.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1