C#在Office 2016上互操作word、剪切和过去的工作,但在Office 2019上不互操作
我发现了类似的问题,但并不完全相同 我有一个word模板,由用户输入的文本填充 在用户界面上,有一个文本字段和两个签名字段(生成图像文件的第三方组件) 如果文本不是很长,它将传递两个版本的word。但是,如果文本很长并且有一些输入,那么它在Office 2019和Office 365上不起作用。在Office 2016上,它始终有效 为了更好地解释 我打开文件:C#在Office 2016上互操作word、剪切和过去的工作,但在Office 2019上不互操作,c#,asp.net-mvc-5,office-interop,C#,Asp.net Mvc 5,Office Interop,我发现了类似的问题,但并不完全相同 我有一个word模板,由用户输入的文本填充 在用户界面上,有一个文本字段和两个签名字段(生成图像文件的第三方组件) 如果文本不是很长,它将传递两个版本的word。但是,如果文本很长并且有一些输入,那么它在Office 2019和Office 365上不起作用。在Office 2016上,它始终有效 为了更好地解释 我打开文件: Microsoft.Office.Interop.Word.Application app = null;
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);
在类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();
}
我的Office版本在任何情况下都有效:
以及服务器(Windows server 2016)的offic版本,该版本在大文本情况下不起作用:
提前感谢。剪切方法可能会导致与剪贴板访问相关的安全问题 试一试 和评论
//finalInlineShape.Range.Cut();
我正在尝试你的解决方案。它有效,谢谢!
//finalInlineShape.Range.Cut();