|
-
Nov 3rd, 2008, 01:27 PM
#1
Thread Starter
Member
Wierd behavior when opening Word template
I have VBscript running in a custom Outlook item that upon the click of different User Interface buttons opens different Word documents from templates that are stored in Windows folders, and then populates all the fields in the template from data in the Outlook item. All but one of these templates work as intended. For some reason, one of them generates an input box window titled "Microsoft Office Word" with an OK button and a Cancel button which must be clicked by the user in order for the template document to be displayed. Then when the document is finally displayed it has no Menu bar and it is not possible to save it.
I originally thought it must be the VBscript procedure in my Outlook item that was causing the problem for this one template document. However, I have checked and the code for the problem document is exactly the same syntax as the code for the documents that are working correctly.
This caused me to look at the properties of the problem template compared to the other templates and they are the same. However, I noticed that the behaviour of the problem template varies depending on how it is opened from the Windows folder where it is stored:
1) If you double-click the icon for the template document it generates the problem "Microsoft Office Word" input box, however the document does have a menu bar.
2) If you right-click the icon and use the Open menu item to open it, it opens normally without the problem input box and it does have a menu bar
3) If you right-click the icon and use the Open with.. menu item to open it with Word, it generates the problem input box, however the document does have a menu bar.
Can anyone tell me what is happening here and how to eliminate the problem input box and insure the open document has a menu bar when I open the template document programatically from my VBscript code in Outlook?
I am including my VBscript code that is used to do this below:
Code:
'================================================================================================================
Sub cmdSubmitAd_Click()
' User click of the GoodFaithAdRequest command button will open the GoodFaith Ad template, automatically
' complete fields marked with bookmarks with data from this BidProfile, and display to user for editing/printing.
Dim objDoc 'As Word Document
Dim fso 'As Scripting.FileSystemObject
Dim strFolderName 'As String
Dim strDocName 'As String
Dim strMsg 'As String
Dim intAns 'As Integer
Dim colFields 'As Collection
Dim strBidDate 'As String
On Error Resume Next 'Comment out when debugging
'If Err <> 0 Then 'Remove comments on this block when debugging
'MsgBox "Error number " & Err.Number & _
'vbCrLf & vbCrLf & Err.Description, , _
'"Error in ErrorHandlerCode"
Set objDoc = GetWordDoc(m_strAns & "\GoodFaithEffort\TemplateGoodFaith.dot")
Set colFields = objDoc.FormFields
'Populate the fields and display or print the Word Template...see Mosher Section 22.3 for details
If Not objDoc Is Nothing Then
objDoc.Application.Visible = True
objDoc.Activate
strMsg = "Is this project a Prevailing Wage project?"
intAns = MsgBox(strMsg, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Prevailing Wage Project?")
' Fill-in form fields in Word document from source fields in current item
If intAns = vbNo Then
colFields("PrevailingWageNo").CheckBox.Value = True
colFields("WageType").Result = "non-prevailing wage"
Else
colFields("PrevailingWageYes").CheckBox.Value = True
colFields("WageType").Result = "prevailing wage"
End IF
colFields.Unprotect
colFields("BidNo").Result = Item.UserProperties("File As")
colFields("FaxDate").Result = Date
colFields("ProjectName").Result = Item.UserProperties("Project Name")
If Item.UserProperties("Bid Date/Time") = "1/1/4501" Then
strBidDate = "Not Yet Assigned"
Else
strBidDate = FormatDateTime(Item.UserProperties("Bid Date/Time"),vbGeneralDate)
End If
colFields("BidDateTime").Result = strBidDate
If Item.UserProperties("Bid Date/Time") = "1/1/4501" Then
strBidDate = "Not Yet Assigned"
Else
strBidDate = FormatDateTime(Item.UserProperties("Bid Date/Time"),vbGeneralDate)
End If
colFields("BidDateTime").Result = strBidDate
objDoc.Application.Visible = True
objDoc.Activate
' Uncomment next lines if you want automatic printouts of the report
' objDoc.Application.Options.PrintBackground = True
' objDoc.PrintOut
' Uncomment next line if you don't want to save the document
' objDoc.Close SaveChanges:=wdDoNotSaveChanges
' Uncomment next line if you are not making document visible in above code..required to restore WORD
' Call RestoreWord
' Change name of document and save path before saving
strFolderName = Item.UserProperties("File As")
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FolderExists("C:\Documents and Settings\" & strFolderName) = False Then
MsgBox "A Windows folder for this BidNo. does not yet exist and will be created now.", _
vbMsgBoxSetForeground
Call CreateBidFolder(strFolderName)
End If
strDocName = strFolderName & "_ GoodFaithAdRequest"
objDoc.SaveAs ("C:\Documents and Settings\" & strFolderName & "\" & strDocName)
objDoc.Application.Visible = True
objDoc.SetFocus
End If
Set objDoc = Nothing
Set fso = Nothing
Set strFolderName = Nothing
Set strDocName = Nothing
Set strMsg = Nothing
Set intAns = Nothing
Set colFields = Nothing
Set strBidDate = Nothing
End Sub
'==============================================================================================================
'===============================================================
' GetWordDoc
' From Mosher book, Listing 22.8
'---------------------------------------------------------------
' Purpose : Returns Word document, either from template or blank
' Notes : Sets m_blnWeOpenedWord and m_blnWordPrintBackground
' for use with RestoreWord. If you don't plan to
' print, you don't need to set the latter.
'---------------------------------------------------------------
' Arguments :
'-----------
' strTemplatePath (Optional) (String) -- name of Word template
'---------------------------------------------------------------
' Returns : New Word.Document
'===============================================================
Function GetWordDoc(strTemplatePath)
Dim objWord 'As Word.Application
On Error Resume Next 'Comment out when debugging
'If Err <> 0 Then 'Remove comments on this block when debugging
'MsgBox "Error number " & Err.Number & _
'vbCrLf & vbCrLf & Err.Description, , _
'"Error in ErrorHandlerCode"
'End If
m_blnWeOpenedWord = False
Set objWord = GetObject(, "Word.Application")
If objWord Is Nothing Then
Set objWord = CreateObject("Word.Application")
m_blnWeOpenedWord = True
End If
m_blnWordPrintBackground = _
objWord.Options.PrintBackground
If strTemplatePath = "" Then
strTemplatePath = "Normal.dot"
End If
Set GetWordDoc = objWord.Documents.Add(strTemplatePath)
objWord.Visible = True
On Error GoTo 0
Set objWord = Nothing
End Function
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|