Question : How to declare the document I'm working in

Hi experts

I have a template that on new opens up a user form where the user selects the type of document they want to create. From their selection it inserts the contents of another file into the document. Once they select the option button I record their selection in the customdocumentproperties. Once it finishes inserting the document I call on the customdocumentproperty to fill a part of the nextform (called GEOProcurementMenu). But when it goes to refer to the customdocumentproperty I get a run-time error 76 File Path Not found. But I can confirm that it is actually recorded in the document.

The weird thing is that it works fine on my computer.

Help? Does anyone know what could be going on?
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
Sub AutoNew
   ProcurementDocType.Show
   
'RUN TIME ERROR 76 ON LINE BELOW
   If ActiveDocument.CustomDocumentProperties.Item("xDocType") = "Request for Tender" Then GEOProcurementMenu.txtDocNo = "RFT"
   If ActiveDocument.CustomDocumentProperties.Item("xDocType") = "Request for Quote" Then GEOProcurementMenu.txtDocNo = "RFQ"
   If ActiveDocument.CustomDocumentProperties.Item("xDocType") = "Request for Proposal" Then GEOProcurementMenu.txtDocNo = "RFP"
   
    GEOProcurementMenu.txtRevisionDate = Format$(Date, "dd/mm/yyyy")
    
    GEOProcurementMenu.Show
    
End Sub

Private Sub ClickAction()


    If Me.OptTender = False And _
        Me.OptQuote = False And _
        Me.OptProposal = False Then
        MsgBox "Please select the Document Type.", , "SELECTION REQUIRED"
        Exit Sub
    End If

   'Load fields

    If Me.OptTender = True Then
        ActiveDocument.CustomDocumentProperties.Item("xDocType").Value = "Request for Tender"
        ElseIf Me.OptQuote = True Then
        ActiveDocument.CustomDocumentProperties.Item("xDocType").Value = "Request for Quote"
        ElseIf Me.OptProposal = True Then
        ActiveDocument.CustomDocumentProperties.Item("xDocType").Value = "Request for Proposal"
        
    End If


Call InsertDoc

Unload Me

End Sub

Private Sub InsertDoc()

Path = ActiveDocument.AttachedTemplate.Path

Selection.GoTo What:=wdGoToBookmark, Name:="StartHere"


n = Selection.Information(wdActiveEndSectionNumber)
            
Call UnprotectMyDoc

If ActiveDocument.CustomDocumentProperties.Item("xDocType").Value = "Request for Tender" Then
        Selection.InsertFile Path & "\Support File Proc_Tender.docx"
    
    ElseIf ActiveDocument.CustomDocumentProperties.Item("xDocType").Value = "Request for Quote" Then
            Selection.InsertFile Path & "\Support File Proc_Quote.docx"
        
    ElseIf ActiveDocument.CustomDocumentProperties.Item("xDocType").Value = "Request for Proposal" Then
            Selection.InsertFile Path & "\Support File Proc_Proposal.docx"
    
    Else
    MsgBox "Unable to find support file. Please contact your administrator", , "Error"
    End If
 
Call ProtectMyDoc
 
 
ActiveDocument.Sections(n).Headers(wdHeaderFooterPrimary).LinkToPrevious = True
ActiveDocument.Sections(n).Footers(wdHeaderFooterPrimary).LinkToPrevious = True
  
  
End Sub

Answer : How to declare the document I'm working in

Since the heading of this asks about declaring a document, I am going to throw out the idea of replacing ActiveDocument with a variable name.

I would suggest beginning your code with:
Dim doc As Document

Then you can make sure that the correct document is involved with:
Set doc = ActiveDocument

Since you mentioned that the user is creating a document, you might even want to make sure it's attached to the template by using:
Set doc = Documents.Add(Template:="Normal")

With the template name being whatever you need.  The nice thing about using a template for a new document is that you can be certain that the bookmarks will exist.

Without seeing the rest of your template, I'm not entirely sure what is missing, but you could tighten things by declaring a Document type. ActiveDocument can get kind of wonky if the user has multiple files open (though it's unlikely if you're calling the code within the active document).
Random Solutions  
 
programming4us programming4us