• Dealing with open documents

    Author
    Topic
    #462996

    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.

    Code:
       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
    Viewing 0 reply threads
    Author
    Replies
    • #1180242

      You should be able to use something like:

      objWord.Documents.Count

      to determine whether there are already documents open (and then put up a prompt to close any open docs, if any are found).

      You could alternatively start assigning object variables to any already-open documents, but that could get messy since you wouldn’t know in advance just how many documents might be open – so it’s probably neater just to prompt the user to close any existing documents, before your code proceeds.

      Gary

      • #1180243

        Thanks Gary! Just the type of hint I was looking for.

    Viewing 0 reply threads
    Reply To: Dealing with open documents

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: