What I've done so far :-
1. VBA script to open a template word document. copy the value from column(A1) and paste it in a word document by using the find function.
2. I have done this with two columns for two different locations in a word document.
3. Then, I have saved the document using the value from column(A1).
What I wish to do:-
1. Right now, this runs only for one word document.
2. I wish to run a loop such that I can copy each row into a word document, save it and move onto the next row and save the template word document with a unique name(i.e, from column A)
3. based on the code below, I'm guessing I need two loops, I just can't figure it out and would love some help.
Code I've written so far:-
1. some of them return the function call and didn't think it would be useful, thus I didn't paste it.
Set appWd = CreateObject("Word.Application")
appWd.Visible = True
Set docWD = appWd.Documents.Open("C:\Users\Draft.docx")
'Select Sheet where copying from in excel
Set sheet1 = Sheets("Sheet1")
Set wdFind = appWd.Selection.Find
ClipT = " "
ClipEmpty.SetText ClipT
sheet1.Range("A1").Copy
wdFind.Text = "DateHere"
Call FormatPaste
sheet1.Range("B1").Copy
wdFind.Text = "EntryHere"
Call FormatPaste
saveCell1 = sheet1.Range("B1").Text
dir2 = "C:\Users\" & saveCell1
docWD.SaveAs (dir2 & ".docx")
Related
I am trying to copy a bunch of text data from an excel spreadsheet into multiple separate word documents (one excel row = one document, but each column's heading has to be included before the text from the respective field of the row). I also want these documents' names to be the text from specific fields in the spreadsheet (row headers).
Because I want fancy formatting, and a specific order of copying/pasting things (not all fields are included), I am using a word template with bookmarks that I can feed into VBA. It fills the template well, but I cannot save it as a standard word document before repeating the loop. I get the error 'Object doesn't support this property or method'. Is there a way to overcome it, or a more elegant method I've missed?
Here is the code:
Sub Primitive()
Dim objWord As Object
Dim ws As Worksheet
Dim i As Integer
Set ws = ThisWorkbook.Sheets("Sheet1")
i = 2 ' First row to process
'Start of loop
Do Until ws.Range("B" & i) = ""
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
'Change to local path of template file
objWord.Documents.Open "C:path of template file.dotx"
With objWord.ActiveDocument
.Bookmarks("FirstBookmark").Range.Text = ws.Range("B" & i).Value & " " & ws.Range("C" & i)
.Bookmarks("headlineZ").Range.Text = ws.Range("Z1").Value
lots of this^ here to arrange the data right in the document
NewFileName = "C:\path of where I want the new file" & ws.Range("C" & i).Value & ".docx"
This is the line that gives the error 483:
objWord.SaveAs2 Filename:="NewFileName"
End With
objWord.Close
Set objWord = Nothing
i = i + 1
Loop
End Sub
You are trying to save Word, rather than the document. If you replace this
objWord.SaveAs2 Filename:="NewFileName"
with this
objWord.ActiveDocument.SaveAs2 Filename:="NewFileName"
it should work better. That said, you should not use ActiveDocument in your macros. Consider replacing
objWord.Documents.Open "C:path of template file.dotx"
with something like
Set TargetDocument = objWord.Documents.Open("C:path of template file.dotx")
and replace all ActiveDocument with TargetDocument.
Long story short, I need to maintain a reference table that maps criteria to the slides where these criteria are addressed. Obviously, this is painful since people will be updating PowerPoints until the very last minute.
My goal is to have a macro that can do the following:
I put all PowerPoint files in a given folder (we'll it .../ImportMe)
Run a script that opens each PowerPoint file.
Makes a list of the Slide#'s in column B
Find the Text Box I'm looking for (These all start with "CT:"). Copy the text and paste it into column C for that respective row.
Put the PowerPoint file name in column A for all applicable rows.
I.e. the below...
I have created code that can get me the slide numbers (and I could get the file name, although I have not done it yet). I'm struggling with copying and pasting the data from a specific textbox (or at all, really) -- this is the portion I want to focus on at the moment...
Set xlSheet = Excel.Application.ActiveWorkbook.Sheets("Reference Table")
pptpath = "C:\Users\Username\ImportMe"
Set PP = CreateObject("PowerPoint.Application")
Set pptPres = PP.Presentations.Open(pptpath)
PP.Visible = msoCTrue
For Each pptSlide In pptPres.Slides
'Find new last row
LastRow = xlSheet.Range("B" & xlSheet.Rows.Count).End(xlUp).Row
NewRow = LastRow + 1
xlSheet.Range("B" & NewRow).Value = pptSlide.SlideNumber
For Each pptShape In pptSlide.Shapes
If pptShape.TextFrame2.HasText Then
pptText = pptShape.TextFrame2.TextRange.Text
If InStr(1, pptText, "CT:") > 0 Then
'pptShape.TextFrame2.TextRange.Copy
xlSheet.Range("C" & NewRow).Value = pptText
Else
'Do Nothing
End If
Else
'Do Nothing
End If
Next pptShape
Next pptSlide
pptPres.Close
This just gives me a blank in column C. My impression is that I'm not looping through the PowerPoint "shapes" correctly. I say that because this because it will correctly put the slide #'s in column B.
Please let me know if you have any questions!
Edit to address comment:
Added additional code
The code is hosted in excel
With ".Value" I get that same exact result
***Important Note: I had an error bypass on (not sure why -- this is dumb when testing code). I turned it off and I am getting error...
Run-time error '-2147024893 (80070003)': Method 'Open' of objection 'Presentations' failed
Which doesn't make any sense because the code is able to open the PP and pull the slide #'s.
I'm writing an Excel macro to read MS Word comments and put it in a sheet. To do this I'm using this VBA Functions:
Set myWord = New Word.Application
Set myDoc = myWord.Documents.Open(varFile)
For Each thisComment In myDoc.Comments
With thisComment
destSheet.Cells(rowToUse, colToUse + 2).Value = .Range.Text
destSheet.Cells(rowToUse, colToUse + 5).Value = .Scope.Text
End With
rowToUse = rowToUse + 1
Next thisComment
Is there a VBA function that could give me the internal number of the comment? I cannot use INDEX because it gave me the location of the comment inside the collection, and it change if the user insert another comments.
What I need is to be able to identify the comments inside the sheet, so I can Append/Update/Delete comments (specially Update) on the sheet.
I'm using MS Word comments to insert "qualitative coding" info on my documents, controling with added specific info (for each comment) in a Excel sheet.
Warm regards
I need to copy name and last name from Excel, paste it into a Word template and print. I have an Excel file from which I need to copy two columns from each row (i.e. E31:F31,E40:F40) into a bookmark in Word and then print it.
I need to loop either from row X to Y or X number of times. The Excel file is not well formatted. I managed to apply paste&print to a custom number of cells, but I get an error probably because I try to do it all at once.
document is in use do you want to open a temp copy
Copied text shows up in Word with big gaps between two words (what came from columns E and F). How do I fix?
Sub Copy_Excel_Cell_to_Word_Form()
Dim wdApp As Object 'Word.Application
Dim wdDoc As Object 'Word.Document
For i = 31 To 60
'Open new instance of Microsoft Word
Set wdApp = CreateObject("Word.Application")
'Make application visible
'wdApp.Visible = False
'Open the word document
Set wdDoc = wdApp.Documents.Open("\\example.doc")
'Copy value
Worksheets(1).Range("E" & i, "F" & i).Copy
'Paste to word document
wdDoc.Bookmarks("WORKER").Range.PasteAndFormat (wdFormatPlainText)
wdDoc.PrintOut
Next
End Sub
For your second point, try this:
Declare a string variable called workerName. Instead of Worksheets(1).Range("E" & i, "F" & i).Copy use
workerName = Worksheets(1).Cells(i,5).Value 'e = 5
workerName = workerName & " " & Worksheets(1).Cells(i,6).Value & vbCr 'f = 6
And instead of use wdDoc.Bookmarks("WORKER").Range.PasteAndFormat (wdFormatPlainText) use
wdDoc.Bookmarks("WORKER").Range.Text = workerName
This should clear up the big gaps.
So, I needed to take some data done in MS Word tables, and manipulate in excel.
I decided to get it from word to excel via a VBA subroutine to "save time".
My source word document contained like twentysomething tables.
I took my source document's tables, extracted my data and made a new document, with a new table, only needing me to copy and paste it into excel.
However, while the final table before copy looks good in word. When i copy it to excel, it breaks up the cells that contain whole paragraphs into separate cells.
As most excel peeps would know, even though a solution looks like in excel, doing a merge and center - that only preserves the content in the uppermost cell in the selection!
So, any advice, on either a better merge and center, or a better "time saver" alltogether, would be great.
Here's a sample of the code so far:
Sub First()
Dim tableLength, tableIndex
tableLength = ThisDocument.Tables.Count
Dim tblReport As Table
Dim docReport As Document
Set docReport = Documents.Add
Set tblReport = docReport.Tables.Add(Selection.Range, 1, 2)
With tblReport
Dim fieldOne, subvalueAription, subvalueA, subvalueB, subvalueC
For tableIndex = 1 To tableLength
fieldOne = ThisDocument.Tables(tableIndex).Rows(2).Cells(2).Range.Text
subvalueA = Trim(ThisDocument.Tables(tableIndex).Rows(4).Cells(2).Range.Text)
subvalueB = "A: " & Trim(ThisDocument.Tables(tableIndex).Rows(5).Cells(2).Range.Text)
subvalueC = "B: " & Trim(ThisDocument.Tables(tableIndex).Rows(6).Cells(2).Range.Text)
subvalueAription = subvalueA & subvalueB & subvalueC & "C: "
Dim rowNext As row
Set rowNext = .Rows.Add
rowNext.Cells(1).Range.Text = fieldOne
rowNext.Cells(2).Range.Text = subvalueA & subvalueB & subvalueC
Next
End With
End Sub
Excel uses a different line terminator than Word. In order to avoid the problem with the text from the Word table being split over several Excel cells, you need to handle the line terminator conversion yourself.
'Word > Excel
newText = Replace(wordText, vbCrLf, vbLf)
I'm posting this by memory, but that's the root of that problem.