Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word VBA : field update

Status
Not open for further replies.

shimuno

Technical User
Oct 9, 2008
1
FR
Hello

I've been working on a macro enabling a user to update a Word document linked to an Excel workbook. The macro open the Excel file if it closed, then the user can modify the Excel file before updating the Word document.

The code below works quite well approximately 9 times out of 10. Unfortunately, sometimes the following errors happen :
- the Excel file does not open
- the Excel file opens but it is impossible to update the fields
- the Excel file window is not activated, so the user thinks that it cannot open it (actually it is opened, but hidden behind the Word window).

Any ideas ?

Here is the code :


Sub Updatelinks


Dim xlApp As Object
Dim xlWB As Object

Set xlApp = CreateObject("Excel.Application")

On Error Resume Next

Set xlApp = GetObject(, "Excel.Application")


Set xlWB = xlApp.Workbooks("Champs automatiques.xls")

On Error GoTo 0


'Open the Excel file if it is closed

If xlWB Is Nothing Then

xlApp.AskToUpdateLinks = False

Set xlWB = xlApp.Workbooks.Open("H:\OBLIGS\Breve\Modeles\Champs automatiques.xls", ReadOnly:=True)

xlApp.Visible = True

End If

ActiveDocument.Fields.Update

End sub


Thank you for your help !
 




I'd suggest you open VBA Help and get help on GetObject. You've got a few more things to include, as you will see.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 



I would also use the explicit server name, rather than a drive letter...
Code:
Set xlWB = xlApp.Workbooks.Open("[b]\\YourServerName[/b]\OBLIGS\Breve\Modeles\Champs automatiques.xls", ReadOnly:=True)


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top