A
Anne P.
Hi,
I have a userform with two textboxes: txtReLine and txtATName. After the
user fills out these two textboxes, I am trying to create an AutoText entry
in a template named Personal.dot in the Word startup directory. I am
creating a hidden instance of Word and opening a new document to insert the
text from txtReline, so that I can then either select it or assign it to a
range and then create an AutoText entry.
If I run the macro to open the userform, I get an error message "Object
variable or with block variable not set" and nothing happens. If I step
through the code in VB Editor, an AutoText entry is created in the
Personal.dot template, but is created under the category Normal and the
value is empty. On the line "selection.typetext" the text is typed into the
active document, not the new document that is created. I believe from this
point, I need to somehow assign the text that is entered into the document
to a range object, but I am not sure how to accomplish this.
Below is the relevant code from the cmdOK button. I appreciate any help
with this.
strATCategory = "ReLine"
' Create new hidden instance of Word.
Set wdApp = New Word.Application
' Create a new document.
Set docNew = wdApp.Documents.Add
' Add text to document.
strReLine = txtReLine.Text
Selection.TypeText strReLine
'Create autotext in personal.dot
' assign the global template to an object
strPath = Options.DefaultFilePath(wdStartupPath) & _
"\Personal.dot"
Set objTemplate = Templates(strPath)
On Error Resume Next
bRemoveStyle = False
' if the style already exists, this succeeds
Set oNewStyle = docNew.Styles(strATCategory)
If Err.Number < 0 Then
' the style didn't exist, so create it
Err.Clear
Set oNewStyle = _
docNew.Styles.Add(Name:=strATCategory)
bRemoveStyle = True
End If
' change style of selection's paragraph to new category
bmrange.Style = docNew.Styles(strATCategory)
' add AT entry to global template
objTemplate.AutoTextEntries.Add Name:=strATName, _
Range:=Selection.Range
' reverse style assignment
ActiveDocument.Undo
' remove new style if it was created by this code
If bRemoveStyle Then
ActiveDocument.Styles(strATCategory).Delete
End If
'Close document without saving changes.
With docNew
.Close wdDoNotSaveChanges
End With
wdApp.Quit
Set wdApp = Nothing
Unload Me
Thanks,
Anne P.
I have a userform with two textboxes: txtReLine and txtATName. After the
user fills out these two textboxes, I am trying to create an AutoText entry
in a template named Personal.dot in the Word startup directory. I am
creating a hidden instance of Word and opening a new document to insert the
text from txtReline, so that I can then either select it or assign it to a
range and then create an AutoText entry.
If I run the macro to open the userform, I get an error message "Object
variable or with block variable not set" and nothing happens. If I step
through the code in VB Editor, an AutoText entry is created in the
Personal.dot template, but is created under the category Normal and the
value is empty. On the line "selection.typetext" the text is typed into the
active document, not the new document that is created. I believe from this
point, I need to somehow assign the text that is entered into the document
to a range object, but I am not sure how to accomplish this.
Below is the relevant code from the cmdOK button. I appreciate any help
with this.
strATCategory = "ReLine"
' Create new hidden instance of Word.
Set wdApp = New Word.Application
' Create a new document.
Set docNew = wdApp.Documents.Add
' Add text to document.
strReLine = txtReLine.Text
Selection.TypeText strReLine
'Create autotext in personal.dot
' assign the global template to an object
strPath = Options.DefaultFilePath(wdStartupPath) & _
"\Personal.dot"
Set objTemplate = Templates(strPath)
On Error Resume Next
bRemoveStyle = False
' if the style already exists, this succeeds
Set oNewStyle = docNew.Styles(strATCategory)
If Err.Number < 0 Then
' the style didn't exist, so create it
Err.Clear
Set oNewStyle = _
docNew.Styles.Add(Name:=strATCategory)
bRemoveStyle = True
End If
' change style of selection's paragraph to new category
bmrange.Style = docNew.Styles(strATCategory)
' add AT entry to global template
objTemplate.AutoTextEntries.Add Name:=strATName, _
Range:=Selection.Range
' reverse style assignment
ActiveDocument.Undo
' remove new style if it was created by this code
If bRemoveStyle Then
ActiveDocument.Styles(strATCategory).Delete
End If
'Close document without saving changes.
With docNew
.Close wdDoNotSaveChanges
End With
wdApp.Quit
Set wdApp = Nothing
Unload Me
Thanks,
Anne P.