none
How do I make Word write a new file to the current directory?

    Question

  • I'm writing a Word 2007 macro that saves the active .docx file as .html. But it's saving the file in the My Documents folder. I want it to save it in the SAME folder as the .docx without having to hardcode this path in the macro. (Different people will be using the macro and will be using different directory structures.

    Also, when I open another file after the follwoing section of code, it's defaulting to My Documents. I do not want the current directory changed.

    Here's my code for the save-as-filtered-html chunk:

    ' SaveAsHtmlFile()
       'Dim CurrentDirectory As String
       'CurrentDirectory = Options.DefaultFilePath(wdCurrentFolderPath)
       
       Dim strDocName As String
       Dim intPos As Integer
      
      'Find position of extension in file name
       strDocName = ActiveDocument.Name
       intPos = InStrRev(strDocName, ".")
    
       If intPos = 0 Then
    
        'If the document has not yet been saved
        'Ask the user to provide a file name
        strDocName = InputBox("Please enter the name " & _
          "of your document.")
       Else
    
        'Strip off extension and add ".html" extension
        strDocName = Left(strDocName, intPos - 1)
        strDocName = strDocName & ".html"
       End If
    
      'Save file with new extension
       ActiveDocument.SaveAs FileName:=strDocName, _
        FileFormat:= _
        wdFormatFilteredHTML, LockComments:=False, Password:="", AddToRecentFiles _
        :=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts _
        :=False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
        SaveAsAOCELetter:=False
      'End SaveAsHtmlFile
    
    
    Friday, November 12, 2010 5:52 PM

Answers

  • Hi TechPubber

    You can get the file path of the ActiveDocument using:

    ActiveDocument.Path

    So, something like ActiveDocument.SaveAs FileName:=ActiveDocument.Path & "\" & strDocName


    Cindy Meister, VSTO/Word MVP
    Friday, November 12, 2010 6:47 PM
    Moderator

All replies

  • Hi TechPubber

    You can get the file path of the ActiveDocument using:

    ActiveDocument.Path

    So, something like ActiveDocument.SaveAs FileName:=ActiveDocument.Path & "\" & strDocName


    Cindy Meister, VSTO/Word MVP
    Friday, November 12, 2010 6:47 PM
    Moderator
  • Thanks, Cindy, that works. I've discovered another problem with this segment of code:

     'Save file with new extension
      ActiveDocument.SaveAs FileName:=ActiveDocument.Path & "\" & strDocName, _
      FileFormat:=wdFormatFilteredHTML
     'End SaveAsHtmlFile
    

    It seems that the previous segment of the macro is specifying Filtered HTML file format, but there is a lot of Word code in the resulting HTML file (for example, <span class=MsoHyperlink>"), and when I open it in IE, it gives me a security warning.  When I compare it to a file I have manually saved as Filtered HTML, the manually saved one is a lot cleaner.  When I manually save as Filtered HTML, a dialog pops up:

    "Saving <filename>.docx in this format (Web Page, Filtered) will remove Office-specific tags. Some Office features may not be available when you reopen this page. Do you want to save the document in this format? To save, click Yes."  I click Yes, and the file is saved cleanly.

    Is there a way to force Word to save it in this format, discarding all Office-specific features? Thanks for your help.

     

     

    Friday, November 12, 2010 7:36 PM
  • You could also do it by saving the document first (and also avoid the input box) and then use the fullname parameter e.g.

       Dim strDocName As String
       Dim intPos As Integer
       ActiveDocument.Save ' save the document so that it will always have a name
      'Find position of extension in full file path
       strDocName = ActiveDocument.FullName
       intPos = InStrRev(strDocName, ".")

        'Strip off extension and add ".html" extension
        strDocName = Left(strDocName, intPos - 1)
        strDocName = strDocName & ".html"

      'Save file with new extension
       ActiveDocument.SaveAs FileName:=strDocName, _
        FileFormat:= wdFormatFilteredHTML

    As for your other question re html, Word has always produced ugly html code, but in my tests it was the (above) macro that produced the (slightly) tighter code than the manual save - though of course I didn't test with your document. Both versions produced an ActiveX warning when I opened in Explorer and neither produced any warnings when opened in Firefox.



    <TechPubber> wrote in message news:b1ff301f-f426-47e5-bf54-5ec32c51855a@communitybridge.codeplex.com...

    I'm writing a Word 2007 macro that saves the active .docx file as .html. But it's saving the file in the My Documents folder. I want it to save it in the SAME folder as the .docx without having to hardcode this path in the macro. (Different people will be using the macro and will be using different directory structures.

    Also, when I open another file after the follwoing section of code, it's defaulting to My Documents. I do not want the current directory changed.

    Here's my code for the save-as-filtered-html chunk:

    ' SaveAsHtmlFile()
      'Dim CurrentDirectory As String
      'CurrentDirectory = Options.DefaultFilePath(wdCurrentFolderPath)

      Dim strDocName As String
      Dim intPos As Integer

     'Find position of extension in file name
      strDocName = ActiveDocument.Name
      intPos = InStrRev(strDocName, ".")

      If intPos = 0 Then

       'If the document has not yet been saved
       'Ask the user to provide a file name
       strDocName = InputBox("Please enter the name " & _
         "of your document.")
      Else

       'Strip off extension and add ".html" extension
       strDocName = Left(strDocName, intPos - 1)
       strDocName = strDocName & ".html"
      End If

     'Save file with new extension
      ActiveDocument.SaveAs FileName:=strDocName, _
       FileFormat:= _
       wdFormatFilteredHTML, LockComments:=False, Password:="", AddToRecentFiles _
       :=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts _
       :=False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
       SaveAsAOCELetter:=False
     'End SaveAsHtmlFile


    Graham Mayor - Word MVP
    www.gmayor.com
    Posted via the Communities Bridge
    http://communitybridge.codeplex.com/
    Saturday, November 13, 2010 7:56 AM
  • Hi TechPubber

    I see you've posted the new question as a separate one, which is definitely the right way to go :-)

    So I'm going to mark this discussion as "answered". If you don't agree, you can remove that...


    Cindy Meister, VSTO/Word MVP
    Sunday, November 14, 2010 5:29 AM
    Moderator