【问题标题】:SaveAs2 method still prompts! Why?SaveAs2 方法还是提示!为什么?
【发布时间】:2015-11-01 09:58:25
【问题描述】:

上下文:

Vb.net 程序
Visual Studio 2010 终极版
MS Word 2010 自动化
Microsoft.Office.Interop.Word 库

我正在使用 saveAs2 方法来保存我正在创建的新文档,但是当我调用该方法时应用程序仍在提示我。为什么?

应用程序不可见。 application.displayAlerts 为假

各位有什么想法吗?

另外,当我手动完成 SaveUI 提示时,saveAs2 方法会引发异常。

这是我为要求它的人提供的代码:

    Public Sub generateModel() Implements ModelGenerator.generateModel

    wordApp.Visible = True
    wordApp.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone

    wordDoc = wordApp.Documents.Add

    wordDoc.PageSetup.TopMargin = wordApp.InchesToPoints(0.25)
    wordDoc.PageSetup.BottomMargin = wordApp.InchesToPoints(0.25)
    wordDoc.PageSetup.LeftMargin = wordApp.InchesToPoints(0.25)
    wordDoc.PageSetup.RightMargin = wordApp.InchesToPoints(0.25)

    With wordDoc.Content.Paragraphs.Add(wordDoc.Bookmarks.Item("\endofdoc").Range)

        .Range.Text = _text 
        .Format.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
        .Format.LineUnitBefore = 1
        .Range.Font.SmallCaps = True
        .Range.Font.Size = 12

    End With

    Dim logo = wordDoc.Shapes.AddPicture(logoLoc)
    logo.Height = wordApp.InchesToPoints(0.5)
    logo.Width = wordApp.InchesToPoints(1.18)

    Me.mainTable = wordDoc.Tables.Add(wordDoc.Bookmarks.Item("\endofdoc").Range, 3, 2)
    mainTable.Rows.HeightRule = Word.WdRowHeightRule.wdRowHeightExactly
    mainTable.Columns.Width = wordApp.InchesToPoints(4)
    mainTable.Rows.Height = wordApp.InchesToPoints(3.25)
    mainTable.Select()
    wordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft

    With wordDoc.Content.Paragraphs.Add(wordDoc.Bookmarks.Item("\endofdoc").Range)

        .Range.Text = "Rapport journalier de production - page 2"
        .Range.Font.Size = 10
        .Format.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
        .Format.LineUnitBefore = 0
        .Format.SpaceBeforeAuto = False
        .Format.SpaceBefore = 0

    End With

    wordDoc.SaveAs2("C:\Doc1.docx")

    wordDoc.Close(False)
    wordApp.Application.Quit()

End Sub

**更新:

我在另一台机器上测试了代码,它可以工作。所以我在我的身上尝试了这段代码:

Dim app As New Microsoft.Office.Interop.Word.Application
Dim doc = app.Documents.Add
doc.SaveAs2("C:\Users\simon\Documents\Doc3.docx")

它仍然会弹出 saveUI。我很困惑...

【问题讨论】:

  • 为什么不在 SaveAs2 调用中显示您的代码?
  • 我刚刚添加了代码。
  • 抛出哪种类型的异常?你也可以发一下吗?此外,您说:“应用程序不可见”->但是,您有wordApp.Visible = True,表明它应该是可见的?
  • 抛出的异常是一个普通的System.Runtime.InteropServices.COMException,上面写着“命令失败”。是的,在我给你的代码 sn-p 中,应用程序是可见的,但即使它不可见,我也尝试过。另外,应用程序是否可见并不重要。

标签: vb.net ms-word office-interop


【解决方案1】:

终于找到问题的根源了。

每次我安装 Word 时,我的 Acer 电脑都会安装一个插件。从 Word 中删除加载项后,一切恢复正常。

插件是 AcerCloud 插件。

【讨论】:

  • 在我的例子中,它是 Acer 的 abDocs Word Addin。
【解决方案2】:

我刚刚尝试在 C#、Word Interop API v.15 (Office 2013) 中创建示例实现:

var wordApplication = new Application() { Visible = true };
var doc = wordApplication.Documents.Add();
doc.SaveAs2(@"C:\my.docx");

...当您保存到需要管理员权限的位置(例如在 C:\ 驱动器的根目录上)时,Word Interop 会抛出 System.Runtime.InteropServices.COMException 说:“Word 无法保存或创建此文件。确保您要保存文件的磁盘未满、未写保护或损坏。"

相反,您应该将应用程序保存在具有必要写入权限的位置,例如,保存到您自己的用户目录 - 然后它应该可以按预期工作。

【讨论】:

  • 对不起,我应该说这不是我尝试的唯一路径。而且问题绝对不是抛出的异常,而是我调用方法时无缘无故弹出的saveUI。路径前的“@”是什么?
  • '@' 在 C# 中用作逐字字符串的前缀。 'SaveAs' 方法怎么样,你试过吗?
  • 是的,我试过了。我很确定问题出在我安装的 ms word 上
猜你喜欢
  • 2023-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-24
  • 2023-02-22
  • 2011-05-14
  • 1970-01-01
  • 2020-08-31
相关资源
最近更新 更多