none
Using SaveAs to overwrite existing file in wdFormatDocument format RRS feed

  • Question

  • vs2010 & Word 2010

    Desire is to save document in RTF format with filename of "AD1u2r15.doa",
    close document, re-open it, and save in wdFormatDocument over-writing
    the "AD1u2r15.doa" file.

    Step 7 presents a "Save As" dialog with "AD1u2r15.doa.doc" as filename.
    How can the "Save As" dialog be prevented?
    How can the filename be forced to save without the ".doc" extension?

    1. SmartFName = "AD1u2r15.doa"
    2. Dim blnConfirmConversions As Boolean = doc.Application.Options.ConfirmConversions
    3. doc.Application.Options.ConfirmConversions = False
    4. doc.SaveAs(FileName:=SmartFName, FileFormat:=Word.WdSaveFormat.wdFormatRTF,
          AddToRecentFiles:=False)
    5. doc.Close(SaveChanges:=False)
    6. doc = Globals.ThisAddIn.Application.Documents.Open(FileName:=SmartFName,
          AddToRecentFiles:=False)
    7. doc.SaveAs(FileName:=SmartFName, 
          FileFormat:=Word.WdSaveFormat.wdFormatDocument,
          AddToRecentFiles:=False)
    8. doc.Application.Options.ConfirmConversions = blnConfirmConversions

    Thanks in Advance.


    Tim

    Tuesday, April 17, 2012 10:09 PM

Answers

  • Tom & Chris,

    Thanks for your replies.

    I am duplicating functionality used with Word 2003 and did not realize
    that built-in extensions would affect SaveAs.

    I changed my approach to use built-in extensions.

    1. To avoid the SaveAs converion dialog:
       a. Added blnConfirmConversions to contain state of ConfirmConversions
       b. Set App.Options.ConfirmConversions = False
       c. Performed .SaveAs using wdFormatRTF
       d. Returned App.Options.ConfirmConversions to blnConfirmConversions

    Note SaveAs using wdFormatDocument does not present SaveAs conversion dialog.

    2. If SmartFName exists, appended an 'x' to the prefix.
    3. To delete hidden files that Word seems to leave:
       a. Added check for the file where 1st 2 chars replaced with '~$'
       b. If file found, deleted it

    Thanks for your help.

    Friend Function RTFDoc(ByRef doc As Word.Document) As Boolean
      Dim App As Word.Application = doc.Application
      Dim SmartFName As String = Lib1.GetSmartFName(doc:=doc)
      'SmartFName returns a filename like "AD1u2r15.doc"
      Dim RTF_FName = Path.GetFileNameWithoutExtension(SmartFName) & ".rtf"
      Dim blnConfirmConversions As Boolean = App.Options.ConfirmConversions
      App.Options.ConfirmConversions = False  'Avoid dialog confirming conversion
      doc.SaveAs(FileName:=RTF_FName, FileFormat:=Word.WdSaveFormat.wdFormatRTF,
        AddToRecentFiles:=False)
      App.Options.ConfirmConversions = blnConfirmConversions
      doc.Close(SaveChanges:=False)
      doc = App.Documents.Open(FileName:=RTF_FName, AddToRecentFiles:=False, 
        ConfirmConversions:=False)
      If System.IO.File.Exists(SmartFName) Then _
        SmartFName = Path.GetFileNameWithoutExtension(SmartFName) & "x.doc"
      doc.SaveAs(FileName:=SmartFName, FileFormat:=Word.WdSaveFormat.wdFormatDocument,
        AddToRecentFiles:=False)
      System.IO.File.Delete(RTF_FName)
      Dim HiddenFName As String = "~$" & RTF_FName.Substring(2)
      If System.IO.File.Exists(HiddenFName) Then _
        System.IO.File.Delete(HiddenFName)
      doc.Close(SaveChanges:=True, OriginalFormat:=Word.WdSaveFormat.wdFormatDocument)
      doc = App.Documents.Open(FileName:=SmartFName, AddToRecentFiles:=True,
        ConfirmConversions:=False)
      App.ScreenRefresh()
      App = Nothing
      Return True
    End Function


    Tim

    Tuesday, April 24, 2012 3:20 PM

