Getting program to work with Word already open...

A

axeman

I have a situation. I have a Word VBA program that I am running but can't
figure out how to make it work when there is already another Word document
open. The userform itself will pop up but when it's supposed to transfer all
the information over to a template it just sits there and does nothing. It
works fine if there aren't any other open files though. If you could please
provide some insight to this situation it would be of great help.
 
J

Jonathan West

axeman said:
I have a situation. I have a Word VBA program that I am running but can't
figure out how to make it work when there is already another Word document
open. The userform itself will pop up but when it's supposed to transfer
all
the information over to a template it just sits there and does nothing. It
works fine if there aren't any other open files though. If you could
please
provide some insight to this situation it would be of great help.

Its impossible to give much help without seeing the relvant part of the
code.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
A

axeman

Alright here's the code below. I didn't include the code for the macros that
are called in the Fields() module because they aren't the cause of the
problem. Any help is greatly appreciated!!!!

BEGIN COD
---------------------------------------------------------------------------------------------
'Declare for call to mpr.dll.
Declare Function WNetGetUser Lib "mpr.dll" _
Alias "WNetGetUserA" (ByVal lpName As String, _
ByVal lpUserName As String, lpnLength As Long) As Long

Const NoError = 0 'The Function call was successful


Function GetUserName() As String

'Buffer size for the return string.
Const lpnLength As Long = 255

'Get return buffer space.
Dim status As Integer

'For getting user information.
Dim lpName, lpUserName As String

'Assign the buffer size constant to lpUserName.
lpUserName = Space$(lpnLength + 1)

'Get the log-on name of the person using product.
status = WNetGetUser(lpName, lpUserName, lpnLength)

'See whether error occurred.
If status = NoError Then
'This line removes the null character. Strings in C are null-
'terminated. Strings in Visual Basic are not null-terminated.
'The null character must be removed from the C strings to be used
'cleanly in Visual Basic.
lpUserName = Left$(lpUserName, InStr(lpUserName, Chr(0)) - 1)
End If

'Display the name of the person logged on to the machine.
GetUserName = lpUserName

End Function

Sub Offer()
'
' Offer Macro
'
Load formOfferLetter
formOfferLetter.Show

End Sub

Public Sub Fields()
'
' Field Value Updater
'
On Error Resume Next

Dim userDesktopDir As String
Dim OfferLetterName As String
Dim firstname, lastname As String


If formOfferLetter.optMgrToEE.Value = True Then
Application.Run MacroName:="UpdateFieldsEE"
End If

If formOfferLetter.optMgrToMgr.Value = True Then
Application.Run MacroName:="UpdateFieldsMgr"
End If

If Err.Number = 5152 Then

Err.Clear
userDesktopDir = "D:\Profiles\" + GetUserName + "\Desktop\Offer
Letters\"
firstname = ActiveDocument.FormFields("EEFName1").Result
lastname = ActiveDocument.FormFields("EELName1").Result
OfferLetterName = firstname + " " + lastname + " - Offer.doc"
ActiveDocument.SaveAs FileName:=userDesktopDir + OfferLetterName

If Err.Number = 5152 Then

Err.Clear
userDesktopDir = "D:\Documents And Settings\" + GetUserName +
"\Desktop\Offer Letters\"
firstname = ActiveDocument.FormFields("EEFName1").Result
lastname = ActiveDocument.FormFields("EELName1").Result
OfferLetterName = firstname + " " + lastname + " - Offer.doc"
ActiveDocument.SaveAs FileName:=userDesktopDir + OfferLetterName

If Err.Number = 5152 Then
MsgBox "You must create an Offer Letters folder on your
desktop. The wizard will no longer continue."
ActiveDocument.ActiveWindow.Close
SaveChanges:=wdDoNotSaveChanges
End If
End If
End If


End Su
 
J

Jonathan West

Nothing obvious there. Have you tried opening the VBA editor and pressinbg
the F8 key to step through the code a line at a time to see what happens?

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
D

David Sisson

I've never had any luck, when multiple Word docs are open, using the
Activedocument statement. It would work one time, and not the next,
etc.

Try setting the document name before inputting text into the
formfields.

But this in itself will cause more programming headaches, since you'll
have to save the doc first before refering to it by name.)

Dim aDoc as Document

Set aDoc = documents(1).saveas FileName:=userDesktopDir +
OfferLetterName
or
ActiveDocument.SaveAs FileName:=userDesktopDir + OfferLetterName
(since you'll use this right after the template loads (Maybe in a
autoopen event), you'll be safe in using Activedocument.

