Hi All,
I've been putting together a document that builds user guides for me from a user form. I'm trying to shorten the whole document so that I don't have hundreds of the same lines throughout it, Here is a snippet from the start of the button that initiates the macro:
I've tried doing the following:
Unfortunately when I do this I get "Object variable or with block variable not set"
on
objWord.Selection.WholeStory
in CopyPaste
I can't see what I'm doing wrong here and was wondering if anyone else knew if this is even possible to start of with, any help here is appreciated!
I've been putting together a document that builds user guides for me from a user form. I'm trying to shorten the whole document so that I don't have hundreds of the same lines throughout it, Here is a snippet from the start of the button that initiates the macro:
VBA Code:
Public Sub CreateGuideButton_Click()
SetVars 'Runs SetVars sub to get variables
Set objWord = CreateObject("Word.Application")
Set WordSave = objWord.Documents.Add
'Makes the Directory to be saved to-----------------------------------
If Dir(ADir, vbDirectory) = "" Then
MkDir ADir
End If
'Creates the Main Document--------------------------------------------
objWord.Visible = True
WordSave.SaveAs BDir
'Main Page statements---------------------------------------------------------------------------------
If HPCheckBox = True Then
objWord.Documents.Open DocHP, ConfirmConversions = False, ReadOnly = False
objWord.Selection.WholeStory
objWord.Selection.Copy
objWord.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
objWord.Windows(NewGuide).Activate
objWord.Selection.PasteAndFormat (wdUseDestinationStylesRecovery)
End If
If VPCheckBox = True Then
objWord.Documents.Open DocVP, ConfirmConversions = False, ReadOnly = False
objWord.Selection.WholeStory
objWord.Selection.Copy
objWord.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
objWord.Windows(NewGuide).Activate
objWord.Selection.InsertBreak
objWord.Selection.PasteAndFormat (wdUseDestinationStylesRecovery)
End If
If VICheckBox = True Then
objWord.Documents.Open DocVI, ConfirmConversions = False, ReadOnly = False
objWord.Selection.WholeStory
objWord.Selection.Copy
objWord.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
objWord.Windows(NewGuide).Activate
objWord.Selection.InsertBreak
objWord.Selection.PasteAndFormat (wdUseDestinationStylesRecovery)
End If
If XLightsCBox.Value = vbNullString Then
objWord.Documents.Open DocXLights, ConfirmConversions = False, ReadOnly = False
objWord.Visible = True
objWord.Selection.WholeStory
objWord.Selection.Copy
objWord.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
objWord.Windows(NewGuide).Activate
objWord.Selection.InsertBreak
objWord.Selection.PasteAndFormat (wdUseDestinationStylesRecovery)
End If
I've tried doing the following:
VBA Code:
Sub CopyPaste()
SetVars
Dim objWord As Object
Set objWord = CreateObject("Word.Application")
objWord.Selection.WholeStory
objWord.Selection.Copy
objWord.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
objWord.Windows(NewGuide).Activate
objWord.Selection.PasteAndFormat (wdUseDestinationStylesRecovery)
End Sub
Public Sub CreateGuideButton_Click()
SetVars 'Runs SetVars sub to get variables
Set objWord = CreateObject("Word.Application")
Set WordSave = objWord.Documents.Add
'Makes the Directory to be saved to-----------------------------------
If Dir(ADir, vbDirectory) = "" Then
MkDir ADir
End If
'Creates the Main Document--------------------------------------------
objWord.Visible = True
WordSave.SaveAs BDir
'Main Page statements---------------------------------------------------------------------------------
If HPCheckBox = True Then
objWord.Documents.Open DocHP, ConfirmConversions = False, ReadOnly = False
Call CopyPaste
End If
End Sub
Unfortunately when I do this I get "Object variable or with block variable not set"
on
objWord.Selection.WholeStory
in CopyPaste
I can't see what I'm doing wrong here and was wondering if anyone else knew if this is even possible to start of with, any help here is appreciated!