All replies

  • Hi Tim,

    Thanks for posting in the MSDN Forum.

    I never see the "doa" extension in Word application. Would you please clarify it?

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, April 18, 2012 2:40 AM
    Moderator
  • Tom,

    The "doa" extension is used as an indicator of a temporary file.

    Thanks


    Tim

    Wednesday, April 18, 2012 12:47 PM
  • Hi Tim,

    I think you missed my opinion, I want to say that "doa" isn't a build-in extension of MSWord. MSWord will never create or save file to right file which with doa extension.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, April 19, 2012 5:51 AM
    Moderator
  • Hi Tim,

    Tom is correct - but you can modify the situation by associating the extension "doa" with Microsoft Word. Here are links to a long list of content where file extensions and their modification is discussed.
    Registering an extension
    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/4d8760c9-b2c9-4016-b75e-1b3189b0c30b/

    SOX971030700010 VB5: What do all of those file extensions mean?
    https://vkbexternal.partners.extranet.microsoft.com/VKBWebService/ViewContent.aspx?scid=SO;US;SOX971030700010

    SOX060721700021 File Extensions for all Office apps
    https://vkbexternal.partners.extranet.microsoft.com/VKBWebService/ViewContent.aspx?scid=SO;US;SOX060721700021

    New file extensions for the document types you know - Office Open ...
    http://office.microsoft.com/en-us/word-help/office-open-xml-i-exploring-the-office-open-xml-formats-RZ010243529.aspx?section=4

    How to: Add and Delete Document Types and Associated File ...
    http://msdn.microsoft.com/en-us/library/4fcx9b75.aspx

    Examine File Extensions and File Associations in Windows 7
    http://technet.microsoft.com/en-us/magazine/ff687021.aspx

    Specifying File Handlers for File Extensions
    http://msdn.microsoft.com/en-US/library/bb166549(v=VS.80).aspx

    Regards.
    Chris Jensen


    Chris Jensen

    Monday, April 23, 2012 4:09 PM
    Moderator
  • Tom & Chris,

    Thanks for your replies.

    I am duplicating functionality used with Word 2003 and did not realize
    that built-in extensions would affect SaveAs.

    I changed my approach to use built-in extensions.

    1. To avoid the SaveAs converion dialog:
       a. Added blnConfirmConversions to contain state of ConfirmConversions
       b. Set App.Options.ConfirmConversions = False
       c. Performed .SaveAs using wdFormatRTF
       d. Returned App.Options.ConfirmConversions to blnConfirmConversions

    Note SaveAs using wdFormatDocument does not present SaveAs conversion dialog.

    2. If SmartFName exists, appended an 'x' to the prefix.
    3. To delete hidden files that Word seems to leave:
       a. Added check for the file where 1st 2 chars replaced with '~$'
       b. If file found, deleted it

    Thanks for your help.

    Friend Function RTFDoc(ByRef doc As Word.Document) As Boolean
      Dim App As Word.Application = doc.Application
      Dim SmartFName As String = Lib1.GetSmartFName(doc:=doc)
      'SmartFName returns a filename like "AD1u2r15.doc"
      Dim RTF_FName = Path.GetFileNameWithoutExtension(SmartFName) & ".rtf"
      Dim blnConfirmConversions As Boolean = App.Options.ConfirmConversions
      App.Options.ConfirmConversions = False  'Avoid dialog confirming conversion
      doc.SaveAs(FileName:=RTF_FName, FileFormat:=Word.WdSaveFormat.wdFormatRTF,
        AddToRecentFiles:=False)
      App.Options.ConfirmConversions = blnConfirmConversions
      doc.Close(SaveChanges:=False)
      doc = App.Documents.Open(FileName:=RTF_FName, AddToRecentFiles:=False, 
        ConfirmConversions:=False)
      If System.IO.File.Exists(SmartFName) Then _
        SmartFName = Path.GetFileNameWithoutExtension(SmartFName) & "x.doc"
      doc.SaveAs(FileName:=SmartFName, FileFormat:=Word.WdSaveFormat.wdFormatDocument,
        AddToRecentFiles:=False)
      System.IO.File.Delete(RTF_FName)
      Dim HiddenFName As String = "~$" & RTF_FName.Substring(2)
      If System.IO.File.Exists(HiddenFName) Then _
        System.IO.File.Delete(HiddenFName)
      doc.Close(SaveChanges:=True, OriginalFormat:=Word.WdSaveFormat.wdFormatDocument)
      doc = App.Documents.Open(FileName:=SmartFName, AddToRecentFiles:=True,
        ConfirmConversions:=False)
      App.ScreenRefresh()
      App = Nothing
      Return True
    End Function


    Tim

    Tuesday, April 24, 2012 3:20 PM