PDA

View Full Version : Save Active Word Doc with URL File Path



BrI
01-24-2018, 10:13 AM
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

paulked
01-24-2018, 02:09 PM
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

BrI
01-25-2018, 09:27 AM
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

BrI
01-25-2018, 09:59 AM
Just tested directly in Word and encouraged as is working there -- but why not via Excel??

paulked
01-25-2018, 12:02 PM
I tested it fully, and it saved and closed the active doc regardless of whether it was stored on C:\, X:\, https:// no problems.

BrI
01-25-2018, 12:33 PM
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.

paulked
01-25-2018, 12:58 PM
Opened 3 documents, ran the sub 3 times, results are reverse order to me opening the documents (ie correct).

21455

paulked
01-25-2018, 01:02 PM
Sorry, missed your last post. Do you have the correct Object Library referenced? (mine is 16.0)

BrI
01-25-2018, 01:08 PM
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.

paulked
01-25-2018, 01:31 PM
That's a bit weird, it would point to you having more that one instance of Word running.

BrI
01-25-2018, 02:06 PM
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

paulked
01-25-2018, 02:17 PM
You're welcome!

BrI
01-25-2018, 02:46 PM
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.

snb
01-25-2018, 02:59 PM
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

paulked
01-25-2018, 03:45 PM
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