VB.Net saves a document without a hint

Thanks in advance for any help you can give me.

I am trying to create a VB application that will open an existing Word document, make some changes and save it with a new file name. Making changes to a document is easy. Saving a document seems to be just as easy, but there is one serious problem. When I try to save the document, a save dialog will open. It needs to be automated, so it won’t work. I tried a bunch of options:

Sub Main()
    Dim oWord As Word.Application
    Dim oDoc As Word.Document
    Dim fileName As String
    Dim templateName As String
    Dim newFileName As String

    'Start Word and open the document template.
    oWord = CreateObject("Word.Application")
    oWord.Visible = False
    oWord.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone

    templateName = "C:\tmp\template.dotx"
    fileName = "C:\tmp\document.docx"
    newFileName = "C:\tmp\new document.docx"

    oDoc = oWord.Documents.Add(templateName)
    'oDoc = oWord.Documents.Open(fileName)  I have tried both using a template and opening a docx file.

    <make changes>

    oDoc.SaveAs2(newFileName)

    oWord.Documents.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
    oWord.Application.Quit()
    oWord = Nothing
End Sub

He always stops here:

oDoc.SaveAs2

. . , , / , Word. , , , . , .

, - . VB. , , - , , .

,

+4
2

. , , , . . - :

For Each oShape As Word.InlineShape In oDoc.InlineShapes
    If oShape.HasChart And oShape.Range.Bookmarks.Item(1).Name = "ChartName" Then
        Console.WriteLine("Updating ChartName")
        Dim oWorkbook As Excel.Workbook
        oWorkbook = oShape.Chart.ChartData.Workbook

        oWorkbook.Worksheets(1).Range("B2").FormulaR1C1 = "9"
        oWorkbook.Worksheets(1).Range("B3").FormulaR1C1 = "5"
        oWorkbook.Worksheets(1).Range("B4").FormulaR1C1 = "1"

        oWorkbook.Application.Quit()

    End If
Next

:

For Each oShape As Word.InlineShape In oDoc.InlineShapes
    If oShape.HasChart And oShape.Range.Bookmarks.Item(1).Name = "ChartName" Then
        Console.WriteLine("Updating ChartName")
        Dim oWorkbook As Excel.Workbook
        oWorkbook = oShape.Chart.ChartData.Workbook

        oWorkbook.Worksheets(1).Range("B2").FormulaR1C1 = "9"
        oWorkbook.Worksheets(1).Range("B3").FormulaR1C1 = "5"
        oWorkbook.Worksheets(1).Range("B4").FormulaR1C1 = "1"

        oShape.Chart.Refresh()
        oWorkbook.Close(True)

    End If
Next

+2
myDoc.SaveAs(fileNameAndPath)

Word 2007

0

Source: https://habr.com/ru/post/1598774/


All Articles