C# interop word、cut 和 past 在 Office 2016 上有效,但在 Office 2019 上无效
C# interop word, cut and past works on Office 2016 but not on Office 2019
我发现了类似的问题,但不完全相同。
我有一个 word 模板,我用用户输入的文本填充。
在用户界面上,有一个文本字段和两个签名字段(生成图像文件的第 3 方组件)。
如果文字不是很长,就传两个版本的word。但是如果文本很长,并且有一些输入,它在 Office 2019 和 Office 365 上不起作用。在 Office 2016 上,它始终有效。
为了更好地解释,
我打开文档:
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);
在 class 用户方法中:
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) 官方版本在大文本的情况下不起作用:
提前致谢。
Cut 方法可能会导致与剪贴板访问相关的安全问题
尝试
rng.FormattedText = finalInlineShape.Range.FormattedText;
finalInlineShape.Delete();
并发表评论;
//finalInlineShape.Range.Cut();
我发现了类似的问题,但不完全相同。
我有一个 word 模板,我用用户输入的文本填充。
在用户界面上,有一个文本字段和两个签名字段(生成图像文件的第 3 方组件)。
如果文字不是很长,就传两个版本的word。但是如果文本很长,并且有一些输入,它在 Office 2019 和 Office 365 上不起作用。在 Office 2016 上,它始终有效。
为了更好地解释,
我打开文档:
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);
在 class 用户方法中:
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) 官方版本在大文本的情况下不起作用:
提前致谢。
Cut 方法可能会导致与剪贴板访问相关的安全问题
尝试
rng.FormattedText = finalInlineShape.Range.FormattedText;
finalInlineShape.Delete();
并发表评论;
//finalInlineShape.Range.Cut();