Then change

firstname = ActiveDocument.FormFields("EEF­Name1").Result
to
firstname = aDoc.FormFields("EEF­Name1").Result
etc.
 
J

Jonathan West

David Sisson said:
I've never had any luck, when multiple Word docs are open, using the
Activedocument statement. It would work one time, and not the next,
etc.

Try setting the document name before inputting text into the
formfields.

But this in itself will cause more programming headaches, since you'll
have to save the doc first before refering to it by name.)

That is not correct, you can assign a newly opened or created document to a
document object variable, before the document is ever saved


Dim aDoc as Document
Set aDoc = Documents.Add


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
A

axeman

So question, when you use Documents.Open does that make the file that you
just opened the ActiveDocument or no? What I have noticed is that the code
obviously works when there isn't another instance of Word running or another
open Word document. But when there is and the code runs it executes in the
file that was already open so therefore you end up with the code running
appropriately but on the wrong file. I also want to add that when the
userform pops up it runs off of a blank document that then I use
ActiveDocument.Close to close it in the subroutine called Fields() (That line
was missing from the code that I submitted earlier). It then opens up the
appropriate template according to the user's selection and runs the code.
Does this make any sense to you guys? Also can you expand/explain more as to
how what you guys suggested would make this work? Should I be using something
other than ActiveDocument?

Thanks,
Axe
 
A

axeman

Actually, just to let you guys know I think I figured it out. I turned off
the On Error Resume Next and came to find out why this wasn't working
appropriately. What seems to happen is that when I run an
ActiveDocument.Close to close the blank document and then open the actual
template (while there is another unrelated file open by the user in Word) it
cannot find the template. Why you ask? Because, I am assuming, that when the
focus changes from one document to the next being active the actual path to
that file changes. Therefore, if I just tell it to open a template without
specifying the COMPLETE path in which to find that file it cannot continue.
So, I added the path temporarily and it worked. Brilliant! Now, even if there
are multiple files open, it works like it should! Thanks for the help guys!!!

Axe
 
J

Jonathan West

axeman said:
So question, when you use Documents.Open does that make the file that you
just opened the ActiveDocument or no?

Generally yes. But if the user happens to click on another document in the
taskbar while the macro is running, it won't *stay* the ActiveDocument!
What I have noticed is that the code
obviously works when there isn't another instance of Word running or
another
open Word document. But when there is and the code runs it executes in the
file that was already open so therefore you end up with the code running
appropriately but on the wrong file. I also want to add that when the
userform pops up it runs off of a blank document that then I use
ActiveDocument.Close to close it in the subroutine called Fields() (That
line
was missing from the code that I submitted earlier). It then opens up the
appropriate template according to the user's selection and runs the code.
Does this make any sense to you guys? Also can you expand/explain more as
to
how what you guys suggested would make this work? Should I be using
something
other than ActiveDocument?

The way to handle this is for any macro that may run for a while not ro use
ActiveDocument Instead, If the macro is to operate on a newly created
document, do this

Dim oDoc as Document
Set oDoc = Documents.Add

Add the appropriate template name if you want a new document based on a
template other than normal.dot

To operate on a newly opened document, do something like this

Dim oDoc as Document
Set oDoc = Documents.Open(Filename:="C:\My Documents\myfile.doc")

To operate on the current alreay open document, do this

Dim oDoc as Document
Set oDoc = ActiveDocument

In all these cases, any code you already have that uses ActiveDocument can
instead use oDoc. In addition, anywhere you use the Selection object, it is
better to define one or more Range object variables and use them instead.

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
A

axeman

Thank you for your explanation!

Axe

Jonathan West said:
Generally yes. But if the user happens to click on another document in the
taskbar while the macro is running, it won't *stay* the ActiveDocument!


The way to handle this is for any macro that may run for a while not ro use
ActiveDocument Instead, If the macro is to operate on a newly created
document, do this

Dim oDoc as Document
Set oDoc = Documents.Add

Add the appropriate template name if you want a new document based on a
template other than normal.dot

To operate on a newly opened document, do something like this

Dim oDoc as Document
Set oDoc = Documents.Open(Filename:="C:\My Documents\myfile.doc")

To operate on the current alreay open document, do this

Dim oDoc as Document
Set oDoc = ActiveDocument

In all these cases, any code you already have that uses ActiveDocument can
instead use oDoc. In addition, anywhere you use the Selection object, it is
better to define one or more Range object variables and use them instead.

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top