UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> I Need To Open A Word Doc, Fill With Bookmark Values, Safe And Close Word, Access 2007    
 
   
psalmon
post Nov 16 2017, 03:37 PM
Post#1



Posts: 208
Joined: 20-June 05
From: Vancouver, B.C. Canada


I have a sub that opens a specified Word document, fills in Bookmarks with data from a form and saves it in a specified folder. After it is saved I would like Word to close. I manage to close the document but Word stays open without a document. I have this code:
CODE
                objWord.ActiveDocument.SaveAs fileName:=Folder2 & "\" & Document
                objWord.Application.ActiveDocument.Close
                objWord.Application.Close
                Set objWord = Nothing
                ActiveWindow.Close


Can anyone help me with the code to close Word?

Thanks in advance,
Pat
Go to the top of the page
 
doctor9
post Nov 16 2017, 03:43 PM
Post#2


UtterAccess Editor
Posts: 17,924
Joined: 29-March 05
From: Wisconsin


Pat,

Do you have the Word Object Library included in the References? How are you creating objWord? You probably just need to QUIT the application object, not close it.

Hope this helps,

Dennis

--------------------
(;,;) Li'l Cthulu says: Please talk about what you're trying to do, as well as how you're doing it.
Changing your real table name to "Table1" and your real form name to "Form1" in your posts makes it more difficult to understand what's going on, not easier.
Guidelines for Posting Questions
Go to the top of the page
 
psalmon
post Nov 16 2017, 03:53 PM
Post#3



Posts: 208
Joined: 20-June 05
From: Vancouver, B.C. Canada


Hi Dennis,
Yes I do have the Word Object Library in the References.

For objWord here are the lines for that
CODE
Dim objWord As Object
Set objWord = GetObject(, "Word.Application")

    If TypeName(objWord) = "Nothing" Then
        'Word was not open -- create a new instance
        Set objWord = CreateObject("Word.Application")
        booCloseWord = True
    End If
        
         With objWord
            ' Make the application visible.
            .Visible = True
            
            ' Open the document.
            .Documents.Open fileName:=DocumentOriginal, ReadOnly:=True
            
            ' Move to each bookmark and insert text from the form.
            objWord.ActiveDocument.Bookmarks("JobNumber").Select
            objWord.Selection.Text = (CStr([MainJob]))


After the bookmarks are filled in I save it with the first section of code I posted.

CODE
                objWord.ActiveDocument.SaveAs fileName:=Folder2 & "\" & Document
                objWord.Application.ActiveDocument.Close
                objWord.Application.Close
                Set objWord = Nothing
                ActiveWindow.Close


Thanks,
Pat
Go to the top of the page
 
doctor9
post Nov 16 2017, 03:59 PM
Post#4


UtterAccess Editor
Posts: 17,924
Joined: 29-March 05
From: Wisconsin


Pat,

If you're using the Word Object Library, when you start a new line of code with "objWord." the possible choices should appear as a drop-down list in the editor where you're typing. Again, I believe "Quit" is the one you want.

Hope this helps,

Dennis

--------------------
(;,;) Li'l Cthulu says: Please talk about what you're trying to do, as well as how you're doing it.
Changing your real table name to "Table1" and your real form name to "Form1" in your posts makes it more difficult to understand what's going on, not easier.
Guidelines for Posting Questions
Go to the top of the page
 
JonSmith
post Nov 16 2017, 04:04 PM
Post#5



Posts: 3,160
Joined: 19-October 10



QUOTE
If you're using the Word Object Library, when you start a new line of code with "objWord." the possible choices should appear as a drop-down list in the editor where you're typing. Again, I believe "Quit" is the one you want.


Not unless you actually make use of the library by declaring things. You are mixing late binding behavior by declaring as "Object". Change that to Word.Application and you'll see the intellisense.
(In break mode you can see the intellisense aswell after the variable is instanced but I'm gonna assume you aren't writting code whilst in break mode)
Go to the top of the page
 
doctor9
post Nov 16 2017, 04:08 PM
Post#6


UtterAccess Editor
Posts: 17,924
Joined: 29-March 05
From: Wisconsin


Jon,

Yeah, I just noticed that in Pat's code. You need to do it this way:

CODE
Dim objWord As Word.Application


I find this makes coding MUCH easier.

Hope this helps,

Dennis

--------------------
(;,;) Li'l Cthulu says: Please talk about what you're trying to do, as well as how you're doing it.
Changing your real table name to "Table1" and your real form name to "Form1" in your posts makes it more difficult to understand what's going on, not easier.
Guidelines for Posting Questions
Go to the top of the page
 
psalmon
post Nov 16 2017, 04:14 PM
Post#7



Posts: 208
Joined: 20-June 05
From: Vancouver, B.C. Canada


Yes!!! Thanks so much Dennis!
Go to the top of the page
 
psalmon
post Nov 16 2017, 06:01 PM
Post#8



Posts: 208
Joined: 20-June 05
From: Vancouver, B.C. Canada


Thanks for all of your contributions.

I am having success opening the specified Word doc, filling in the Bookmarks, saving and closing Word!! Yeah

Next little wrinkle, if the user has another Word doc open it closes when this bit of code is executed.

CODE
With objWord
            ' Make the application visible.
            .Visible = True
            
            ' Open the document.
            .Documents.Open fileName:=DocumentOriginal, ReadOnly:=True


I don't want that to happen. Is there a way to close just the Bookmarked document that was just created, closing the document, but leaving Word open for the other previously open Word doc?

Thanks again!!
Pat
Go to the top of the page
 
doctor9
post Nov 17 2017, 09:22 AM
Post#9


UtterAccess Editor
Posts: 17,924
Joined: 29-March 05
From: Wisconsin


Pat,

If this is what you want, then you should probably just create a new instance of Word for your process, and skip all of the code that checks to see if Word is already open.

CODE
Set objWord = CreateObject("Word.Application")


Hope this helps,

Dennis

--------------------
(;,;) Li'l Cthulu says: Please talk about what you're trying to do, as well as how you're doing it.
Changing your real table name to "Table1" and your real form name to "Form1" in your posts makes it more difficult to understand what's going on, not easier.
Guidelines for Posting Questions
Go to the top of the page
 
JonSmith
post Nov 17 2017, 09:41 AM
Post#10



Posts: 3,160
Joined: 19-October 10



You mean

CODE
Set objWord = New Word.Application


Early binding remember tongue.gif
Go to the top of the page
 
doctor9
post Nov 17 2017, 09:59 AM
Post#11


UtterAccess Editor
Posts: 17,924
Joined: 29-March 05
From: Wisconsin


Jon,

Thanks. Need more caffeine this morning, clearly.

Dennis

--------------------
(;,;) Li'l Cthulu says: Please talk about what you're trying to do, as well as how you're doing it.
Changing your real table name to "Table1" and your real form name to "Form1" in your posts makes it more difficult to understand what's going on, not easier.
Guidelines for Posting Questions
Go to the top of the page
 
psalmon
post Nov 17 2017, 01:51 PM
Post#12



Posts: 208
Joined: 20-June 05
From: Vancouver, B.C. Canada


Thanks Dennis & Jon!!

That was perfect!!

Sincerely,
Pat
Go to the top of the page
 


Custom Search
RSSSearch   Top   Lo-Fi    14th December 2017 - 11:28 PM