PDA

View Full Version : Close document



SmithyUK
10-02-2010, 07:36 AM
Hey,

I have a dummy document that I need to close after some code has been run, the code opens another document and makes that active (I believe).

I need to close the document that is running the code, and I have tried ThisDocument.Close, but nothing happens. And I can't close it.

I can paste the code if that helps.

Thanks for any help!

Alex.

macropod
10-03-2010, 04:17 PM
Hi Alex,

Have you changed anything in the document containing the code? If so, you'll need to tell Word what what to do with them before the document can close.

shekhu
10-03-2010, 10:25 PM
Though I am not an expert, try this if this works.


Sub CloseANDNew()

ActiveDocument.Close
Documents.Add Template:="Normal", NewTemplate:=False, DocumentType:=0

End Sub

fumei
10-04-2010, 08:48 AM
"I need to close the document that is running the code"

Odd. So the code is actually IN the document you wish to close? Not a template?

OK, but what is the relationship with this "dummy" document?

"I have a dummy document that I need to close after some code has been run"

What makes it a "dummy"? And what do you even mean by dummy? There really is no such thing.

As a hint for dealing with documents, it is really a good idea to use document objects. Then identification and actions can be very specific.

I am not sure what you are doing, or trying to do.

I am not sure if there is three document happening, or what.

1.. document running the code
2. the "dummy" document
3. some new document ("the code opens another document and makes that active (I believe)")

If you use document objects, then you can name them.

Or, if you know th ename of any document, and want to close it, then
Documents("name.doc").Close


Obviously if you close the document running the code the code terminates.

fumei
10-04-2010, 09:31 AM
"the code opens another document and makes that active (I believe)."

Yes, any time a new document is opened it becomes ActiveDocument. But take a look at:

Documents.Open "c:\whatever\thatdoc.doc"

thatdoc.doc is now ActiveDocument, and the only way to refer to it is with ActiveDocument.


Dim ThatDoc As Document
Set ThatDoc = Documents.Open "c:\whatever\thatdoc.doc"
Now the object ThatDoc is the newly opened document thatdoc.doc.

So now you can use ThatDoc - as an object - rather than ActiveDocument.

The reason for this is that it is better to be able to refer specifically (and explicitly) to a document.

Instructions using ThatDoc action thatdoc.doc...regardless of whether it is ActiveDocument, or not. In other words, you can do things to that document without ever having to have it ActiveDocument.

Including closing it.

fumei
10-04-2010, 09:33 AM
"I can paste the code if that helps."

Probably yes.

SmithyUK
10-08-2010, 02:51 AM
Firstly, thanks for the responses!

I'll paste an explanation from what I posted on another forum:

"Background info:
We use a document management system called OMS from a company called FWBS, this is custom software so I can only go to them for help and they have no suggestions, but I figure there must be a way to do what I want.

Basically this software hooks into MS Word, I have no clue how as they have passworded all their templates etc, which doesn't help. Each different case has a "matter code" which is stored in SQL along with all the information on that "matter" i.e. names, addresses.

Ideas:
Now, I had contemplated pulling it directly from SQL but there is no way of knowing what the current matter number is! So that was a no go.

I also tried using their built in form fields which pull document variables somehow "{ DOCVARIABLE "#ADDRESSEE" \* MERGEFORMAT }", but... I'm using VBA code to calculate my own Word form fields, to use the OMS ones I need to refresh at the end (when it saves) and this removes all my fields. So I set it not to refresh, which means my fields are fine, but the OMS ones don't pull through!

Problem:
It's either one way or the other, I can have the OMS fields OR my fields, and I need them both. I thought about pulling in the doc variables using VBA, but it just doesn't find them, so whatever OMS does, I can't replicate in VBA."

So I created a dummy document that pulls the fields in correctly (this is a template in OMS), I then take the fields as variables, create a new document from template and tried to close the document but couldn't.

The Close and new code looks good, I'll give that a go.

Sorry for the late response, I have not been well.

Alex.

PS. Will paste code if this close and new doesn't work :)

SmithyUK
10-08-2010, 02:54 AM
EDIT: Double post

macropod
10-08-2010, 04:30 AM
Hi SmithyUK,

Please see:
http://www.excelguru.ca/node/7
then post a link here to the other forum, and there to this one.

SmithyUK
10-10-2010, 09:53 AM
Public add1, add2, add3, add4, postcode, address, theirref, ourref, clname As String

Sub billCreate()
' Alex Smith

add1 = ActiveDocument.Fields(1).Result.Text
add2 = ActiveDocument.Fields(2).Result.Text
add3 = ActiveDocument.Fields(3).Result.Text
add4 = ActiveDocument.Fields(4).Result.Text
postcode = ActiveDocument.Fields(5).Result.Text
address = ActiveDocument.Fields(6).Result.Text
theirref = ActiveDocument.Fields(7).Result.Text
ourref = ActiveDocument.Fields(8).Result.Text
clname = ActiveDocument.Fields(9).Result.Text

ActiveDocument.Close 'SaveChanges = False

Documents.Add ("\\norwich2\OMSMC\Precs\Alex\BillPrec.dot")

ActiveDocument.FormFields("name").Result = clname
ActiveDocument.FormFields("address1").Result = add1
ActiveDocument.FormFields("address2").Result = add2
ActiveDocument.FormFields("address3").Result = add3
ActiveDocument.FormFields("address4").Result = add4
ActiveDocument.FormFields("postcode").Result = postcode
ActiveDocument.FormFields("oneline").Result = address
ActiveDocument.FormFields("ourref").Result = ourref
ActiveDocument.FormFields("ourref2").Result = ourref
ActiveDocument.FormFields("clname2").Result = clname


End Sub

This is my code to create the bill and import the variables, I just get an error when trying to execute ActiveDocument.Close

SmithyUK
10-10-2010, 09:58 AM
Hi SmithyUK,

Please see: <>
then post a link here to the other forum, and there to this one.
Need a post count of 5 to post links... I'll post it in the next one -.-

SmithyUK
10-10-2010, 09:58 AM
http://buildyourown.org.uk/forums/topic.asp?TOPIC_ID=51279 - Original thread