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.

Re: Getting program to work with Word already open... by Jonathan

Jonathan
Tue May 24 17:14:43 CDT 2005


"axeman" <axeman@discussions.microsoft.com> wrote in message
news:698C29AE-3255-455C-A485-385ECA6CA2CA@microsoft.com...
>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


Re: Getting program to work with Word already open... by axeman

axeman
Wed May 25 10:58:07 CDT 2005

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
---------------------------------------------------------------------------------------------
END CODE

"Jonathan West" wrote:

>
> "axeman" <axeman@discussions.microsoft.com> wrote in message
> news:698C29AE-3255-455C-A485-385ECA6CA2CA@microsoft.com...
> >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
>
>

Re: Getting program to work with Word already open... by Jonathan

Jonathan
Wed May 25 11:04:27 CDT 2005

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

"axeman" <axeman@discussions.microsoft.com> wrote in message
news:87E348EA-6050-40BB-8736-34269E9C9BA6@microsoft.com...
> 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 CODE
> ---------------------------------------------------------------------------------------------
> '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 Sub
> ---------------------------------------------------------------------------------------------
> END CODE
>
> "Jonathan West" wrote:
>
>>
>> "axeman" <axeman@discussions.microsoft.com> wrote in message
>> news:698C29AE-3255-455C-A485-385ECA6CA2CA@microsoft.com...
>> >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
>>
>>


Re: Getting program to work with Word already open... by David

David
Wed May 25 13:56:18 CDT 2005

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 =3D documents(1).saveas FileName:=3DuserDesktopDir +
OfferLetterName
or
ActiveDocument.SaveAs FileName:=3DuserDesktopDir + 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 =3D ActiveDocument.FormFields("EEF=ADName1").Result
to
firstname =3D aDoc.FormFields("EEF=ADName1").Result=20
etc.


Re: Getting program to work with Word already open... by Jonathan

Jonathan
Wed May 25 14:34:31 CDT 2005


"David Sisson" <dsisson@hotmail.com> wrote in message
news:1117047377.840739.148850@o13g2000cwo.googlegroups.com...
> 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


Re: Getting program to work with Word already open... by David

David
Wed May 25 14:50:03 CDT 2005

>> 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, ...

I stand corrected!


Re: Getting program to work with Word already open... by axeman

axeman
Wed May 25 15:31:06 CDT 2005

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

"Jonathan West" wrote:

>
> "David Sisson" <dsisson@hotmail.com> wrote in message
> news:1117047377.840739.148850@o13g2000cwo.googlegroups.com...
> > 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
>
>

Re: Getting program to work with Word already open... by axeman

axeman
Wed May 25 15:59:21 CDT 2005

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

Re: Getting program to work with Word already open... by Jonathan

Jonathan
Wed May 25 15:59:53 CDT 2005


"axeman" <axeman@discussions.microsoft.com> wrote in message
news:F3B4CF6E-3654-47F7-82FB-2E0A4328D5AA@microsoft.com...
> 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


Re: Getting program to work with Word already open... by axeman

axeman
Thu May 26 08:43:04 CDT 2005

Thank you for your explanation!

Axe

"Jonathan West" wrote:

>
> "axeman" <axeman@discussions.microsoft.com> wrote in message
> news:F3B4CF6E-3654-47F7-82FB-2E0A4328D5AA@microsoft.com...
> > 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
>
>