views:

36

answers:

1

Hi all,

I'm programming a Microsoft Word plugin in VB.Net, and I have a code that calls a function to save the document as PDF (Office 2007).

I use two functions:

Public Sub SaveLandscape_CallBack(ByVal Control As Office.IRibbonControl)
    SaveEbook(True)
End Sub

And

Public Sub SaveEbook(ByVal ForceLandscape As Boolean)
    Try
        Dim FilePath As String = Globals.ThisAddIn.Application.ActiveDocument.Path & "\" & Globals.ThisAddIn.Application.ActiveDocument.Name & ".ebook.pdf"
        Try
            Globals.ThisAddIn.Application.ActiveDocument.ExportAsFixedFormat(OutputFileName:=FilePath, ExportFormat:=Word.WdExportFormat.wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:=Word.WdExportOptimizeFor.wdExportOptimizeForPrint, Range:=Word.WdExportRange.wdExportAllDocument, Item:=Word.WdExportItem.wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, CreateBookmarks:=Word.WdExportCreateBookmarks.wdExportCreateHeadingBookmarks, DocStructureTags:=True, BitmapMissingFonts:=True, UseISO19005_1:=True)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    Catch Ex As Exception
        MsgBox(Ex.ToString)
    End Try

I've trimmed the second one a lot, which is why there is this unused parameter.
The first function is associated to a ribbon button. Every time I run it, ExportAsFixedFormat returns an error saying the file is in use, whatever name I use.

Any idea?

Thanks,
CFP.

EDIT: I've identified the problem to be exactly the same as http://social.msdn.microsoft.com/Forums/en-US/worddev/thread/95c5b101-0d98-49f8-a92b-7b444e61cca8/ . Any ideas anyone?

+1  A: 

Try putting Globals.ThisAddIn.Application.ActiveDocument.Saved = True before the Globals... line.

Otaku
Nope, it doesn't work. Actually, the complete code does check if the document is saved before writing to the pdf file.
CFP
@CFP - I've replicated your above code in VBA and it works without a problem. The only thing I can now think of is what Service Pack you may be on - are you on SP2 or an earlier SP or not at all?
Otaku
So have I. I'm on windows 7, no SP =) Did you see my new link? The problem seems to be related to the ISO_... attribute, and it doesn't happen all the time...
CFP
@CFP - What SP of Office 2007? I tried out your code via VBA on Win Vista + Office 2007 (SP2) and it worked without hitch. I did it both with and without Acrobat Reader installed. This is leading me to think that you may not have SP2 on Office. Just trying to determine the environment as that may be telling us something we're overlooking.
Otaku
Windows 7 + Office 2007 SP2, sadly. I really don't get it... Did you see http://social.msdn.microsoft.com/Forums/en-US/worddev/thread/95c5b101-0d98-49f8-a92b-7b444e61cca8/ ? (thanks for your help, by the way =))
CFP
@CFP: I did see that thread. Same thing though - the issue can't be replicated. This is why it's leading me to believe there is an environmental configuration issue. Can you reproduce under the following tasks **1)** Calling the `SaveEbook` routine from somewhere other than the Ribbon (like during startup of your add-in)?, **2)** Using VBA?, **3)** On a different machine with the same environment?, **4)** By setting `ISO19005_1:=False`? Just trying different methods as I can't reproduce the issue on my side.
Otaku
Hi! 1) Yes. 2) Yes. 3) Nope, sadly. 4) No; the problem seems to be specifically related to setting this to true. Thanks!
CFP
Okay, one more thing to try and then after that, I'm not sure as I can't replicate it. Let's move your code from calling `ActiveDocument` several times to just once. Go with `Dim d As Document = Globals.ThisAddIn.Application.ActiveDocument` and then after that make calls using just `d` for your `FilePath` construction and `ExportAsFixedFormat`.
Otaku