VBA call sub referencing copying from one word document and pasting to another.

RBNebula

New Member
Joined
Aug 7, 2020
Messages
1
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
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:

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!
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.

Forum statistics

Threads
1,223,231
Messages
6,170,885
Members
452,364
Latest member
springate

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