Word does not recognize the New Links

M

maperalia

I have a program (see below)which is run from excel and that does the
following:
1.- Save as the excel template file (template.xls) using the cells to create
the filename.
2.- Open the word template file (template.doc)
3.- Update the links automatically using the same path and filename in the
step 1

After the program finish I check the link in the word file by clicking “Alt
+ F9†to verify that the new links have taken place. Although the new link
took place I “Select all†under edit menu then I select “Update Link†and I
got the following message:
“Word in unable to create a link to the object you specified, Please insert
the object directly into your file without creating a linkâ€
After I click “OK†I got this message in all my links:
“Error! Not a valid link".

So I discover that even thought the link is there is not reading it.
Could you please tell me how can I fix this problem?

Thanks in advance.
Maperalia


‘&&&&&&&&&&&&&&&&&&&&&&&
Option Explicit

Public Sub SaveExcelOpenWordAndUpdateLinks()
Dim sPath As String
sPath = SaveExcelTemplatelAsSaveAs
OpenWordAndUpdateLinks (sPath)
End Sub


Function SaveExcelTemplatelAsSaveAs() As String

Dim WO As String
Dim grdprp As String
Dim sFilename As String
Dim Progname As String
Dim Filename As String
Dim myDateTime As String

WO = Worksheets("summary BLR").Range("M10")
myDateTime = Format(Worksheets("summary BLR").Range("M9").Value, "yyyymmdd")
Filename = "" & WO & ".grdprp." & myDateTime & ""
Progname = "C:\test\" & Filename & ".xls"
ActiveWorkbook.SaveCopyAs Progname

SaveExcelTemplatelAsSaveAs = Progname


End Function

'***********OPEN THE TEMPLATE WORD FILE ****************************
Sub OpenWordAndUpdateLinks(sPathToExcelFile As String)

Dim wordApp As Object
Dim fNameAndPath As String
Dim Filename As String

fNameAndPath = "C:\test\template.doc"
Set wordApp = CreateObject("Word.Application")
wordApp.Documents.Open (fNameAndPath)
wordApp.Visible = True
wordApp.Activate
'wordApp.Run ("C:\test\template.doc!UpdateLinks")


wordApp.Run macroname:="UpdateLinks", vArg1:=sPathToExcelFile
Set wordApp = Nothing



End Sub
'**********************************************************
'NEXT MACRO IS LOCATED IN THE WORD DOCUMENT


Option Explicit

Public Sub UpdateLinks(sPath As String)
Dim alink As Field
Dim linktype As Range
Dim linkfile As Range
Dim linklocation As Range
Dim i As Integer
Dim j As Integer
Dim linkcode As Range
Dim Message, Title, Default, Newfile
Dim counter As Integer



counter = 0
For Each alink In ActiveDocument.Fields
If alink.Type = wdFieldLink Then

Set linkcode = alink.Code
i = InStr(linkcode, Chr(34))

Set linktype = alink.Code
linktype.End = linktype.Start + i
j = InStr(Mid(linkcode, i + 1), Chr(34))

Set linklocation = alink.Code
linklocation.Start = linklocation.Start + i + j - 1



linkcode.Text = linktype & sPath & linklocation




End If
Next alink

End Sub

‘&&&&&&&&&&&&&&&&&&&&&&&
 
M

macropod

Hi maperalia,

From what I can tell, the problem is caused by the fact that paths in fields
need to be separated by either double back-slashes (i.e. '\\') or single
forward slashes (i.e. '/'), but your code only generates single back-slashes
(i.e. '\'). Also, if your filename or path has spaces anywhere in it, the
lot must be enclosed in double quotes.

You may be able to correct this by changing:
linkcode.Text = linktype & sPath & linklocation
to:
linkcode.Text = linktype & Chr(34) & Replace(sPath, "\", "\\") & Chr(34) &
linklocation

Cheers
 
M

maperalia

sMacropod;
Thanks for your quick response. I made the changes in my code with your new
statement. However, when I ran the program I noticed that the links has the
double back-slashes but when I click update link I got this message in all my
links:
"Error! No topic specified.
This error is different than before. I wonder if you can help me to find out
to fix this problem.

Best regards.
Maperalia
 
M

macropod

hi maperalia,

Seems I overdid the changes. All you need is:
linkcode.Text = linktype & Replace(sPath, "\", "\\") & linklocation


Cheers
 
M

maperalia

Macropod;
Thanks very much it is working wonderful!!!!!!!!. I really appreciate your
supporting in this matter.

Best regards.
Maperalia
 
M

maperalia

Macropod;
One last question. I have noticed that after I ran the macro all my links
get gray pattern. Althought this gray pattern does not show up in my prints
out. I wonder if I can I turn it off and on when is neccesary

Best regards
Maperalia
 
M

macropod

Hi maperalia,

The shading isn't caused by the macro - it's caused by your settings under
Tools|Options|View - 'field shading'.

Cheers
 

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