Saving hyperlinked documents to a specified folder

C

Carlos Chalhoub

Hi listmates,

I want to open a document from the LAN loaded with hyperlinks to other documents nested in subfolders below. I need to create a macro that does the following:

1. Create a folder on my C drive which name mimics that of the active document (without the extension).
2. Save the active document to the the just created folder.
(I came up with this code, but it does not work)

strFileA = ActiveDocument.Name
pos = InStr(strFileA, ".")
If pos > 0 Then
strFileA = Left(myDocname, pos - 1)
End If
MkDir "C:\" & strFileA
strFileB = "C:\" & strFileA
' Define backup target file path.
ActiveDocument.SaveAs FileName:=strFileB

3. Grab all the hyperlinks in the active document and save the linked files to subfolders created according the path in the hyperlink, i.e. if the hyperlink address is
{ HYPERLINK "Process Flow\\BF2273Delete.f" }
then create a sub-folder (under the folder created at step 1) named "Process folder" and save the file "BF2273Delete.f" to it.

4. After closing the master document, would it be possible to go to the main folder (created at step 2) and zip it (using WinZip) with all the subfolders and files included?

Thanks for any pointers or help.
Carlos
 
C

Carlos Chalhoub

Hi listmates,

I have been able to fix steps 1 and 2 (see code below). However, I need some help with saving the hyperlinks. Please be aware, that the hyperlinks do not necessarily point to Word files. These could be Excel, PDF, MS Project, etc. files. I guess there should not be a .Follow in my code, because that would open the link and mess up the macro. Thanks for any help.

Carlos

On Error GoTo CheckErr
'Error Handler

strFileA = ActiveDocument.Name
pos = InStr(strFileA, ".")
If pos > 0 Then
strFileA = Left(strFileA, pos - 1)
End If
sPath = "C:\" & strFileA
MkDir sPath
' Define backup target file path.
ActiveDocument.SaveAs fileName:=sPath & "\" & ActiveDocument.Name

CheckErr:
If err = 75 Then 'Directory Exists
MsgBox "The Folder you are trying to create already exists. The procedure has been stopped."
Exit Sub
End If


Hi listmates,

I want to open a document from the LAN loaded with hyperlinks to other documents nested in subfolders below. I need to create a macro that does the following:

1. Create a folder on my C drive which name mimics that of the active document (without the extension).
2. Save the active document to the the just created folder.
(I came up with this code, but it does not work)

strFileA = ActiveDocument.Name
pos = InStr(strFileA, ".")
If pos > 0 Then
strFileA = Left(myDocname, pos - 1)
End If
MkDir "C:\" & strFileA
strFileB = "C:\" & strFileA
' Define backup target file path.
ActiveDocument.SaveAs FileName:=strFileB

3. Grab all the hyperlinks in the active document and save the linked files to subfolders created according the path in the hyperlink, i.e. if the hyperlink address is
{ HYPERLINK "Process Flow\\BF2273Delete.f" }
then create a sub-folder (under the folder created at step 1) named "Process folder" and save the file "BF2273Delete.f" to it.

4. After closing the master document, would it be possible to go to the main folder (created at step 2) and zip it (using WinZip) with all the subfolders and files included?

Thanks for any pointers or help.
Carlos
 
C

Carlos Chalhoub

Hi again,

I have recycled some code I found on the newsgroup, but can't get it to
work. The code breaks at the midpath line. What I'm trying to do is create a
folder according to the last folder string in the hyperlink's path. Example:
if the hyperlink path is Release\Technnical\srce\CSDC5670.doc, I want to
create a "srce" folder and copy the linked file to it.

Can somebody help please?

For Each aHyperlink In ActiveDocument.Hyperlinks
If Len(aHyperlink.Address) > 0 Then 'hyplink to file
docname = Mid(aHyperlink.Address, InStrRev(aHyperlink.Address, "\") +
1)
midpath = aHyperlink.Address - docname - "\"
docpath = sPath & "\" & Mid(midpath, InStrRev(midpath, "\") + 1)
If docname = docpath Then 'same folder as curr doc
docpath = ActiveDocument.Path & Application.PathSeparator &
docname
Else: MkDir docpath
FileSystemObject.CopyFile docname, docpath & "\"
End If
End If
Next aHyperlink


Hi listmates,

I have been able to fix steps 1 and 2 (see code below). However, I need some
help with saving the hyperlinks. Please be aware, that the hyperlinks do not
necessarily point to Word files. These could be Excel, PDF, MS Project, etc.
files. I guess there should not be a .Follow in my code, because that would
open the link and mess up the macro. Thanks for any help.

Carlos

On Error GoTo CheckErr
'Error Handler

strFileA = ActiveDocument.Name
pos = InStr(strFileA, ".")
If pos > 0 Then
strFileA = Left(strFileA, pos - 1)
End If
sPath = "C:\" & strFileA
MkDir sPath
' Define backup target file path.
ActiveDocument.SaveAs fileName:=sPath & "\" & ActiveDocument.Name

CheckErr:
If err = 75 Then 'Directory Exists
MsgBox "The Folder you are trying to create already exists. The
procedure has been stopped."
Exit Sub
End If


Hi listmates,

I want to open a document from the LAN loaded with hyperlinks to other
documents nested in subfolders below. I need to create a macro that does the
following:

1. Create a folder on my C drive which name mimics that of the active
document (without the extension).
2. Save the active document to the the just created folder.
(I came up with this code, but it does not work)

strFileA = ActiveDocument.Name
pos = InStr(strFileA, ".")
If pos > 0 Then
strFileA = Left(myDocname, pos - 1)
End If
MkDir "C:\" & strFileA
strFileB = "C:\" & strFileA
' Define backup target file path.
ActiveDocument.SaveAs FileName:=strFileB

3. Grab all the hyperlinks in the active document and save the linked files
to subfolders created according the path in the hyperlink, i.e. if the
hyperlink address is
{ HYPERLINK "Process Flow\\BF2273Delete.f" }
then create a sub-folder (under the folder created at step 1) named "Process
folder" and save the file "BF2273Delete.f" to it.

4. After closing the master document, would it be possible to go to the main
folder (created at step 2) and zip it (using WinZip) with all the subfolders
and files included?

Thanks for any pointers or help.
Carlos
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top