Excel VBA copy range from Excel and paste it to Word header Text Box












0














I have Excel Workbook from where I am running following code below. I have logo and page numbering already in Word document so I do not need to paste the whole range from Excel. I have two Text Boxes where data from spreadsheet should be inserted.




  1. I need to copy Worksheets("Other Data").Range("A58:A60") and paste it to "Text Box 1" that I have in Word documents header. Three sentances on different rows. Text Box should be wrapped?


  2. I need to copy Worksheets("Other Data").Range("A68") and paste it to "Text Box 2" that I have in Word documents header. One sentance.


  3. AutoFitWindows doesn't work. There have to be something with variables but I can't figure what exactly is wrong. Tried different ways with no success.



Here is my code:



Sub excelToWord_click()

Dim head As Excel.Range
Dim foot As Excel.Range
Dim WordTable As Word.Table
Set wdApp = CreateObject("Word.Application")
wdApp.Documents.Open FileName:=ThisWorkbook.Path & "" & "MyDOC" & ".docx"
wdApp.Visible = True

Set head = ThisWorkbook.Worksheets("Other Data").Range("A58:A60")

head.Copy

'|| I need to paste copied cells to "Text Box 1" in my Word document ||'

With wdApp.ActiveDocument.Sections(1)
.Headers(wdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Shapes("Text Box 1").Activate
head.Paste
End With

'|| ---------------------------------------------------------------- ||'

Set head2 = ThisWorkbook.Worksheets("Other Data").Range("A68")

head2.Copy

'|| I need to paste copied cells to "Text Box 2" in my Word document ||'

With wdApp.ActiveDocument.Sections(1)
.Headers(wdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Shapes("Text Box 2").Activate
head2.Paste
End With

'|| ---------------------------------------------------------------- ||'

Set foot = ThisWorkbook.Worksheets("Other Data").Range("A62:H65")
foot.Copy

With wdApp.ActiveDocument.Sections(1)
.Footers(wdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Paste
End With

'|| Autofit table to page in Footer ||'

WordTable.AutoFitBehavior (wdAutoFitWindow)

'|| ---------------------------------------------------------------- ||'

'restore Word
If wdApp.ActiveWindow.View.SplitSpecial <> 0 Then
wdApp.ActiveWindow.Panes(2).Close
End If
If wdApp.ActiveWindow.ActivePane.View.Type = 1 _
Or wdApp.ActiveWindow.ActivePane.View.Type = 2 Then
wdApp.ActiveWindow.ActivePane.View.Type = 3
End If
wdApp.WordBasic.AcceptAllChangesInDoc
'wdApp.ActiveDocument.PrintOut, Copies:=1

wdApp.ActiveDocument.ExportAsFixedFormat outputfilename:=ThisWorkbook.Path & "" & Sheets("MAIN").Range("D14").Value & ", " & Sheets("MAIN").Range("D11").Value & "_" & "Document" & "_" & ".pdf", exportformat:=wdExportFormatPDF

wdApp.ActiveDocument.SaveAs ThisWorkbook.Path & "" & Worksheets("MAIN").Range("D14").Value & ", " & Worksheets("MAIN").Range("D11").Value & "_" & "Document" & "_" & ".docx"

wdApp.Quit '<--| quit Word
Set wdApp = Nothing '<--| release object variable
'wdApp.ActiveWindow.Close savechanges:=False
End Sub









share|improve this question
























  • I recommend to activate Option Explicit: In the VBA editor go to ToolsOptionsRequire Variable Declaration and you will see your issue immediately.
    – Pᴇʜ
    Nov 20 at 14:37












  • @PEH Thanks for the edit.
    – Freeflow
    Nov 20 at 14:47










  • @user7202022 Its also helpful to check that syntax checking is enabled. In the VB IDE goto Tools.Options.Editor and make sure that all the check boxes in the Code Settings pane are ticked. Some people quibble about the need for the Auto syntax check box as it can be annoying but its a starting point. Turn it off later if it annoys you.
    – Freeflow
    Nov 20 at 14:50










  • Cross-posted at: mrexcel.com/forum/general-excel-discussion-other-questions/…. For cross-posting etiquette, please read: excelguru.ca/content.php?184
    – macropod
    Nov 21 at 0:58
















0














I have Excel Workbook from where I am running following code below. I have logo and page numbering already in Word document so I do not need to paste the whole range from Excel. I have two Text Boxes where data from spreadsheet should be inserted.




  1. I need to copy Worksheets("Other Data").Range("A58:A60") and paste it to "Text Box 1" that I have in Word documents header. Three sentances on different rows. Text Box should be wrapped?


  2. I need to copy Worksheets("Other Data").Range("A68") and paste it to "Text Box 2" that I have in Word documents header. One sentance.


  3. AutoFitWindows doesn't work. There have to be something with variables but I can't figure what exactly is wrong. Tried different ways with no success.



Here is my code:



Sub excelToWord_click()

Dim head As Excel.Range
Dim foot As Excel.Range
Dim WordTable As Word.Table
Set wdApp = CreateObject("Word.Application")
wdApp.Documents.Open FileName:=ThisWorkbook.Path & "" & "MyDOC" & ".docx"
wdApp.Visible = True

Set head = ThisWorkbook.Worksheets("Other Data").Range("A58:A60")

head.Copy

'|| I need to paste copied cells to "Text Box 1" in my Word document ||'

With wdApp.ActiveDocument.Sections(1)
.Headers(wdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Shapes("Text Box 1").Activate
head.Paste
End With

'|| ---------------------------------------------------------------- ||'

Set head2 = ThisWorkbook.Worksheets("Other Data").Range("A68")

head2.Copy

'|| I need to paste copied cells to "Text Box 2" in my Word document ||'

With wdApp.ActiveDocument.Sections(1)
.Headers(wdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Shapes("Text Box 2").Activate
head2.Paste
End With

'|| ---------------------------------------------------------------- ||'

Set foot = ThisWorkbook.Worksheets("Other Data").Range("A62:H65")
foot.Copy

With wdApp.ActiveDocument.Sections(1)
.Footers(wdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Paste
End With

'|| Autofit table to page in Footer ||'

WordTable.AutoFitBehavior (wdAutoFitWindow)

'|| ---------------------------------------------------------------- ||'

'restore Word
If wdApp.ActiveWindow.View.SplitSpecial <> 0 Then
wdApp.ActiveWindow.Panes(2).Close
End If
If wdApp.ActiveWindow.ActivePane.View.Type = 1 _
Or wdApp.ActiveWindow.ActivePane.View.Type = 2 Then
wdApp.ActiveWindow.ActivePane.View.Type = 3
End If
wdApp.WordBasic.AcceptAllChangesInDoc
'wdApp.ActiveDocument.PrintOut, Copies:=1

wdApp.ActiveDocument.ExportAsFixedFormat outputfilename:=ThisWorkbook.Path & "" & Sheets("MAIN").Range("D14").Value & ", " & Sheets("MAIN").Range("D11").Value & "_" & "Document" & "_" & ".pdf", exportformat:=wdExportFormatPDF

wdApp.ActiveDocument.SaveAs ThisWorkbook.Path & "" & Worksheets("MAIN").Range("D14").Value & ", " & Worksheets("MAIN").Range("D11").Value & "_" & "Document" & "_" & ".docx"

wdApp.Quit '<--| quit Word
Set wdApp = Nothing '<--| release object variable
'wdApp.ActiveWindow.Close savechanges:=False
End Sub









share|improve this question
























  • I recommend to activate Option Explicit: In the VBA editor go to ToolsOptionsRequire Variable Declaration and you will see your issue immediately.
    – Pᴇʜ
    Nov 20 at 14:37












  • @PEH Thanks for the edit.
    – Freeflow
    Nov 20 at 14:47










  • @user7202022 Its also helpful to check that syntax checking is enabled. In the VB IDE goto Tools.Options.Editor and make sure that all the check boxes in the Code Settings pane are ticked. Some people quibble about the need for the Auto syntax check box as it can be annoying but its a starting point. Turn it off later if it annoys you.
    – Freeflow
    Nov 20 at 14:50










  • Cross-posted at: mrexcel.com/forum/general-excel-discussion-other-questions/…. For cross-posting etiquette, please read: excelguru.ca/content.php?184
    – macropod
    Nov 21 at 0:58














0












0








0







I have Excel Workbook from where I am running following code below. I have logo and page numbering already in Word document so I do not need to paste the whole range from Excel. I have two Text Boxes where data from spreadsheet should be inserted.




  1. I need to copy Worksheets("Other Data").Range("A58:A60") and paste it to "Text Box 1" that I have in Word documents header. Three sentances on different rows. Text Box should be wrapped?


  2. I need to copy Worksheets("Other Data").Range("A68") and paste it to "Text Box 2" that I have in Word documents header. One sentance.


  3. AutoFitWindows doesn't work. There have to be something with variables but I can't figure what exactly is wrong. Tried different ways with no success.



Here is my code:



Sub excelToWord_click()

Dim head As Excel.Range
Dim foot As Excel.Range
Dim WordTable As Word.Table
Set wdApp = CreateObject("Word.Application")
wdApp.Documents.Open FileName:=ThisWorkbook.Path & "" & "MyDOC" & ".docx"
wdApp.Visible = True

Set head = ThisWorkbook.Worksheets("Other Data").Range("A58:A60")

head.Copy

'|| I need to paste copied cells to "Text Box 1" in my Word document ||'

With wdApp.ActiveDocument.Sections(1)
.Headers(wdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Shapes("Text Box 1").Activate
head.Paste
End With

'|| ---------------------------------------------------------------- ||'

Set head2 = ThisWorkbook.Worksheets("Other Data").Range("A68")

head2.Copy

'|| I need to paste copied cells to "Text Box 2" in my Word document ||'

With wdApp.ActiveDocument.Sections(1)
.Headers(wdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Shapes("Text Box 2").Activate
head2.Paste
End With

'|| ---------------------------------------------------------------- ||'

Set foot = ThisWorkbook.Worksheets("Other Data").Range("A62:H65")
foot.Copy

With wdApp.ActiveDocument.Sections(1)
.Footers(wdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Paste
End With

'|| Autofit table to page in Footer ||'

WordTable.AutoFitBehavior (wdAutoFitWindow)

'|| ---------------------------------------------------------------- ||'

'restore Word
If wdApp.ActiveWindow.View.SplitSpecial <> 0 Then
wdApp.ActiveWindow.Panes(2).Close
End If
If wdApp.ActiveWindow.ActivePane.View.Type = 1 _
Or wdApp.ActiveWindow.ActivePane.View.Type = 2 Then
wdApp.ActiveWindow.ActivePane.View.Type = 3
End If
wdApp.WordBasic.AcceptAllChangesInDoc
'wdApp.ActiveDocument.PrintOut, Copies:=1

wdApp.ActiveDocument.ExportAsFixedFormat outputfilename:=ThisWorkbook.Path & "" & Sheets("MAIN").Range("D14").Value & ", " & Sheets("MAIN").Range("D11").Value & "_" & "Document" & "_" & ".pdf", exportformat:=wdExportFormatPDF

wdApp.ActiveDocument.SaveAs ThisWorkbook.Path & "" & Worksheets("MAIN").Range("D14").Value & ", " & Worksheets("MAIN").Range("D11").Value & "_" & "Document" & "_" & ".docx"

wdApp.Quit '<--| quit Word
Set wdApp = Nothing '<--| release object variable
'wdApp.ActiveWindow.Close savechanges:=False
End Sub









share|improve this question















I have Excel Workbook from where I am running following code below. I have logo and page numbering already in Word document so I do not need to paste the whole range from Excel. I have two Text Boxes where data from spreadsheet should be inserted.




  1. I need to copy Worksheets("Other Data").Range("A58:A60") and paste it to "Text Box 1" that I have in Word documents header. Three sentances on different rows. Text Box should be wrapped?


  2. I need to copy Worksheets("Other Data").Range("A68") and paste it to "Text Box 2" that I have in Word documents header. One sentance.


  3. AutoFitWindows doesn't work. There have to be something with variables but I can't figure what exactly is wrong. Tried different ways with no success.



Here is my code:



Sub excelToWord_click()

Dim head As Excel.Range
Dim foot As Excel.Range
Dim WordTable As Word.Table
Set wdApp = CreateObject("Word.Application")
wdApp.Documents.Open FileName:=ThisWorkbook.Path & "" & "MyDOC" & ".docx"
wdApp.Visible = True

Set head = ThisWorkbook.Worksheets("Other Data").Range("A58:A60")

head.Copy

'|| I need to paste copied cells to "Text Box 1" in my Word document ||'

With wdApp.ActiveDocument.Sections(1)
.Headers(wdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Shapes("Text Box 1").Activate
head.Paste
End With

'|| ---------------------------------------------------------------- ||'

Set head2 = ThisWorkbook.Worksheets("Other Data").Range("A68")

head2.Copy

'|| I need to paste copied cells to "Text Box 2" in my Word document ||'

With wdApp.ActiveDocument.Sections(1)
.Headers(wdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Shapes("Text Box 2").Activate
head2.Paste
End With

'|| ---------------------------------------------------------------- ||'

Set foot = ThisWorkbook.Worksheets("Other Data").Range("A62:H65")
foot.Copy

With wdApp.ActiveDocument.Sections(1)
.Footers(wdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Paste
End With

'|| Autofit table to page in Footer ||'

WordTable.AutoFitBehavior (wdAutoFitWindow)

'|| ---------------------------------------------------------------- ||'

'restore Word
If wdApp.ActiveWindow.View.SplitSpecial <> 0 Then
wdApp.ActiveWindow.Panes(2).Close
End If
If wdApp.ActiveWindow.ActivePane.View.Type = 1 _
Or wdApp.ActiveWindow.ActivePane.View.Type = 2 Then
wdApp.ActiveWindow.ActivePane.View.Type = 3
End If
wdApp.WordBasic.AcceptAllChangesInDoc
'wdApp.ActiveDocument.PrintOut, Copies:=1

wdApp.ActiveDocument.ExportAsFixedFormat outputfilename:=ThisWorkbook.Path & "" & Sheets("MAIN").Range("D14").Value & ", " & Sheets("MAIN").Range("D11").Value & "_" & "Document" & "_" & ".pdf", exportformat:=wdExportFormatPDF

wdApp.ActiveDocument.SaveAs ThisWorkbook.Path & "" & Worksheets("MAIN").Range("D14").Value & ", " & Worksheets("MAIN").Range("D11").Value & "_" & "Document" & "_" & ".docx"

wdApp.Quit '<--| quit Word
Set wdApp = Nothing '<--| release object variable
'wdApp.ActiveWindow.Close savechanges:=False
End Sub






excel vba ms-word header






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 14:40









Pᴇʜ

20.2k42650




20.2k42650










asked Nov 20 at 14:27









user7202022

1288




1288












  • I recommend to activate Option Explicit: In the VBA editor go to ToolsOptionsRequire Variable Declaration and you will see your issue immediately.
    – Pᴇʜ
    Nov 20 at 14:37












  • @PEH Thanks for the edit.
    – Freeflow
    Nov 20 at 14:47










  • @user7202022 Its also helpful to check that syntax checking is enabled. In the VB IDE goto Tools.Options.Editor and make sure that all the check boxes in the Code Settings pane are ticked. Some people quibble about the need for the Auto syntax check box as it can be annoying but its a starting point. Turn it off later if it annoys you.
    – Freeflow
    Nov 20 at 14:50










  • Cross-posted at: mrexcel.com/forum/general-excel-discussion-other-questions/…. For cross-posting etiquette, please read: excelguru.ca/content.php?184
    – macropod
    Nov 21 at 0:58


















  • I recommend to activate Option Explicit: In the VBA editor go to ToolsOptionsRequire Variable Declaration and you will see your issue immediately.
    – Pᴇʜ
    Nov 20 at 14:37












  • @PEH Thanks for the edit.
    – Freeflow
    Nov 20 at 14:47










  • @user7202022 Its also helpful to check that syntax checking is enabled. In the VB IDE goto Tools.Options.Editor and make sure that all the check boxes in the Code Settings pane are ticked. Some people quibble about the need for the Auto syntax check box as it can be annoying but its a starting point. Turn it off later if it annoys you.
    – Freeflow
    Nov 20 at 14:50










  • Cross-posted at: mrexcel.com/forum/general-excel-discussion-other-questions/…. For cross-posting etiquette, please read: excelguru.ca/content.php?184
    – macropod
    Nov 21 at 0:58
















I recommend to activate Option Explicit: In the VBA editor go to ToolsOptionsRequire Variable Declaration and you will see your issue immediately.
– Pᴇʜ
Nov 20 at 14:37






I recommend to activate Option Explicit: In the VBA editor go to ToolsOptionsRequire Variable Declaration and you will see your issue immediately.
– Pᴇʜ
Nov 20 at 14:37














@PEH Thanks for the edit.
– Freeflow
Nov 20 at 14:47




@PEH Thanks for the edit.
– Freeflow
Nov 20 at 14:47












@user7202022 Its also helpful to check that syntax checking is enabled. In the VB IDE goto Tools.Options.Editor and make sure that all the check boxes in the Code Settings pane are ticked. Some people quibble about the need for the Auto syntax check box as it can be annoying but its a starting point. Turn it off later if it annoys you.
– Freeflow
Nov 20 at 14:50




@user7202022 Its also helpful to check that syntax checking is enabled. In the VB IDE goto Tools.Options.Editor and make sure that all the check boxes in the Code Settings pane are ticked. Some people quibble about the need for the Auto syntax check box as it can be annoying but its a starting point. Turn it off later if it annoys you.
– Freeflow
Nov 20 at 14:50












Cross-posted at: mrexcel.com/forum/general-excel-discussion-other-questions/…. For cross-posting etiquette, please read: excelguru.ca/content.php?184
– macropod
Nov 21 at 0:58




Cross-posted at: mrexcel.com/forum/general-excel-discussion-other-questions/…. For cross-posting etiquette, please read: excelguru.ca/content.php?184
– macropod
Nov 21 at 0:58












1 Answer
1






active

oldest

votes


















1














Your problem is because you are late binding your word application object rather than installing the Word reference to the VBA IDE.
This means that any references to word constants without qualification to the variable you are using for your word app will be interpreted as the default (0 or Null) value.



The simplest way to resolve this issue is in the VBA IDE; goto Tools.References and make sure that the check box next to Microsoft Word ...... is ticked.



If you would prefer to qualify your variables then you need to change word constants so that they are prefixed with WdApp, your variable for the Word Application.



e.g. wdApp.wdHeaderFooterIndex.wdHeaderFooterPrimary



With the Word reference installed you can just say



wdHeaderFooterPrimary.






share|improve this answer























  • I have enabled MS Word reference in Excel but I can't get Excel range to "Text Box 1". It says .Headers(wdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Shapes("Text Box 1").Activate - Object Not Found...
    – user7202022
    Nov 20 at 15:01












  • Head.paste will paste whatever is on the clipboard to the excel range defined by head. It will not paste into the text box. To transfer information into the text box you will need to assign the content of the head range to the text property of the text box. To do this you are likely to need to use ShapeRange("Text Box 1").TextFrame.TextRange.Text = Head.Values2 (but I can't be certain that's the exact correct syntax).
    – Freeflow
    Nov 20 at 15:13













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53395173%2fexcel-vba-copy-range-from-excel-and-paste-it-to-word-header-text-box%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Your problem is because you are late binding your word application object rather than installing the Word reference to the VBA IDE.
This means that any references to word constants without qualification to the variable you are using for your word app will be interpreted as the default (0 or Null) value.



The simplest way to resolve this issue is in the VBA IDE; goto Tools.References and make sure that the check box next to Microsoft Word ...... is ticked.



If you would prefer to qualify your variables then you need to change word constants so that they are prefixed with WdApp, your variable for the Word Application.



e.g. wdApp.wdHeaderFooterIndex.wdHeaderFooterPrimary



With the Word reference installed you can just say



wdHeaderFooterPrimary.






share|improve this answer























  • I have enabled MS Word reference in Excel but I can't get Excel range to "Text Box 1". It says .Headers(wdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Shapes("Text Box 1").Activate - Object Not Found...
    – user7202022
    Nov 20 at 15:01












  • Head.paste will paste whatever is on the clipboard to the excel range defined by head. It will not paste into the text box. To transfer information into the text box you will need to assign the content of the head range to the text property of the text box. To do this you are likely to need to use ShapeRange("Text Box 1").TextFrame.TextRange.Text = Head.Values2 (but I can't be certain that's the exact correct syntax).
    – Freeflow
    Nov 20 at 15:13


















1














Your problem is because you are late binding your word application object rather than installing the Word reference to the VBA IDE.
This means that any references to word constants without qualification to the variable you are using for your word app will be interpreted as the default (0 or Null) value.



The simplest way to resolve this issue is in the VBA IDE; goto Tools.References and make sure that the check box next to Microsoft Word ...... is ticked.



If you would prefer to qualify your variables then you need to change word constants so that they are prefixed with WdApp, your variable for the Word Application.



e.g. wdApp.wdHeaderFooterIndex.wdHeaderFooterPrimary



With the Word reference installed you can just say



wdHeaderFooterPrimary.






share|improve this answer























  • I have enabled MS Word reference in Excel but I can't get Excel range to "Text Box 1". It says .Headers(wdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Shapes("Text Box 1").Activate - Object Not Found...
    – user7202022
    Nov 20 at 15:01












  • Head.paste will paste whatever is on the clipboard to the excel range defined by head. It will not paste into the text box. To transfer information into the text box you will need to assign the content of the head range to the text property of the text box. To do this you are likely to need to use ShapeRange("Text Box 1").TextFrame.TextRange.Text = Head.Values2 (but I can't be certain that's the exact correct syntax).
    – Freeflow
    Nov 20 at 15:13
















1












1








1






Your problem is because you are late binding your word application object rather than installing the Word reference to the VBA IDE.
This means that any references to word constants without qualification to the variable you are using for your word app will be interpreted as the default (0 or Null) value.



The simplest way to resolve this issue is in the VBA IDE; goto Tools.References and make sure that the check box next to Microsoft Word ...... is ticked.



If you would prefer to qualify your variables then you need to change word constants so that they are prefixed with WdApp, your variable for the Word Application.



e.g. wdApp.wdHeaderFooterIndex.wdHeaderFooterPrimary



With the Word reference installed you can just say



wdHeaderFooterPrimary.






share|improve this answer














Your problem is because you are late binding your word application object rather than installing the Word reference to the VBA IDE.
This means that any references to word constants without qualification to the variable you are using for your word app will be interpreted as the default (0 or Null) value.



The simplest way to resolve this issue is in the VBA IDE; goto Tools.References and make sure that the check box next to Microsoft Word ...... is ticked.



If you would prefer to qualify your variables then you need to change word constants so that they are prefixed with WdApp, your variable for the Word Application.



e.g. wdApp.wdHeaderFooterIndex.wdHeaderFooterPrimary



With the Word reference installed you can just say



wdHeaderFooterPrimary.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 at 14:41









Pᴇʜ

20.2k42650




20.2k42650










answered Nov 20 at 14:35









Freeflow

486128




486128












  • I have enabled MS Word reference in Excel but I can't get Excel range to "Text Box 1". It says .Headers(wdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Shapes("Text Box 1").Activate - Object Not Found...
    – user7202022
    Nov 20 at 15:01












  • Head.paste will paste whatever is on the clipboard to the excel range defined by head. It will not paste into the text box. To transfer information into the text box you will need to assign the content of the head range to the text property of the text box. To do this you are likely to need to use ShapeRange("Text Box 1").TextFrame.TextRange.Text = Head.Values2 (but I can't be certain that's the exact correct syntax).
    – Freeflow
    Nov 20 at 15:13




















  • I have enabled MS Word reference in Excel but I can't get Excel range to "Text Box 1". It says .Headers(wdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Shapes("Text Box 1").Activate - Object Not Found...
    – user7202022
    Nov 20 at 15:01












  • Head.paste will paste whatever is on the clipboard to the excel range defined by head. It will not paste into the text box. To transfer information into the text box you will need to assign the content of the head range to the text property of the text box. To do this you are likely to need to use ShapeRange("Text Box 1").TextFrame.TextRange.Text = Head.Values2 (but I can't be certain that's the exact correct syntax).
    – Freeflow
    Nov 20 at 15:13


















I have enabled MS Word reference in Excel but I can't get Excel range to "Text Box 1". It says .Headers(wdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Shapes("Text Box 1").Activate - Object Not Found...
– user7202022
Nov 20 at 15:01






I have enabled MS Word reference in Excel but I can't get Excel range to "Text Box 1". It says .Headers(wdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Shapes("Text Box 1").Activate - Object Not Found...
– user7202022
Nov 20 at 15:01














Head.paste will paste whatever is on the clipboard to the excel range defined by head. It will not paste into the text box. To transfer information into the text box you will need to assign the content of the head range to the text property of the text box. To do this you are likely to need to use ShapeRange("Text Box 1").TextFrame.TextRange.Text = Head.Values2 (but I can't be certain that's the exact correct syntax).
– Freeflow
Nov 20 at 15:13






Head.paste will paste whatever is on the clipboard to the excel range defined by head. It will not paste into the text box. To transfer information into the text box you will need to assign the content of the head range to the text property of the text box. To do this you are likely to need to use ShapeRange("Text Box 1").TextFrame.TextRange.Text = Head.Values2 (but I can't be certain that's the exact correct syntax).
– Freeflow
Nov 20 at 15:13




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53395173%2fexcel-vba-copy-range-from-excel-and-paste-it-to-word-header-text-box%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin