Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6014477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:41:52+00:00 2026-05-23T02:41:52+00:00

i am copying cells from excel into an open word document. the way i

  • 0

i am copying cells from excel into an open word document. the way i am doing this is just copying the contents of a cell into the clipboard and REPLACING a specific KEYWORD in the word document like so:

if cell A1 = "some word" i need too replace the string “QUERYA1” in the word document

i am doing it like this:

Sub NoFormatPaste()

    wdFind.Replacement.Text = ""
    wdFind.Forward = True
    wdFind.Wrap = wdFindContinue
    wdFind.Execute
    If IsEmpty(Selection.Text) And Len(Selection.Text) = 0 Then
    ClipEmpty.PutInClipboard
    appWd.Selection.PasteSpecial DataType:=wdPasteText
    End
    Else
    appWd.Selection.PasteSpecial DataType:=wdPasteText
    End If
    CutCopyMode = False

End Sub

when this sub runs, it works on every field except it gives an error if the cell is empty. i have this formula in the cell: =+IF(K10="XXX","",K10)

when this formula yields NOTHING or a blank, and i run my macro, i get an error on PASTING this into word. i am getting an error called 4168 command failed/command execution on this line:

appWd.Selection.PasteSpecial DataType:=wdPasteText

here is my complete code:

Dim appWd As Word.Application
Dim wdFind As Object
Dim ClipEmpty As New MSForms.DataObject
Dim ClipT As String

Sub FormatPaste()

    wdFind.Replacement.Text = ""
    wdFind.Forward = True
    wdFind.Wrap = wdFindContinue
    wdFind.Execute
    If IsEmpty(Selection.Text) And Len(Selection.Text) = 0 Then
    ClipEmpty.PutInClipboard
    appWd.Selection.Paste
    End
    Else
    appWd.Selection.Paste
    End If
    CutCopyMode = False

End Sub

Sub NoFormatPaste()

    wdFind.Replacement.Text = ""
    wdFind.Forward = True
    wdFind.Wrap = wdFindContinue
    wdFind.Execute
    If IsEmpty(Selection.Text) And Len(Selection.Text) = 0 Then
    ClipEmpty.PutInClipboard
    appWd.Selection.PasteSpecial DataType:=wdPasteText
    End
    Else
    appWd.Selection.PasteSpecial DataType:=wdPasteText
    End If
    CutCopyMode = False

End Sub

Sub CopyDatatoWord()

Dim docWD As Word.Document
Dim sheet1 As Object
Dim sheet2 As Object
Dim SaveCell1 As String
Dim SaveCell2 As String
Dim SaveCell3 As String
Dim Dir1 As String
Dim Dir2 As String


    Set appWd = CreateObject("Word.Application")
    appWd.Visible = True
    'Set docWD = appWD.Documents.Open("S:\Practice Quarterly Reports\2011 Q1 - V5\Practice Profile Template 2011.docx")
    Set docWD = appWd.Documents.Open("C:\Documents and Settings\jhill\Desktop\Practice Profile Template 2011.docx")

    'Select Sheet where copying from in excel
    Set sheet1 = Sheets("TABLES")
    Set sheet2 = Sheets("REPORT INFO")
    Set wdFind = appWd.Selection.Find
    ClipT = "  "
    ClipEmpty.SetText ClipT

    sheet1.Range("B3:B6").Copy
    wdFind.Text = "Qwerty01"
    Call FormatPaste

    sheet1.Range("B10:B15").Copy
    wdFind.Text = "Qwerty02"
    Call FormatPaste

    sheet1.Range("C21:D28").Copy
    wdFind.Text = "Qwerty03"
    Call FormatPaste

    sheet1.Range("B32:F42").Copy
    wdFind.Text = "Qwerty04"
    Call FormatPaste

    sheet1.Range("B46:D52").Copy
    wdFind.Text = "Qwerty05"
    Call FormatPaste

    sheet1.Range("B58:F68").Copy
    wdFind.Text = "Qwerty06"
    Call FormatPaste

    sheet1.Range("B74:G84").Copy
    wdFind.Text = "Qwerty07"
    Call FormatPaste

    sheet1.Range("B87").Copy
    wdFind.Text = "Qwerty08"
    Call NoFormatPaste

    sheet1.Range("B88").Copy
    wdFind.Text = "Qwerty09"
    Call NoFormatPaste

    sheet1.Range("B89").Copy
    wdFind.Text = "Qwerty10"
    Call NoFormatPaste

    sheet1.Range("B90").Copy
    wdFind.Text = "Qwerty11"
    Call NoFormatPaste

    sheet1.Range("B91").Copy
    wdFind.Text = "Qwerty12"
    Call NoFormatPaste

    sheet1.Range("B92").Copy
    wdFind.Text = "Qwerty13"
    Call NoFormatPaste

    sheet1.Range("B93").Copy
    wdFind.Text = "Qwerty14"
    Call NoFormatPaste

    sheet1.Range("B94").Copy
    wdFind.Text = "Qwerty15"
    Call NoFormatPaste

    sheet2.Range("D4").Copy
    wdFind.Text = "Qwerty16"
    Call NoFormatPaste

    sheet2.Range("B5").Copy
    wdFind.Text = "Qwerty17"
    Call NoFormatPaste


    sheet2.Range("D4").Copy
    wdFind.Text = "Qwerty18"
    Call NoFormatPaste

    sheet2.Range("B8").Copy
    wdFind.Text = "Qwerty19"
    Call NoFormatPaste

    sheet2.Range("B9").Copy
    wdFind.Text = "Qwerty20"
    Call NoFormatPaste

    sheet2.Range("B10").Copy
    wdFind.Text = "Qwerty21"
    Call NoFormatPaste

    sheet2.Range("B11").Copy
    wdFind.Text = "Qwerty22"
    Call NoFormatPaste

    sheet2.Range("B12").Copy
    wdFind.Text = "Qwerty23"
    Call NoFormatPaste

    sheet2.Range("B13").Copy
    wdFind.Text = "Qwerty24"
    Call NoFormatPaste

    sheet2.Range("B14").Copy
    wdFind.Text = "Qwerty25"
    Call NoFormatPaste

    sheet2.Range("B15").Copy
    wdFind.Text = "Qwerty26"
    Call NoFormatPaste

    sheet2.Range("B16").Copy
    wdFind.Text = "Qwerty27"
    Call NoFormatPaste

    sheet2.Range("B17").Copy
    wdFind.Text = "Qwerty28"
    Call NoFormatPaste

    sheet2.Range("B5").Copy
    wdFind.Text = "Qwerty29"
    Call NoFormatPaste

    sheet2.Range("B5").Copy
    wdFind.Text = "Qwerty30"
    Call NoFormatPaste

    sheet2.Range("B5").Copy
    wdFind.Text = "Qwerty31"
    Call NoFormatPaste

    SaveCell1 = sheet2.Range("D3").Text
    SaveCell2 = sheet2.Range("B6").Text
    SaveCell3 = SaveCell2 & "\" & SaveCell1

    Dir1 = "\\annapurna\Shared\Practice Quarterly Reports\2011 Q1 - V5\ & SaveCell2"
    Dir2 = "\\annapurna\Shared\Practice Quarterly Reports\2011 Q1 - V5\ & SaveCell3"


    If Len(Dir1) = False Then
    MkDir Dir1
    End If


    'docWD.SaveAs (Dir2 & ".docx")
    docWD.SaveAs ("\\annapurna\Shared\Practice Quarterly Reports\Q1_2011\Test.docx")

    'appWD.Quit

