Results 1 to 3 of 3
  1. #1
    3 Star Lounger
    Join Date
    Nov 2001
    Location
    Brisbane, Queensland, Australia
    Posts
    330
    Thanks
    10
    Thanked 0 Times in 0 Posts
    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
    "Heading for the deep end"

  2. #2
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts
    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

  3. #3
    3 Star Lounger
    Join Date
    Nov 2001
    Location
    Brisbane, Queensland, Australia
    Posts
    330
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Thanks Gary! Just the type of hint I was looking for.
    "Heading for the deep end"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •