Consulting

Results 1 to 15 of 15

Thread: Save Active Word Doc with URL File Path

  1. #1
    VBAX Regular
    Joined
    Apr 2017
    Posts
    66
    Location

    Save Active Word Doc with URL File Path

    Working from Excel, I want to save active Word docs that have been retrieved from a data base and have a "URL" File Path like: http://fsc...../mot542.doc.

    The code below works with active Word docs retrieved from a standard C:\ Drive etc. But the URL File Path does not seem to be recognized even though the these documents have a .doc extension are open and active in Word.

    If there are no other Word docs open, the Error is # 4248
    This command is not available because no document is open
    If there are other Word docs open, one of the other docs will be saved instead of the active "URL Word doc".

    Sub ActiveURLWordDoc
    
        Dim objWord
        Dim objDoc As Object
        Set objWord = GetObject(, "Word.Application")
        objWord.Visible = True
        
    'Below works for Word docs with file path like: "C:\ ....mot542.doc"
    'But gives error when Word doc has a file path like: "http://fsc...../mot542.doc"
    
        Debug.Print objWord.ActiveDocument.FullName <-------------- ERROR 
    
         OrigDoc = objWord.ActiveDocument.FullName
    
        Set objDoc = objWord.Documents(OrigDoc)
    
         objDoc.Save
    
    End Sub
    Last edited by BrI; 01-24-2018 at 10:28 AM.

  2. #2
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    I got the error 'Bad File Name on the

    Set objDoc = objWord.Documents(OrigDoc)
    line. But this worked for any location:

    Sub ActiveURLWordDoc()
        Dim objWord As Word.Application
        Set objWord = GetObject(, "Word.Application")
        objWord.ActiveDocument.Save
        objWord.ActiveDocument.Close
    End Sub
    Semper in excretia sumus; solum profundum variat.

  3. #3
    VBAX Regular
    Joined
    Apr 2017
    Posts
    66
    Location
    Thanks but still have the same issue.

    It may be your code seems to be working but is actually referencing another open word doc rather than the Active Doc - that's what I find happens.

    I only get the error when the Active Doc has a file path like http://.... .doc and no other docs with "normal" file paths are open.

    To simplify for testing, I am using:

        Dim objWord
        Set objWord = GetObject(, "Word.Application")
           
        Debug.Print objWord.ActiveDocument.FullName   <----- ERROR Here

  4. #4
    VBAX Regular
    Joined
    Apr 2017
    Posts
    66
    Location
    Just tested directly in Word and encouraged as is working there -- but why not via Excel??

  5. #5
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    I tested it fully, and it saved and closed the active doc regardless of whether it was stored on C:\, X:\, https:// no problems.
    Semper in excretia sumus; solum profundum variat.

  6. #6
    VBAX Regular
    Joined
    Apr 2017
    Posts
    66
    Location
    OK, still not working for me - maybe something unique to my settings etc.

    One thing I noted was I couldn't use the line below from your code - gives me error as shown.

    Dim objWord As Word.Application <---- Compile Error: User-defined type not defined.

  7. #7
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    Opened 3 documents, ran the sub 3 times, results are reverse order to me opening the documents (ie correct).

    shot25.jpg
    Semper in excretia sumus; solum profundum variat.

  8. #8
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    Sorry, missed your last post. Do you have the correct Object Library referenced? (mine is 16.0)
    Semper in excretia sumus; solum profundum variat.

  9. #9
    VBAX Regular
    Joined
    Apr 2017
    Posts
    66
    Location
    Appreciate your efforts, would have given up otherwise.

    I discovered basic error as did not have a reference to the Word Object library. Setting that resolved compile error and then testing did return correct file paths on occasion which was better than previous. But not consistent - sometimes get error, sometimes returns path for non-active document.

  10. #10
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    That's a bit weird, it would point to you having more that one instance of Word running.
    Semper in excretia sumus; solum profundum variat.

  11. #11
    VBAX Regular
    Joined
    Apr 2017
    Posts
    66
    Location
    That's what some of my other reading seems to be pointing to, maybe has to do with how the http doc opens. I will continue to work on it - thanks for assistance

  12. #12
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    You're welcome!
    Semper in excretia sumus; solum profundum variat.

  13. #13
    VBAX Regular
    Joined
    Apr 2017
    Posts
    66
    Location
    Just FYI, in case its somehow possible to overcome this issue - the http docs are retrieved from a company program/application and they are opened in a new instance of word - can be seen in the Task Manager.

  14. #14
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    I'd use:

    Sub M_snb()
        With CreateObject("Word.application")
            With .documents.Open("http://www.snb-vba.eu/bestanden/Liander tarieven.docx")
            .SaveAs2 "G:\OF\voorbeeld.docx"
            .Close 0
            End With
            .Quit
        End With
    End Sub

  15. #15
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    You could loop through all open documents and save & close them all with something like:

    Sub ActiveURLWordDoc()
        Dim objWord As Word.Application
        Do
            Set objWord = GetObject(, "Word.Application")
            objWord.ActiveDocument.Save
            objWord.ActiveDocument.Close
        Loop Until objWord.Windows.Count = 0
    End Sub
    Semper in excretia sumus; solum profundum variat.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •