HELP! crazy crazy issue with excel macro initiating word

bontemps

New Member
Joined
Jul 13, 2014
Messages
9
Hi there
I have the strangest problem! I have a form builder that opens a word template and populates the letter based on the values in the form, then saves the .dotx template as a .doc file. It works fine unless I go back to the form builder and make a change and request the program to run again. Then the program modifies the Any help is appreciated! This was a last minute find and the program is being presented to the vp on Tuesday! My word code is below:
Code:
public Sub Insert_Bm_Text()
Dim WDApp As Word.Application
Dim WDDoc As Word.Document
Dim wdbm As Word.Bookmark

Dim path As String
Dim template As String
Dim tmp As String

template = "hletemplate.dotx"
Set WDApp = New Word.Application
'reference path'
path = ActiveWorkbook.path
'Reference active document'
Dim BMRange As Range
Dim wordapp As Object
Dim wrdDoc As Object
Dim tmpDoc As Object


Set wordapp = GetObject(, "Word.Application")
If wordapp Is Nothing Then
'no current word application
Set wordapp = CreateObject("Word.Application")
Set WDDoc = Word.Documents.Open(path & "\" & template)
wordapp.Visible = True
Else:
'word app running
Set WDDoc = Word.Documents.Open(path & "\" & template)
WDApp.Visible = True
End If
'loop -on error continue
On Error Resume Next

The program then fills in the fields and then the following code saves the doc and notifies the user 
'save as'
path = path & "\" & Txtclient & "_" & txtcr_no & "HLE.doc"
WDDoc.SaveAs (path)
'save document and close word'
'WDDoc.Save
WDDoc.Close
WDApp.Quit False
Set WDApp = Nothing
AppActivate "Microsoft Excel"
MsgBox (path & " has been created. Click OK to view")
tmp = "explorer.exe """ & path & """"
Shell tmp, vbNormalFocus
I can't figure out why if the program is generated more than once while the docs are open with no variable change the template would be changed. Any help is soooo greatly appreciated!
 
Last edited by a moderator:

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
You have a number of logic errors in the code all related to which Word instance your code is communicating with. You start with launching an instance of Word and assigning it to a variable WDApp. But in further down you then try to grab a running instance of Word using the GetObject command. And to confuse things more you also try to use Word directly (Word.Documents.Open(...)).
I expect this code should do better:
Code:
Public Sub Insert_Bm_Text()
    Dim WDApp As Word.Application
    Dim WDDoc As Word.Document
    Dim wdbm As Word.Bookmark

    Dim path As String
    Dim template As String
    Dim tmp As String

    Dim BMRange As Range
    Dim wordapp As Object
    Dim wrdDoc As Object
    Dim tmpDoc As Object

    template = "hletemplate.dotx"
    Set WDApp = New Word.Application
    WDApp.Visible = True
    'reference path'
    path = ActiveWorkbook.path
    'Reference active document'

    Set WDDoc = WDApp.Documents.Open(path & "\" & template)

    'The program then fills in the fields and then the following code saves the doc and notifies the user
    'save as'
    path = path & "\" & Txtclient & "_" & txtcr_no & "HLE.doc"
    WDDoc.SaveAs (path)
    'save document and close word'
    'WDDoc.Save
    WDDoc.Close False
    WDApp.Quit
    Set WDApp = Nothing
    AppActivate "Microsoft Excel"
    MsgBox (path & " has been created. Click OK to view")
    tmp = "explorer.exe """ & path & """"
    Shell tmp, vbNormalFocus
End Sub
 
Upvote 0
Thank you very much for the support and the quick response. As you can see I am not a programmer. I am just getting my feet wet and these sound like basic vba coding errors. I also think that I need to tell the program to check to see if the file is open and if so tell the user that there is an error and they have to close the file before continuing. Any thoughts on how I can do this without causing additional logic errors? Again -thank you so much for the guidance!
 
Upvote 0
Instead of this line:

Code:
Set WDDoc = WDApp.Documents.Open(path & "\" & template)

use this:

Code:
Set WDDoc = WDApp.Documents.Add(path & "\" & template)

It will create a new empty document based on the template, rather than open the template for editing.
 
Upvote 0

Forum statistics

Threads
1,215,068
Messages
6,122,950
Members
449,095
Latest member
nmaske

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top