When using Word.MailingLabel Word cannot complete the save due to a file permission error

Henk Wisselink 1 Reputation point
2020-12-09T15:32:16.427+00:00

When creating MailingLabel documents in Word (in my case version 2010) I can no longer save changes to the original Word document.
The following code demonstrates the issue:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim wordApp As Word.Application
        Dim wordDoc As Word.Document
        Dim wordLabel As Word.Document

        Try
            'Start Word:
            wordApp = New Word.Application
            'Add and save a document with 'Any text':
            wordDoc = wordApp.Documents.Add
            wordDoc.Range.Text = "Any text"
            wordDoc.SaveAs("C:\Users\User\AppData\Local\Temp\Worddocument.docx")

            'Create a mailinglabel document based on an existing 'label':
            wordLabel = wordApp.MailingLabel.CreateNewDocument("label", "", "", False, 0, False, False)
            'Code for printing the label goes here......
            wordLabel.Close(False)

            'Change, save, close and re-open the document:
            wordDoc.Range.Text = "Another text"
            wordDoc.Save()  'THIS FIRST SAVE WORKS CORRECTLY
            wordDoc.Close(False)
            wordDoc = wordApp.Documents.Open("C:\Users\User\AppData\Local\Temp\Worddocument.docx")

            'Again create a mailinglabel document based on an existing 'label':
            wordLabel = wordApp.MailingLabel.CreateNewDocument("label", "", "", False, 0, False, False)
            'Code for printing the label goes here......
            wordLabel.Close(False)

            'Change and save the document:
            wordDoc.Range.Text = "Again another text"
            wordDoc.Save()  'THIS SECOND SAVE GENERATES THE ERROR 'Word cannot complete the save due to a file permission error'

        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            wordLabel.Close(False)
            wordDoc.Close(False)
            wordApp.Quit(False)
        End Try
    End Sub

After generating the first labeldocument the wordDoc is changed, saved, closed and re-opend. This save works correctly.
Then a labeldocument is generated a second time. And the following document change and subsequent save will generate the error.
It lookes like wordLabel is changing something in wordDoc that prevents it from proper saving, but I cannot find any reason for that. I do close the wordLabel but that does not help either.
Two things are essential for this error to occur:

  1. the in between closing and re-opening of wordDoc;
  2. the renewed creation of wordLabel.
    If one of them is omitted the save will work correctly.

Can anyone think of a reason and how this can be avoided?

Windows for business | Windows Client for IT Pros | Devices and deployment | Configure application groups
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2020-12-10T08:00:26.073+00:00

    Hello,

    Thank you so much for posting here.

    As for our issue, it is suggested that we could turn to the developer forum for professional assistance. Below is the link:

    https://developer.microsoft.com/en-us/office/docs#officecommunity

    Hope we could get the answer there. Thank you so much for your understanding and support.

    Best regards,
    Hannah Xiong

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.