Set appWd = Nothing
Set docWD = Nothing
Set appXL = Nothing
Set wbXL = Nothing

End Sub

what am i doing wrong? what is the reason i get an error only on the paste of a blank

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:41:53+00:00Added an answer on May 23, 2026 at 2:41 am

    Here is the code solution:

    You had to reference the countclipboardformats function to check if there was anything on the clipboard and then if empty set to a string value chosen.

    It appears to be a glitch MS clipboard copy and paste function and the clipboard function.

    Public Declare Function CountClipboardFormats Lib "user32" () As Long
    
    Dim appWd As Word.Application
    Dim wdFind As Object
    Dim ClipEmpty As New MSForms.DataObject
    Dim ClipT As String
    
    Function IsClipboardEmpty() As Boolean
        IsClipboardEmpty = (CountClipboardFormats() = 0)
    End Function
    
    Sub CheckClipBrd()
    
    If IsClipboardEmpty() = True Then
    ClipEmpty.PutInClipboard
    End If
    End Sub
    
    Sub FormatPaste()
    
        wdFind.Replacement.Text = ""
        wdFind.Forward = True
        wdFind.Wrap = wdFindContinue
        wdFind.Execute
        Call CheckClipBrd
        appWd.Selection.Paste
        CutCopyMode = False
    
    End Sub
    
    Sub NoFormatPaste()
    
        wdFind.Replacement.Text = ""
        wdFind.Forward = True
        wdFind.Wrap = wdFindContinue
        wdFind.Execute
        Call CheckClipBrd
        appWd.Selection.PasteSpecial DataType:=wdPasteText
        CutCopyMode = False
    
    End Sub
    
    Sub CopyDatatoWord()
    
    Dim docWD As Word.Document
    Dim sheet1 As Object
    Dim sheet2 As Object
    Dim saveCell1 As String
    Dim saveCell2 As String
    Dim saveCell3 As String
    Dim dir1 As String
    Dim dir2 As String
    
    
        Set appWd = CreateObject("Word.Application")
        appWd.Visible = True
        Set docWD = appWd.Documents.Open("\\annapurna\Shared\Practice Quarterly Reports\Q1_2011\Practice Profile Template 2011.docx")
    
        'Select Sheet where copying from in excel
        Set sheet1 = Sheets("TABLES")
        Set sheet2 = Sheets("REPORT INFO")
        Set wdFind = appWd.Selection.Find
        ClipT = "  "
        ClipEmpty.SetText ClipT
    
        sheet1.Range("B3:B6").Copy
        wdFind.Text = "Qwerty01"
        Call FormatPaste
    
        sheet1.Range("B10:B15").Copy
        wdFind.Text = "Qwerty02"
        Call FormatPaste
    
        sheet1.Range("C21:D28").Copy
        wdFind.Text = "Qwerty03"
        Call FormatPaste
    
        sheet1.Range("B32:F42").Copy
        wdFind.Text = "Qwerty04"
        Call FormatPaste
    
        sheet1.Range("B46:D52").Copy
        wdFind.Text = "Qwerty05"
        Call FormatPaste
    
        sheet1.Range("B58:F68").Copy
        wdFind.Text = "Qwerty06"
        Call FormatPaste
    
        sheet1.Range("B74:G84").Copy
        wdFind.Text = "Qwerty07"
        Call FormatPaste
    
        sheet1.Range("B87").Copy
        wdFind.Text = "Qwerty08"
        Call NoFormatPaste
    
        sheet1.Range("B88").Copy
        wdFind.Text = "Qwerty09"
        Call NoFormatPaste
    
        sheet1.Range("B89").Copy
        wdFind.Text = "Qwerty10"
        Call NoFormatPaste
    
        sheet1.Range("B90").Copy
        wdFind.Text = "Qwerty11"
        Call NoFormatPaste
    
        sheet1.Range("B91").Copy
        wdFind.Text = "Qwerty12"
        Call NoFormatPaste
    
        sheet1.Range("B92").Copy
        wdFind.Text = "Qwerty13"
        Call NoFormatPaste
    
        sheet1.Range("B93").Copy
        wdFind.Text = "Qwerty14"
        Call NoFormatPaste
    
        sheet1.Range("B94").Copy
        wdFind.Text = "Qwerty15"
        Call NoFormatPaste
    
        sheet2.Range("D4").Copy
        wdFind.Text = "Qwerty16"
        Call NoFormatPaste
    
        sheet2.Range("B5").Copy
        wdFind.Text = "Qwerty17"
        Call NoFormatPaste
    
        sheet2.Range("D4").Copy
        wdFind.Text = "Qwerty18"
        Call NoFormatPaste
    
        sheet2.Range("B8").Copy
        wdFind.Text = "Qwerty19"
        Call NoFormatPaste
    
        sheet2.Range("B9").Copy
        wdFind.Text = "Qwerty20"
        Call NoFormatPaste
    
        sheet2.Range("B10").Copy
        wdFind.Text = "Qwerty21"
        Call NoFormatPaste
    
        sheet2.Range("B11").Copy
        wdFind.Text = "Qwerty22"
        Call NoFormatPaste
    
        sheet2.Range("B12").Copy
        wdFind.Text = "Qwerty23"
        Call NoFormatPaste
    
        sheet2.Range("B13").Copy
        wdFind.Text = "Qwerty24"
        Call NoFormatPaste
    
        sheet2.Range("B14").Copy
        wdFind.Text = "Qwerty25"
        Call NoFormatPaste
    
        sheet2.Range("B15").Copy
        wdFind.Text = "Qwerty26"
        Call NoFormatPaste
    
        sheet2.Range("B16").Copy
        wdFind.Text = "Qwerty27"
        Call NoFormatPaste
    
        sheet2.Range("B17").Copy
        wdFind.Text = "Qwerty28"
        Call NoFormatPaste
    
        sheet2.Range("C3").Copy
        wdFind.Text = "Qwerty29"
        Call FormatPaste
    
        sheet2.Range("C3").Copy
        wdFind.Text = "Qwerty30"
        Call FormatPaste
    
        sheet2.Range("C3").Copy
        wdFind.Text = "Qwerty31"
        Call FormatPaste
    
        saveCell1 = sheet2.Range("D3").Text
        saveCell2 = sheet2.Range("B6").Text
        saveCell3 = saveCell2 & "\" & saveCell1
    
        dir1 = "\\annapurna\Shared\Practice Quarterly Reports\Q1_2011\" & saveCell2
        dir2 = "\\annapurna\Shared\Practice Quarterly Reports\Q1_2011\" & saveCell3
    
    
        If Len(dir1) = False Then
        MkDir dir1
        End If
    
    
        'docWD.SaveAs (Dir2 & ".docx")
        docWD.SaveAs ("\\annapurna\Shared\Practice Quarterly Reports\Q1_2011\Test.docx")
    
        'appWD.Quit
    
    Set appWd = Nothing
    Set docWD = Nothing
    Set appXL = Nothing
    Set wbXL = Nothing
    
    End Sub
    

    😉 Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm copying a piece of text from Excel to Word using : Range(A1:C37).Copy appWD.Selection.PasteExcelTable
How do you go about copying a cell from one table to another using
I am copying code from website matplotlib and pasting into the Vim editor in
this code does not seem to work well always when copying currency data from
I am copying a value 8.03 from excel sheet and reading that value in
I'm reading a custom table cell in tableView:cellForRowAtIndexPath: from a nib file. This works
I have an Excel macro that is copying all of the information from a
I'm trying to transfer data from a DataSet into an Excel workbook. Unfortunately, I
I need to transfer a string from C# app into already opened Excel worksheet.
So I want to run through A1-C200 and paste everything into a Word document.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.