4
votes

Thanks in advance for any help you can give me.

I'm 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 the changes to the document is easy. Saving the document seems like it should be just as easy but there is one serious problem. When I try to save the document, the save as dialog opens. This is supposed to be automated so that doesn't work. I have tried a whole bunch of variations of:

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

It always stops here:

oDoc.SaveAs2

and opens the save as dialog. I need to find a way to suppress the save as dialog or a new way of editing the word files. Everything I have found so far about the problem has either not been solved/updated or was related to Word addins. I don't have any of the addins that people said caused the problem. To be safe, I disabled all word addins.

I would appreciate it if anyone has either solved it or has a different approach. I'm not stuck on the idea of using VB. The only reason I'm going this route is because I think it gives me the best library for editing charts and formatting the documents.

Thanks, Steve

2
Is the document local or on a network path? Are you sure it's not in use before you try saving to it? - Saragis
In excel you set application.displayalerts = false and specify a conflictResolution value in the saveas prompt. I would suspect something similar for word - Greg
@Saragis The file is local. It's not in use. The new file name is a random UUID, so I'm sure it doesn't exist already. - stevepra
@Greg I am setting DisplayAlerts = False. I have tried setting other properties to false as well; SaveNormalPrompt = False and SavePropertiesPrompt = False. There is no conflictResolution parameter. I have tried putting in values for all of the optional parameters of SaveAs and SaveAs2 - stevepra

2 Answers

2
votes

I found the problem. The answer lies in what I was doing in the part I omitted where I was making changes to the document. Part of those changes are to update data in the charts in the document. I had something like:

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

I changed it to:

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

Now it works

0
votes
myDoc.SaveAs(fileNameAndPath)

Works for me in Word 2007