Process Mapping Logo

Process Mapping - Forums

Sharing 19 years of knowledge and experience

 
Metastorm BPM forums
Sign up Latest Topics
 
 
 


Reply
  Author   Comment  
RichG

Member
Registered:
Posts: 23
Reply with quote  #1 

I'm currently using a C# script to generate a word document and populate it with some data. I have a visual script for each record loop that needs to create a new page in the same document and add more data to it. I know very little about C# so wondered if someone could give me some advice.

 

With my existing code it runs through the loop and just overwrites the data with the last record in the loop.

 

Any help would be much appreciated.

 

This is my current code :-

 

Quote:

#region

Using Statement

 

using

System;

using

System.Data;

using

System.Collections.Generic;

using

Metastorm.Ide.Extensibility;

using

Metastorm.Runtime.Core;

using

Metastorm.Runtime.Types;

using

System.ComponentModel;

 

using

System.IO;

using

DocumentFormat.OpenXml.Packaging;

 

#endregion

Using Statement

 

//Change this to a namespace that will suit you

namespace

Metastorm.Runtime.Models.Staff_Parking_Pemits

{

 

///<summary>

 

/// This needs to be a class or an interface that will support public, static methods/properties

 

///</summary>

 

publicclass WordExample

{

 

// To search and replace content in a Word document.

 

publicstaticvoid SearchAndReplace(string wordTemplatePath, string wordOutputPath, string findString, string replaceString, string findString2, string replaceString2, string findString3, string replaceString3, string findString4, string replaceString4, string findString5, string replaceString5, string findString6, string replaceString6, string findString7, string replaceString7, string findString8, string replaceString8, string findString9, string replaceString9, string findString10, string replaceString10, string findString11, string replaceString11, string findString12, string replaceString12)

 

{

 

 

//create instance of our template in the output location

File.Copy(wordTemplatePath, wordOutputPath,

true); // overwrite = true

 

using (WordprocessingDocument wordOutputDoc = WordprocessingDocument.Open(wordOutputPath, true))

{

 

string docText = null;

 

using (StreamReader sr = new StreamReader(wordOutputDoc.MainDocumentPart.GetStream()))

{

docText = sr.ReadToEnd();

}

docText = docText.Replace(findString, replaceString);

docText = docText.Replace(findString2, replaceString2);

docText = docText.Replace(findString3, replaceString3);

docText = docText.Replace(findString4, replaceString4);

docText = docText.Replace(findString5, replaceString5);

docText = docText.Replace(findString6, replaceString6);

docText = docText.Replace(findString7, replaceString7);

docText = docText.Replace(findString8, replaceString8);

docText = docText.Replace(findString9, replaceString9);

docText = docText.Replace(findString10, replaceString10);

docText = docText.Replace(findString11, replaceString11);

docText = docText.Replace(findString12, replaceString12);

 

 

using (StreamWriter sw = new StreamWriter(wordOutputDoc.MainDocumentPart.GetStream(FileMode.Create)))

{

sw.Write(docText);

}

}

}

 

 

 

}

}

0
RichG

Member
Registered:
Posts: 23
Reply with quote  #2 
I've tried changing the StreamWriter to

Quote:

using

 

 

(StreamWriter sw = new StreamWriter(wordOutputDoc.MainDocumentPart.GetStream(FileMode.Append,FileAccess.Write)))


but get the following error :-

Failed to execute deployed method 'WhenActionCompleted', using entity 'EnterPermitParametersO'. FileMode value is not supported.

 

0
Jerome

Avatar / Picture

Guru
Registered:
Posts: 5,507
Reply with quote  #3 
I'd have to see the code calling this function
__________________
Post an example, and we will have a much better idea what the problem is. In about 90% of posts, the problem is one of communication. Examples bridge that gap.
0
RichG

Member
Registered:
Posts: 23
Reply with quote  #4 

Hi Jerome,

The code I'm using is just the standard script Metastorm provide. Here is the code requested:

