I want to control footer in Word file via Excel VBA.
Dim objWord
Dim objDoc
Dim Sec
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add
objWord.Visible = True
Set Sec = objDoc.Sections(1).Footers(wdHeaderFooterPrimary).PageNumbers.Add
I get "Object does'not support this property or method." alert
Where am I wrong.Please help.
Add the Microsoft Word XX.0 Object Library (I am on 16) from Tools > References in the VBA IDE
Sub createWordAddFooter()
Dim objWord As Word.Application
Dim objDoc As Word.Document
Set objWord = New Word.Application
Set objDoc = objWord.Documents.Add
objWord.Visible = True
objDoc.Sections(1).Footers(wdHeaderFooterPrimary).PageNumbers.Add
End Sub
Related
I'm creating a tool in Excel
Which is going to read in some data and the create a word document based on that data.
So far I've got excel to create the word document and add a few lines of text without any issue.
The next bit though to add a table is causing issues.
I can add the table in fine, but for some reason it deletes the lines of text that I added in the first place.
This is my code:
Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim objSelection As Object
Dim objRange As Object
Dim objTable As Object
Dim ctr as long
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.documents.Add
Set objSelection = objWord.Selection
Set objRange = objDoc.Range
'Adding some heading Text
objSelection.Style = objDoc.Styles("Heading 1")
objSelection.Font.Bold = True
objSelection.TypeText ("Heading Text")
objSelection.TypeParagraph
'Adding some normal Text
objSelection.Style = objDoc.Styles("Normal")
objSelection.Font.Bold = False
objSelection.TypeText ("Normal Text")
objSelection.TypeParagraph
Stop
'Adding the table
objDoc.Tables.Add objRange, 10, 2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitWindow
objWord.Quit SaveChanges:=False
Set objWord = Nothing
I put in a stop points after my heading and normal text are added and they appear in the word document fine.(screenshot below)
But as soon as the code reaches the Tables.Add bit, all my text disappears and the document has nothing but the table. (also screenshot below)
I looked around online and tried putting
objSelection.Collapse WdCollapseDirection.wdCollapseEnd
before the Tables.Add line of code, but that didn't help.
Your code to add a table fails because you are adding the table into objRange which you defined as the entire document.
You should also get into the habit of avoiding use of the Selection object, both in Word and Excel. Not only is it ineffecient (the screen has to be redrawn constantly) it is also error prone as the selection could be changed by the user to something you're not expecting.
The code below should work for you.
Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim objRange As Word.Range
Dim objTable As Word.Table
Dim ctr As Long
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.documents.Add
'Adding some heading Text
With objDoc.Paragraphs(1).Range
.Style = objDoc.Styles(wdStyleHeading1)
.Font.Bold = True
.Text = "Heading Text"
.InsertParagraphAfter
End With
'Adding some normal Text
With objDoc.Paragraphs(2).Range
.Style = objDoc.Styles(wdStyleNormal)
.Font.Bold = False
.Text = "Normal Text"
.InsertParagraphAfter
End With
Set objRange = objDoc.Paragraphs.Last.Range
'Adding the table
Set objTable = objDoc.Tables.Add(objRange, 10, 2, _
DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitWindow)
objWord.Quit SaveChanges:=False
Set objWord = Nothing
I did the test with the code below and it works :
Pre requisite : add reference "Microsoft Word xx.x Object Library" in your VBA project
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
' create an instance of MS Word
Set WordApp = CreateObject("word.application")
WordApp.Visible = True
Set WordDoc = WordApp.Documents.Add
Range("A1:A2").Copy
WordApp.Selection.TypeText ("Here are my comment")
WordApp.Selection.Paste
' fit the table with window
WordDoc.Tables(1).AutoFitBehavior wdAutoFitWindow
' Save the content into the .doc file
WordDoc.SaveAs2 ("C:\mypath\myDocument.doc")
Excel provides me a customized text in cell A1, which then should be passed on to a word letter at a specific section (after a bookmark).
Just started with VBA and found+edited a code that was able to provide half of the solution. Where I struggle is how to insert the text in cell A1 in sheet OutputText to the word document after the bookmark?
Here is my code so far:
Function FnBookMarkInsertAfter()
Dim objWord
Dim objDoc
Dim objRange
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open("C:\Users\[...]")
objWord.Visible = True
Set objRange = objDoc.Bookmarks("bookmark_1").Range
objRange.InsertAfter ("Cell A1 from Sheet OutputText")
End Function
Thank you!
Would this work for you? (I can't take credit for the code):
Sub test()
Dim objWord As Object
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets(1)
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
objWord.Documents.Open "C:\test.docx" ' change as required
With objWord.ActiveDocument
.Bookmarks("bookmark_1").Range.Text = ws.Range("A1").Value
End With
Set objWord = Nothing
End Sub
I'd like to change the view in Word from my Excel macro (which creates the Word document).
I'd like to execute: ActiveWindow.View.Type = wdWebView
In my Excel macro, I have:
Dim objWord
Dim objDoc
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add
As mentioned in the comments, you're mixing late-binding and early-binding, and also need to reference the Word instance.
An early-binding approach might be (add a reference to the Microsoft Word xx.0 Object Library under Tools > References).
Sub MyWord()
Dim wordApp As New Word.Application
Dim myDoc As Word.Document
wordApp.Visible = True
Set myDoc = wordApp.Documents.Add
wordApp.ActiveWindow.View.Type = wdWebView
End Sub
If you want to late-bind, note from the WdViewType enum docs that wdWebView corresponds to the value 6.
Sub MyWord()
Const wdWebView As Long = 6
Dim objWord As Object
Dim objDoc As Object
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Add
objWord.ActiveWindow.View.Type = wdWebView
End Sub
You don't have to use early binding to use wdWebView. Instead, you could use:
Dim objWord As Object, objDoc As Object
Const wdWebView As Long = 6
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add
objWord.ActiveWindow.View.Type = wdWebView
In EXCEL, I have some VBA codes to open a Word Document A and copy its content from certain page to a new document. Currently, I can copy its text. I am wondering how to copy both context and formatting. Below is my current code and I appreciate any suggestions!
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add
objWord.Visible = True
Set objSelection = objWord.Selection
'Prepare Document B
objDoc.SaveAs (Folderpath to Document B)
Set objTempWord = CreateObject("Word.Application")
Set tempDoc = objWord.Documents.Open(Folderpath to Document A)
'copy context from Document A
With tempDoc.Application
.Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Name:="2"
.Selection.EndKey Unit:=wdStory, Extend:=wdExtend
.Selection.Copy
End With
objSelection.TypeParagraph
objSelection.Paste
objSelection.InsertBreak Type:=wdSectionBreakNextPage
tempDoc.Close
objDoc.Application.Statusbar = False
objDoc.Save
This here does the same, without the superfluous extra Application object and without the use of Selection:
Dim objWord As Word.Application
Dim objDoc As Word.Document, newDoc As Word.Document
Dim r As Word.Range, r2 As Word.Range
Set objWord = CreateObject("Word.Application") 'or Set objWord = new Word.Application
Set objDoc = objWord.Documents.Open(FolderpathToDocumentA)
Set newDoc = objWord.Documents.Add
newDoc.SaveAs FolderpathToDocumentB
Set r = objDoc.GoTo(what:=wdGoToPage, which:=wdGoToAbsolute, Name:=2)
r.End = objDoc.Range.End
'copy context from Document A
r.Copy
newDoc.Content.InsertBreak Type:=wdSectionBreakNextPage
newDoc.Range(newDoc.Content.Start, newDoc.Content.Start).Paste
newDoc.Content.InsertBefore vbCrLf
newDoc.Save
objWord.Quit
Does that do what you need?
I wrote below code to take screenshot form Excel document and paste on word document.It works fine However I am unable to convert This Word document into PDF and error displays"Object does not support this property or method".It seems that i defined Variable (Objword as variant) not correct.Please any one can help.
Sub CopyToWordPicture()
Dim WDApp As Word.Application
Dim WDDoc As Word.Document
Dim objword As variant
Set objword = CreateObject("Word.Application")
objword.Visible = True
objword.Documents.Open "C:\Automation\BH Report\Daily&BH RAN KPI report_ok.docx"
Workbooks.Open Filename:="C:\Automation\BH Report\Daily_Hourly KPI Template.xlsx"
Sheets("BH_Graphs").Select
Range("A1:k50").CopyPicture xlPrinter
With objword
'.Documents.Add
.Selection.Paste
.Visible = True
End With
'WDApp.Visible = True
' WDApp.Selection.GoToNext wdGoToPage
Windows("Daily_Hourly KPI Template.xlsx").Activate
Sheets("Daily_Graphs").Select
Range("A1:J50").CopyPicture xlPrinter
With objword
'.Documents.Add
.Selection.Paste
.Visible = True
End With
'export as PDF
objword.ExportAsFixedFormat OutputFileName:="C:\Automation\BH Report\Daily&BH RAN KPI report.pdf", _
ExportFormat:=wdExportFormatPDF
end sub