Hi all,
I am currently using the code below in Access (yes, I use it and only just understand it ) to create a .doc file from a set template, shove detail into set fields, save it and then display it. The code I have works fine unless the user already has a .doc file open. If so, the current file is saved with the specified filename and the new file opens as a new document.
What I need is code added to this that either says, “You already have a doc open, please close it before continuing” or code that recognises that a file is already open and somehow keeps track of which file is which and does the right thing to the right one.
I’ve left a bit out but I think all the important stuff is there.
Set objWord = GetObject(, "Word.Application") If Err.Number = 429 Then 'Word is not running; creating a Word object Set objWord = CreateObject("Word.Application") Err.Clear End If ' open file if it already exists strWordDoc = QPath & "F-" & [QuoteNum] & ".doc" strTestFile = Nz(Dir(strWordDoc)) If Not strTestFile = "" Then Set objDocs = objWord.Documents objDocs.Open FileName:=strWordDoc objWord.Visible = True objWord.Activate objWord.windowstate = 1 Exit Sub End If Set objDocs = objWord.Documents objDocs.Add strWordTemplate 'Write information to Word custom document properties from 'previously created variables ' get prps.Item values 'Highlight the entire Word document and update fields, so the data 'written to the custom doc props is displayed in the DocProperty fields objWord.selection.WholeStory objWord.selection.Fields.Update objWord.selection.HomeKey Unit:=6 objWord.selection.Goto Name:="QDescript" objWord.selection.InsertAfter strQDescription objWord.selection.Goto Name:="Price" objWord.selection.InsertAfter strFQPrice objWord.Visible = True objWord.Activate objWord.windowstate = 0 strSaveName = "F-" & strQuoteNum & ".doc" strSaveNamePath = strDocsPath & strSaveName objWord.activedocument.SaveAs strSaveNamePath Set objWord = Nothing