How to make this VBS script keep the document clos

2019-08-26 12:34发布

问题:

I just received this VBS code that appends a line to my table in MS "Word" 2003. It works fine, but I want it to add a line to the table without opening the file. Is it possible ? Or, perhaps, I need to use some command that would close the document as soon as it is opened.

Set wd = CreateObject("Word.Application")

wd.Visible = True

Set doc = wd.Documents.Open ("c:\docs\addtotable.doc")

Set r = doc.Tables(1).Rows.Add

aa = Split("turtle,dog,rooster,maple", ",")

For i = 0 To r.Cells.Count - 1
  r.Cells(i + 1).Range.Text = aa(i)
Next

回答1:

If you don't want to open Word's window, use "wd.Visible = False" instead of "wd.Visible = True". In that case you may want to save to changed document to the same/a new file. Read the VBA documentation about .Save and .SaveAs. Closing the application by .Quit may be a good idea too.