Quote:
WordExample.SearchAndReplace(@""+GeneratePermitData.MailMergePath+"", ""+GeneratePermitData.Filename+"", "[PERMITTYPEFULL]", ""+GeneratePermitData.PermitTypeFull+"", "[CARPARK]", ""+OfficersPermitRun1.CarPark+"", "[DATEFORMAT]", ""+GeneratePermitData.FormattedDateofExpiry+"", "[PERMITTYPE]", ""+OfficersPermitRun1.PermitType+"", "[OFFICELOCATION]", ""+OfficersPermitRun1.OfficeLocation+"", "[FIRSTNAME]", ""+OfficersPermitRun1.FirstName+"", "[SURNAME]", ""+OfficersPermitRun1.Surname+"", "[PERMITTYPE1]", ""+OfficersPermitRun1.PermitType+"", "[DATEFORMAT1]", ""+GeneratePermitData.FormattedDateofExpiry+"", "[CARPARK1]", ""+OfficersPermitRun1.CarPark+"", "[OFFICELOCATION1]", ""+OfficersPermitRun1.OfficeLocation+"", "[PERMITNUMBER]", ""+GeneratePermitData.PermitStartNumber+"");


Thanks

Rich

0
Jerome

Avatar / Picture

Guru
Registered:
Posts: 5,507
Reply with quote  #5 
Where do they provide this?

But what is important is how are you using it in a loop? If the only one that makes it is the last one, in my experience that is an issue with the loop.

__________________
Post an example, and we will have a much better idea what the problem is. In about 90% of posts, the problem is one of communication. Examples bridge that gap.
0
RichG

Member
Registered:
Posts: 23
Reply with quote  #6 
Quote:
Where do they provide this?


http://communitycentral.metastorm.com/t5/BPM-Version-9-Developer-Central/Microsoft-Word-Mail-Merge-Functionality-in-Metastorm-BPM-9-x/ta-p/40695

Quote:
But what is important is how are you using it in a loop? If the only one that makes it is the last one, in my experience that is an issue with the loop.


This is the visual script loop and I believe it works correctly:

Attached Images
jpeg loop.jpg (106.62 KB, 8 views)

0
Jerome

Avatar / Picture

Guru
Registered:
Posts: 5,507
Reply with quote  #7 
and that code calling the server function is in the 'Mail Merger' code activity?

What Business Object is selected for the foreach activity?

__________________
Post an example, and we will have a much better idea what the problem is. In about 90% of posts, the problem is one of communication. Examples bridge that gap.
0
RichG

Member
Registered:
Posts: 23
Reply with quote  #8 

Quote:
and that code calling the server function is in the 'Mail Merger' code activity?


Yes thats Correct.

My business object :

Quote:
SELECT StaffPermitsUsers.*
FROM StaffPermitsUsers
WHERE StaffPermitsUsers.PermitType IN ('O', 'OM')

0
Jerome

Avatar / Picture

Guru
Registered:
Posts: 5,507
Reply with quote  #9 
And the name of the business object? I have to assume it's not the one used to pass data to the function, as that has a process name feel to it, so why not?
__________________
Post an example, and we will have a much better idea what the problem is. In about 90% of posts, the problem is one of communication. Examples bridge that gap.
0
RichG

Member
Registered:
Posts: 23
Reply with quote  #10 
The business object name is OfficersPermitRun1

The Process Map is GeneratePermitData
0
Jerome

Avatar / Picture

Guru
Registered:
Posts: 5,507
Reply with quote  #11 
Ok, I see the BO, now. Use a quote box on your post, the code box is a bit awful to see.
__________________
Post an example, and we will have a much better idea what the problem is. In about 90% of posts, the problem is one of communication. Examples bridge that gap.
0
RichG

Member
Registered:
Posts: 23
Reply with quote  #12 
Now in quotes :)
0
Jerome

Avatar / Picture

Guru
Registered:
Posts: 5,507
Reply with quote  #13 
Are you just overwriting the file each time? I would use a unique id in the BO, or just an incrementing number.

I'd also do all of this in code within the for each loop

__________________
Post an example, and we will have a much better idea what the problem is. In about 90% of posts, the problem is one of communication. Examples bridge that gap.
0
RichG

Member
Registered:
Posts: 23
Reply with quote  #14 
Quote:
Are you just overwriting the file each time? I would use a unique id in the BO, or just an incrementing number.

I'd also do all of this in code within the for each loop

Every time the loop goes round I want it to add on to the end of the same file.

I see where your coming from, when the business object query is executed it retrieves 276 records at once. I will try to run through each record at a time and report how I get on.

Thanks for looking at this.

Rich

0
Previous Topic | Next Topic
Print
Reply

Quick Navigation:


Create your own forum with Website Toolbox!