I am having some issues with some VBA code. What I am trying to do is export code from Excel cells and import them into a Word document in a text field.
Here is the code I have.
Sub test()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")
Dim objWord As Object
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
objWord.Documents.Open "C:\Users\Acer Windows 7\Desktop\test.docx"
With objWord.ActiveDocument
.Text1.Value = ws.Range("A1").Value
.Text2.Value = ws.Range("B1").Value
.Text3.Value = ws.Range("C1").Value
End With
End Sub
This code takes static cells and exports them into a Word document. What I need is a link or button on each row that will export that code from said row and put them into the word document.
Example if I click the link/button on row 4 it takes the data from C4, E4, F4
Is this possible? I am not sure how to do so.
If there are many rows it can be a bit cumbersome to add a button for each row. I suggest to only use 1 button that exports the selected row. So you only have to maintain 1 button/sub instead of many.
Cells(Row, Column) can be used here.
Sub test()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")
Dim objWord As Object
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
objWord.Documents.Open "C:\Users\Acer Windows 7\Desktop\test.docx"
With objWord.ActiveDocument
.Text1.Value = ws.Cells(Selection.Row, 1).Value
.Text2.Value = ws.Cells(Selection.Row, 2).Value
.Text3.Value = ws.Cells(Selection.Row, 3).Value
End With
End Sub
The code above would always use the row of cell which is selected. So first you select the row that you like to export and then you press the button that runs test().
Related
I'm trying to do something that seems so simple: Take the value that is in cell A1 in my worksheet and put it in a document at a certain point (using a bookmark).
I can open my document but I'm trying to make the cell a variable (string), and print it in/after the bookmark.
I have some coding experience from about 7 years ago so if it could be explained in the simplest way possible that'd be ideal.
The Cell is A1
Name of Bookmark is "Earth"
Value in the Cell is World
Here's my code so far
Private Sub CreateWordDoc()
Dim wdApp As Word.Application
Set wdApp = New Word.Application
Dim Cell As String
Range("A1").Value = Cell
With wdApp
.Visible = True
.Activate
.Documents.Add "C:\Users\Desktop\WordBookmarkTest.docx"
wdApp.Activate
End With
End Sub
I don't know if that makes sense. Like I said, my coding experience is from A Level Computer Science 7 years ago.
Like this:
Private Sub PopulateWordDoc()
Dim wdApp As Word.Application, doc As Word.Document, ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Values") 'for example
Set wdApp = New Word.Application
With wdApp
.Visible = True
.Activate
'get a reference to the opened document
Set doc = .Documents.Open("C:\Users\Desktop\WordBookmarkTest.docx")
'populate bookmark "Earth" in `doc`
AddTextToBookmark doc, "Earth", ws.Range("A1").Value
End With
End Sub
'Add text `txt` to bookmark `bmName` in document `doc`
'Adding the text deletes the bookmark, so recreate it
Sub AddTextToBookmark(doc As Word.Document, bmName As String, txt)
Dim rng As Word.Range
Set rng = doc.Bookmarks(bmName).Range 'get the range of the bookmark
rng.Text = txt 'add the text
doc.Bookmarks.Add bmName, rng 'recreate the bookmark
End Sub
how to insert a picture which is in an Excel Celle (C1) by VBA in a new created Word document in the header without formatting(no cell color)?
logo.copy
Set objHeader = myDoc.Sections(1).Headers(1).Range
objHeader.Paste
thank you!
Please, try the next way:
Sub InsertHeaderPict()
'copy picture from Excel (open session, active sheet):
Dim appExcel As Excel.Application, ws As Excel.Worksheet
Set appExcel = GetObject(, "Excel.Application")
Set ws = appExcel.ActiveWorkbook.ActiveSheet
ws.Shapes("Picture 1").CopyPicture xlScreen, xlBitmap 'use here your real picture name
'create a table of a row, 3 columns and paste the copied picture in its first cell:
Dim oSec As Word.Section, rng As Range
Set oSec = ActiveDocument.Sections(1)
Set rng = oSec.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
With rng
.Tables.Add Range:=rng, NumRows:=1, NumColumns:=3, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitWindow
With .Tables(1)
.Borders.InsideLineStyle = wdLineStyleNone
.Borders.OutsideLineStyle = wdLineStyleNone
.Rows.SetLeftIndent LeftIndent:=-37, RulerStyle:=wdAdjustNone
.Cell(1, 1).Range.PasteSpecial
End With
End With
End Sub
There must be a picture in the active sheet of the Excel open session. Use this real picture name instead of "Picture 1" and run the code.
I believe you have the right idea, just have to use copypicture and pastespecial
This is a snippet of my code that does basically the same thing I'm just using the shape object instead of a range.
Set reportHeader = masterReport.Sections.Item(1).Headers(wdHeaderFooterPrimary).Range
masterWorkbook.Worksheets("Template").Shapes("LogoSmall").CopyPicture
reportHeader.PasteSpecial
Thanks again to this post where I originally found this answer.
I'm designing an excel worksheet, where I can add the text from certain cells to a new word document if a condition for the each cell is met.
My code pastes the text from the cell to the new word document. But it always replaces the text from the previous cell. So only the last cell is visible. How can I change that?
Private Sub CommandButton1_Click()
Dim WrdApp As Word.Application
Dim WrdDoc As Word.Document
Set WrdApp = New Word.Application
WrdApp.Visible = True
WrdApp.Activate
Set WrdDoc = WrdApp.Documents.Add
a = Worksheets("Tabelle1").Cells(Rows.Count, 1).End(xlUp).Row
For i = 6 To a
If Worksheets("Tabelle1").Cells(i, 5).Value = "Ja" Then
Worksheets("Tabelle1").Cells(i, 4).Copy
WrdDoc.Paragraphs(1).Range.PasteSpecial xlPasteValues
End If
Next
Application.CutCopyMode = False
End Sub
Your problem is that you are potentially pasting 6 times into the exact same location, leading to the text at that location being replaced each time. You need to think about what you would do if you were doing this task without code, and then write code that does the same.
You could start by changing:
WrdDoc.Paragraphs(1).Range.PasteSpecial xlPasteValues
to
WrdDoc.Characters.Last.PasteSpecial xlPasteValues
But you will still need to add something between each value you paste.
I'm trying to copy data from one workbook into another workbook that is build as a survey.
In the survey form, we are using ActiveX controls for combo boxes and check boxes. I left two samples of ways I have tried (and failed).
Sub TransferData()
Set Source = Workbooks.Open("FromHere.xlsm")
Set qstnr = Workbooks.Open("ToHere.xlsx")
' Banner Form Classification
Source.Activate
Cells(8, 2).Copy
qstnr.Activate
Set Cbo_Classification = qstnr.OLEObjects("Cbo_Classification")
With Cbo_Classification.Object
.Text = "Not sure what to do here"
End With
' Reporting Organization
Source.Activate
Cells(9, 2).Copy
qstnr.Activate
'ActiveSheet.OLEObjects("Cbo_RptOrg").PasteSpecial Paste:=xlPasteValues
End Sub
EDIT: I have been able to get the object to be pasted into when working in the same workbook with the copy below. I don't understand why it's not successful when working outside the document.
Sub TransferObjects()
Dim wbk As Workbook: Set wbk = Workbooks.Open("CopyFrom.xlsm")
Dim tmplt As Workbook: Set tmplt = Workbooks.Open("CopyTo.xlsx")
Dim qstnr As Worksheet
Set qstnr = tmplt.Sheets("Sheet1")
qstnr.OLEObjects("Cbo_RptOrg").Object.Value = Range("K12").Value
End Sub
' Reporting Organization
Source.Activate
Dim Cbo_RptOrg As Variant
Cbo_RptOrg = Cells(2, 9).Value
qstnr.OLEObjects("Cbo_RptOrg").Object.Value = Cbo_RptOrg
This ended up working. Using a variable as suggested.
I am trying to amend a VBA macro to enable pasting of an Excel range (as a picture, for formatting purposes) to a Word bookmark.
Sub test2()
Dim objWord As Object
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("PREMIUMS")
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
objWord.Documents.Open "C:\TEST\BTM Macro Template.docx"
With objWord.ActiveDocument
.Bookmarks("PLAN_1_SHEET").Range.Text = ws.Range("A34").Value
.Bookmarks("PLAN_2_SHEET").Range.Text = ws.Range("BTM_PREM").Value
End With
Set objWord = Nothing
End Sub
The macro pastes a single cell text reference fine ("A34"), but using the same code for a range "BTM_PREM") returns a type mismatch error.
I know it is due to the range not being a string, but can't seem to identify how to amend this line to enable pasting of "BTM_PREM", as a picture, at the "PLAN_2_SHEET" bookmark.
.Bookmarks("PLAN_2_SHEET").Range.Text = ws.Range("BTM_PREM").Value
This is a piece of code that works for me:
ActiveWorkbook.Sheets("Lease 1").Range("B16:AF25").CopyPicture Appearance:=xlScreen, Format:=xlPicture
wdoc.Bookmarks("Bkmrk1").Range.Paste
Application.CutCopyMode = False
It's not a complete macro, just a part of it so you'll have to adjust a bit, But I think you get the idea.
you could use Copy() method on Excel Range object and then either Paste() or PasteSpecial() or PasteExcelTable() Word Range object methods, like follows:
ws.Range("BTM_PREM").Copy
.Bookmarks("PLAN_2_SHEET").Range.Paste
or
ws.Range("BTM_PREM").Copy
.Bookmarks("PLAN_2_SHEET").Range.PasteSpecial Link:=True
or
ws.Range("BTM_PREM").Copy
.Bookmarks("PLAN_2_SHEET").Range.PasteExcelTable LinkedToExcel:=True, WordFormatting:=False, RTF:=True