I found similar questions but not exactly the same.
I have a word template which I fill by texts entered by users.
On the user interface, there is a text field and two signature fields (a 3rd party component which produces an image file).
If the text is not very long, it passes on two versions of word. But if the text is long and there is some enters, it doesn't work on Office 2019 and Office 365. On Office 2016, it works always.
To better explain,
I open the document :
Microsoft.Office.Interop.Word.Application app = null;
Microsoft.Office.Interop.Word.Document doc = null;
...
app = new Microsoft.Office.Interop.Word.Application();
doc = app.Documents.Open(tempPath);
app.Visible = false;
doc.Bookmarks["comment"].Select();
app.Selection.TypeText(orderComment); //Order comment is typed by the user
...
//this code saves the signature as a png image and it works in any case. The image exists in the folder before calling the rest of the code.
string clientSignaturePath = System.Configuration.ConfigurationManager.AppSettings["TempPath"] + Guid.NewGuid().ToString().Substring(0, 6) + ".png";
using (FileStream fs = new FileStream(clientSignaturePath, FileMode.Create))
{
using (BinaryWriter bw = new BinaryWriter(fs))
{
byte[] data = Convert.FromBase64String(model.ClientSignature);
bw.Write(data);
bw.Close();
}
fs.Close();
}
//If the orderComment is too long, it gives this error in this method when I call the line rng.Paste(); on Office 2019 and 365 but not on 2016.
error : this method or property is not available because the clipboard is empty or invalid
UserMethods.InsertImage(doc, clientSignaturePath, "client", 79, 175);
In the class UserMethods:
public static void InsertImage(Microsoft.Office.Interop.Word.Document doc, string imagePath, string type, float? imageHeight = null, float? imageWidth = null)
{
Range rng = null;
if (type == "tech")
rng = doc.Tables[7].Cell(1, 1).Range;
else if (type == "client")
rng = doc.Tables[7].Cell(1, 2).Range;
else
rng = doc.Tables[7].Cell(1, 3).Range;
Microsoft.Office.Interop.Word.InlineShape autoScaledInlineShape = rng.InlineShapes.AddPicture(imagePath);
float scaledWidth = imageWidth ?? autoScaledInlineShape.Width;
float scaledHeight = imageHeight ?? autoScaledInlineShape.Height;
autoScaledInlineShape.Delete();
// Create a new Shape and fill it with the picture
Microsoft.Office.Interop.Word.Shape newShape = doc.Shapes.AddShape(1, 0, 0, scaledWidth, scaledHeight);
newShape.Fill.UserPicture(imagePath);
// Convert the Shape to an InlineShape and optional disable Border
Microsoft.Office.Interop.Word.InlineShape finalInlineShape = newShape.ConvertToInlineShape();
//finalInlineShape.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;
// Cut the range of the InlineShape to clipboard
finalInlineShape.Range.Cut();
// And paste it to the target Range
rng.Paste();
}
My Version of Office which works in any case:
And the server's (Windows Server 2016) offic version which doesn't work in case of large text :
Thanks in advance.
The Cut method may cause security issues related to Clipboard access
try
rng.FormattedText = finalInlineShape.Range.FormattedText;
finalInlineShape.Delete();
and comment;
//finalInlineShape.Range.Cut();