How to copy excel embedded image to word header in table in specific cell? - excel

I'm calling procedure from my main procedure to make header in word, that contains 2 lines of text, then image, then 1 line of text. I'm trying to do that with table that has 1 column and 4 rows. In 3rd row I want picture. Picture is stored on sheet in excel file, that contains all data for report in word. Paste is not working. Can't figure out how to get image in cell.
Found that picture can be added from file, but I don't want to keep picture in separate file, because if I move my excel file I have to move picture file also.
'Procedure, to create header
Sub MakeHeader()
Dim StrArr(1 To 2) As String
Dim RangeObj As Word.Range
'load text from excel table
StrArr(1) = ActiveSheet.Range("A26").Value
StrArr(2) = ActiveSheet.Range("A27").Value
'to create table
Set RangeObj = ActiveDocument.Sections(1).Headers(1).Range
RangeObj.Tables.Add Range:=RangeObj, NumRows:=4, NumColumns:=1
'populate table
'//
RangeObj.Tables(1).Cell(1, 1).Range.Text = StrArr(1)
RangeObj.Tables(1).Cell(2, 1).Range.Text = StrArr(2)
'copy picture that is embedded in excel sheet
'Shapes(4), because there are more then one object in sheet
ActiveSheet.Shapes(4).CopyPicture xlScreen, xlBitmap
RangeObj.Tables(1).Cell(3, 1).Application.Selection.Paste
'//
'center
ActiveDocument.Sections(1).Headers(1).Range.ParagraphFormat.Alignment = 1
End Sub

The main issue in the code is in the line
RangeObj.Tables(1).Cell(3, 1).Application.Selection.Paste
The Picture is getting pasted in the document itself as it is being referred to Application object selection (normally it is not in the header table but in the main document). So changing the line to
RangeObj.Tables(1).Cell(3, 1).Range.Paste
would paste it in the header table as shown below
Also instead of referring ActiveDocument directly in excel VBA (causing problem in some instances of run) it may be referred via Word Application.
The Full modified code:
Sub MakeHeader()
Dim StrArr(1 To 2) As String
Dim RangeObj As Word.Range
'Next line Added for test
Dim wd As Word.Application
'load text from excel table
StrArr(1) = ActiveSheet.Range("A26").Value
StrArr(2) = ActiveSheet.Range("A27").Value
'to create table
'Next Three line Added for test
Set wd = CreateObject("Word.Application")
wd.Visible = True
wd.Documents.Add
'Wd i.e. referance to Word application added to ActiveDocument
Set RangeObj = wd.ActiveDocument.Sections(1).Headers(1).Range
RangeObj.Tables.Add Range:=RangeObj, NumRows:=4, NumColumns:=1
'populate table
'//
RangeObj.Tables(1).Cell(1, 1).Range.Text = StrArr(1)
RangeObj.Tables(1).Cell(2, 1).Range.Text = StrArr(2)
'copy picture that is embedded in excel sheet
'Shapes(4), because there are more then one object in sheet
'shapes(4) modified to Shapes(1) for test. Change to Your requirement
ActiveSheet.Shapes(1).CopyPicture xlScreen, xlBitmap
'This line was causing Problem as Range.Application was referring to Word application
' And picture is getting pasted in the document not in header Table
RangeObj.Tables(1).Cell(3, 1).Range.Paste
'//
'center
'Wd i.e. referance to Word application added to ActiveDocument
wd.ActiveDocument.Sections(1).Headers(1).Range.ParagraphFormat.Alignment = 1
End Sub

Try:
Sub MakeWordHeader()
Dim wdApp As New Word.Application, wdDoc As Word.Document
Dim wdRng As Word.Range, wdTbl As Word.Table
Dim xlSht As Excel.Worksheet: Set xlSht = ActiveSheet
With wdApp
.Visible = True
Set wdDoc = .Documents.Add
With wdDoc
Set wdRng = .Sections(1).Headers(1).Range
Set wdTbl = .Tables.Add(Range:=wdRng, NumRows:=4, NumColumns:=1)
With wdTbl
.Cell(1, 1).Range.Text = xlSht.Range("A26").Text
.Cell(2, 1).Range.Text = xlSht.Range("A27").Text
xlSht.Shapes(4).CopyPicture xlScreen, xlBitmap
.Cell(3, 1).Range.Paste
End With
wdRng.ParagraphFormat.Alignment = wdAlignParagraphCenter
End With
End With
End Sub

For someone in future that wants to do something similar, but without Table
'Procedure, to create header
Sub MakeHeader(WApp As Object)
Dim StrArr(1 To 3) As String
Dim ImageObj As Excel.Shape
Dim Doc As Word.Document
Dim i As Long
Dim Count As Long
'load text from excel file
StrArr(1) = ActiveSheet.Range("A26").Value
StrArr(2) = ActiveSheet.Range("A27").Value
StrArr(3) = ActiveSheet.Range("A28").Value
'create object to hold picture
Set ImageObj = ActiveSheet.Shapes(4)
Set Doc = WApp.ActiveDocument
With Doc.Sections(1).Headers(1).Range
'centers text
.ParagraphFormat.Alignment = 1
'choosing font
.Font.Name = "Verdana"
.Font.Size = 9
'writes text
.InsertAfter StrArr(1)
.Paragraphs.Add
.InsertAfter StrArr(2)
.Paragraphs.Add
'creates space for image
For i = 1 To 8
.InsertAfter vbNullString
.Paragraphs.Add
Next
.InsertAfter StrArr(3)
'change font size for paragraphs 1 and 2
.Paragraphs(1).Range.Font.Size = 10
.Paragraphs(2).Range.Font.Size = 10
'copies image form excel file
With ImageObj
.Copy
End With
'collapses selection, 0 = wdCollapseEnd
.Collapse Direction:=0
'paste image, 3 = wdPasteMetafilePicture
.PasteSpecial DataType:=3
'centers image
.ShapeRange.Align msoAlignCenters, True
'lowers it from top of page
.ShapeRange.Top = 35
End With
'counts words in header
Count = Doc.Sections(1).Headers(1).Range.Words.Count
'underlines last two words, count considers ".", "#" and etc. as words
With Doc.Sections(1).Headers(1).Range
.Words(Count - 1).Font.Underline = 1
.Words(Count - 2).Font.Underline = 1
.Words(Count - 3).Font.Underline = 1
.Words(Count - 4).Font.Underline = 1
.Words(Count - 5).Font.Underline = 1
.Words(Count - 6).Font.Underline = 1
.Words(Count - 7).Font.Underline = 1
'don't need to underline comma ","
.Words(Count - 9).Font.Underline = 1
.Words(Count - 10).Font.Underline = 1
.Words(Count - 11).Font.Underline = 1
.Words(Count - 12).Font.Underline = 1
.Words(Count - 13).Font.Underline = 1
.Words(Count - 14).Font.Underline = 1
.Words(Count - 15).Font.Underline = 1
End With
End Sub

Related

Word to Excel data transfer of bookmark section locations (cross references)

I am trying to obtain the numbered locations of my bookmarks (paragraph number without context) in a Word document (a lengthy legal document template) and. I am currently using the following code to pull the bookmarked text values from the Word document into an Excel workbook I've built out to grab other data from other sources, but I haven't been able to figure out how to manipulate the code to grab the bookmark's paragraph numbers (I searched high and low for this one too, and am a VBA newbie. I know just enough to be dangerous, but not enough to be helpful lol). Please Help!
Sub SectionLocationImportTESTING()
Dim intDocCount As Integer
Dim wdApp As Word.Application, wdDoc As Word.Document, xlWb As Excel.Workbook, xlWs As Excel.Worksheet
Dim BookmarkText As String
Set wdApp = GetObject(, "Word.Application")
On Error GoTo 0
If wdApp Is Nothing Then
MsgBox "There are no MS Word Documents open.", vbInformation, "No Word Documents open"
Exit Sub
End If
Set xlWb = ThisWorkbook
Set xlWs = ActiveWorkbook.Sheets("Data Input")
intDocCount = wdApp.Documents.Count
If intDocCount > 1 Then
MsgBox "There are " & intDocCount & " Word Documents open." & vbNewLine & vbNewLine & _
"Please close the additional MS Word Documents", vbCritical, "Too many Word Documents open!"
Set wdApp = Nothing
Exit Sub
End If
With wdApp
Set wdDoc = wdApp.ActiveDocument
wdDoc.Activate
'This is very abbreviated, I have about 300 bookmarks that transfer
If wdDoc.Bookmarks.Exists("Section_Rent") = True Then
BookmarkText = wdDoc.Bookmarks("Section_Rent").Range.Text
xlWs.Cells(202, 22) = ("Section_Rent")
xlWs.Cells(202, 23) = BookmarkText
End If
End With
ActiveWorkbook.RefreshAll
ActiveSheet.PivotTables("Data_Input_Table").PivotFields("Trimmed Data"). _
PivotFilters.Add2 Type:=xlCaptionIsGreaterThan, Value1:="0"
Columns("D:D").EntireColumn.AutoFit
Range("A1").Select
MsgBox "Transfer is complete."
End Sub
I don't think there's a straight-forward way of doing that.
You could do this for example:
Sub Tester()
Debug.Print ParagraphNumber(Selection.Range)
End Sub
Function ParagraphNumber(rng As Range)
ParagraphNumber = rng.Document.Range(0, rng.End).Paragraphs.Count
End Function
...but it will also count "empty" paragraphs.
If you have a lot of bookmarks, you could consider listing the names in your Excel sheet and then looping over that range to run the text extraction. If you hard-code all those names into your VBA that's going to be very hard to maintain.
E.g.
'...
Dim c As Range, bm As String, rngBM As Word.Range
'...
'...
Set wdDoc = wdApp.ActiveDocument
wdDoc.Activate
'range with your bookmark names
Set rngBM = ThisWorkbook.Sheets("Bookmarks").Range("A2:A300")
For Each c In rngBM.Cells
bm = c.Value 'bookmark name
If wdDoc.Bookmarks.Exists(bm) Then
Set rngBM = wdDoc.Bookmarks(bm).Range
'for demo purposes just putting info next to the bookmark name...
c.Offset(0, 1).Value = rngBM.Text
c.Offset(0, 2).Value = ParagraphNumber(rngBM)
End If
Next c
There's 2 ways to get the paragraph number, depending on what you want:
Option 1
This will get the exact string of the auto-numbering that you see in the paragraph itself:
E.g. the below paragraph will get you 1.
This is a test paragraph.
If wdDoc.Bookmarks.Exists("Section_Rent") Then
Dim BookmarkText As String
BookmarkText = wdDoc.Bookmarks("Section_Rent").Range.Text
xlWs.Cells(202, 22) = "Section_Rent"
xlWs.Cells(202, 23) = BookmarkText
Dim BookmarkParaNum As String
BookmarkParaNum = wdDoc.Bookmarks("Section_Rent").Range.ListFormat.ListString
xlWs.Cells(202, 24) = BookmarkParaNum
End If
Option 2
This will get the string that you see if you insert a cross reference to the paragraph:
Using the below code for the same paragraph in Option 1 will give you just 1, the same as what inserting it as cross reference will get you.
wdDoc.Paragraphs.Last.Range.InsertParagraphAfter 'A temporary paragraph for inserting field later
Dim fieldRng As Range
Set fieldRng = wdDoc.Paragraphs.Last.Range.Duplicate
If wdDoc.Bookmarks.Exists("Section_Rent") Then
Dim BookmarkText As String
BookmarkText = wdDoc.Bookmarks("Section_Rent").Range.Text
xlWs.Cells(202, 22) = "Section_Rent"
xlWs.Cells(202, 23) = BookmarkText
fieldRng.InsertCrossReference ReferenceType:="Bookmark", ReferenceKind:=wdNumberNoContext, ReferenceItem:="Section_Term", InsertAsHyperlink:=True, IncludePosition:=False, SeparateNumbers:=False, SeparatorString:=" "
Dim tempField As Field
Set tempField = fieldRng.Fields(1)
Dim BookmarkParaNum As String
BookmarkParaNum = tempField.Result
xlWs.Cells(202, 24) = BookmarkParaNum
tempField.Delete
End If
fieldRng.Delete 'Delete the temporary paragraph

Get Value and Position of Checkbox in Word Table to Copy Entire Table to Excel

I have a Word file with approximately 10 pages and 20 tables. Some of these tables have checkboxes. I want to copy these tables into an Excel file.
The following code copies all tables from my Word file into my Excel file:
Sub Import()
Option Explicit
Dim wb As Workbook
Dim sh As Worksheet
Dim sheet As Worksheet
Dim lzeile As Integer
Set wb = ActiveWorkbook
Set sh = wb.ActiveSheet
Set sheet = wb.Worksheets("Tabelle1")
Dim Btxt As Object
Set Btxt = CreateObject("Word.Application")
Btxt.Visible = True
Btxt.documents.Open "C:\Users\*.doc" '*=path
lzeile = 0
For i = 1 To 20
Btxt.ActiveDocument.Tables(i).Range.Copy
Application.Goto sheet.Cells(1 + lzeile, 1)
sheet.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False
lzeile = sheet.Cells(Rows.Count, 1).End(xlUp).Row
lzeile = lzeile + 1
sheet.Cells(lzeile, 1) = "Tabelle" & i
Next i
Btxt.Quit
Set Btxt = Nothing
Set wb = Nothing
Set sh = Nothing
End Sub
It does not copy checkboxes or the value (0 = not checked / 1 = checked) of the checkbox.
I can write the value of a checkbox into a cell in my excel sheet with this line:
sheet.Cells(j, 10) = Btxt.ActiveDocument.Tables(i).FormFields.Item("Kontrollkästchen" & j).Result
With a loop j over all "Kontrollkästchen" (german translation of contentcontrol or formfield item) so basically the name of all formfield items in this Word file.
How can I get the position of these formfield items or identify which formfield item / ContentControl is in which table?
I tried to go through all rows and columns in each table because none of them are larger than 10x10. But I can´t find a way to check if a checkbox is maybe in table 3 on column 5 row 5 and then read the name of this checkbox to a safe the value (0 / 1) in the Excel cell on the same position in my copied table.
The solution depends on whether they're formfields or content controls.
Assuming they're formfields:
Sub Demo()
Dim i As Long, j As Long, Rng As Range
With ActiveDocument
For i = .FormFields.Count To 1 Step -1
With .FormFields(i)
If .Type = wdFieldFormCheckBox Then
j = Abs(.CheckBox.Value)
Set Rng = .Range
.Delete
Rng.Text = j
End If
End With
Next
End With
End Sub
Assuming they're content controls:
Sub Demo()
Dim i As Long, j As Long, Rng As Range
With ActiveDocument
For i = .ContentControls.Count To 1 Step -1
With .ContentControls(i)
If .Type = wdContentControlCheckBox Then
j = Abs(.Checked)
Set Rng = .Range
.Delete
Rng.Text = j
End If
End With
Next
End With
End Sub
For the sake of simplicity and clarity, the sample code below leaves out the parts having to do with Excel, as well as creating the instance of the Word Application. It shows only how to access the Word document's checkboxes and convert those to static values.
At the end, also, the document is closed without saving changes, which means forms protection and the checkboxes should be left intact - the macro will not have affected them.
Note: You should have Option Explicit at the top of the code page, not inside a "Sub".
How it works
The document to be processed is opened and at that moment set to an object (doc). Use this instead of ActiveDocument as it will be much clearer and, in case the user would try to do something, won't affect macro execution.
If the document has forms protection, this must be turned off in order to delete the checkboxes and insert static values.
Then all the form fields are looped. If they are checkboxes, the value is determined, the checkbox removed and the value assigned to the range the checkbox occupied.
After this has completed comes the code to transfer data to Excel. Then the document is closed without saving changes.
Sub ConvertCheckBoxesToValues()
Dim ff As Object ' Word.FormField
Dim doc As Object ' Word.Document
Dim cbValue As String
Dim rngFF As Object ' Word.Range
Set doc = Btxt.Documents.Open("C:\Users\*.doc") '*=path
If doc.ProtectionType <> -1 Then 'wdNoProtection
doc.Unprotect
End If
For Each ff In doc.FormFields
If ff.Type = 71 Then 'wdFieldFormCheckBox
If ff.CheckBox.value = True Then
cbValue = "1"
Else
cbValue = "0"
End If
Set rngFF = ff.Range
ff.Delete
rngFF = cbValue
End If
Next
'Transfer the information to Excel, then
doc.Close 0 'wdDoNotSaveChanges
End Sub

Paste Error Excel to Powerpoint VBA

I am pasting some excel data into powerpoint as a picture and I am having some issues. I have 290 files which I am pasting a table into slide 4, 5 and 6 of each PP file. This worked perfectly yesterday when I was only doing 1 table into slide 6. I have replicated the process and now I keep getting random errors at random times. Sometimes its file 10, others file 50, different everytime. The errors range from the paste datatype is not available OR the clipboard is empty. I have tried every datatype, pasting as a metafile, as a shape, as a picture, just basic pasting and nothing stops the error. I have no idea! Here is my code: PLEASE HELP !
Sub Update_Site_Report()
'Initiate Variables
Dim objPPT As Object
Dim PPTPrez As Object
Dim FinSlide As Object
Dim AssumSlide As Object
Dim RiskSlide As Object
Dim FinTable As Object
Dim AssumTable As Object
Dim RiskTable As Object
Dim fileNameString As String
Dim PicCount As Long
Dim PicCount1 As Long
Dim PicCount2 As Long
Dim i As Long
Dim fileN As String
Dim Directory As String
'Create and open powerpoint application
Set objPPT = CreateObject("PowerPoint.Application")
objPPT.Visible = True
Application.ScreenUpdating = False
'Update site report table from spreadsheet
For i = 2 To 291
Sheet20.Cells(18, 2) = Sheet20.Cells(5, i)
Sheet20.Cells(19, 2) = Sheet20.Cells(6, i)
Sheet20.Cells(20, 2) = Sheet20.Cells(7, i)
Sheet20.Cells(21, 2) = Sheet20.Cells(8, i)
Sheet20.Cells(18, 3) = Sheet20.Cells(10, i)
Sheet20.Cells(19, 3) = Sheet20.Cells(11, i)
Sheet20.Cells(20, 3) = Sheet20.Cells(12, i)
Sheet20.Cells(21, 3) = Sheet20.Cells(13, i)
'Take column header from spreadsheet and set as filename
fileN = Sheet20.Cells(4, i)
' Allow directory to be set in excel tab
Directory = Sheet20.Cells(18, 5)
'Open powerpoint presentation at Directory with Filename
Set PPTPrez = objPPT.Presentations.Open(Directory & fileN & ".pptx")
'Set range for site report table
Set Financials = Sheet20.Range("A17:C21")
Set Assumptions = Sheet45.Range("A1:C7")
Set Risks = Sheet45.Range("A24:D41")
'Choose which slide to paste site report table
Set FinSlide = PPTPrez.Slides(6)
Set AssumSlide = PPTPrez.Slides(4)
Set RiskSlide = PPTPrez.Slides(5)
'If there is a table in powerpoint slide, delete the table
For PicCount1 = AssumSlide.Shapes.Count To 1 Step -1
If AssumSlide.Shapes(PicCount1).Type = msoPicture Then
AssumSlide.Shapes(PicCount1).Delete
End If
Next
For PicCount = FinSlide.Shapes.Count To 1 Step -1
If FinSlide.Shapes(PicCount).Type = msoPicture Then
FinSlide.Shapes(PicCount).Delete
End If
Next
For PicCount2 = RiskSlide.Shapes.Count To 1 Step -1
If RiskSlide.Shapes(PicCount2).Type = msoPicture Then
RiskSlide.Shapes(PicCount2).Delete
Debug.Print
End If
Next
'Paste the site report table into the site report
Financials.Copy
FinSlide.Shapes.PasteSpecial ppPasteShape
Set FinTable = FinSlide.Shapes(FinSlide.Shapes.Count)
Assumptions.Copy
AssumSlide.Shapes.PasteSpecial ppPasteShape
Set AssumTable = AssumSlide.Shapes(AssumSlide.Shapes.Count)
Risks.Copy
RiskSlide.Shapes.PasteSpecial ppPasteShape
Set RiskTable = RiskSlide.Shapes(RiskSlide.Shapes.Count)
'Set position of site report table in powerpoint
FinTable.Left = 36
FinTable.Top = 175
FinTable.Width = 614
AssumTable.Left = 36
AssumTable.Top = 80.8
RiskTable.Left = 36
RiskTable.Top = 80.8
RiskTable.Width = 641.5
'Set filename as string
fileNameString = Directory & fileN & ".pptx"
'Save file as filename
PPTPrez.SaveAs fileNameString
'Close powerpoint presentation
PPTPrez.Close
'Repeat for every site (column) - increment i
Next i
'quit powerpoint
objPPT.Quit
Application.ScreenUpdating = True
MsgBox ("Update complete, click ok to exit powerpoint")
End Sub
Disabling Windows clipboard history solves this issue.

Creating tables at bookmarks in a Word document through Excel VBA from dynamic controls

I'm hoping someone can help me on this as I have been banging my head against this for about a week or so.
I have a userform that creates a set of controls (3 text boxes and a combo box) and interates a counter by 1 each time the button is pressed.
I have written an Excel VBA Macro that should open up a specific document (which will be a dotx in the final version but is a docx for this test) and;
Go to a set bookmark in the Word Document ("table1")
Move to the start of the line with the bookmark
Move up to the previous line
Create a table with 1 row and 3 columns.
Inserts the values from the first and second textboxes and combobox into columns 1,2 and 3 respectively
Return to the specified "table2" bookmark and repeat this for each iterated line of the dynamically created controls
Also the Macro should
Go to a set bookmark in the Word Document ("table2")
Move to the start of the line with the bookmark
Move up to the previous line
Create a table with 1 row and 2 columns.
Inserts the values from the first and third textbox into columns 1 and 2 respectively
Return to the specified "table2" bookmark and repeat this for each iterated line of the dynamically created controls
So basically at each bookmark if there are 3 lines of dynamic controls after the macro runs there should be
A table with 3 rows and 3 columns located above bookmark "table1" containing the values from the first and second textbox and the combobox value.
A table with 3 rows and 2 columns located above bookmark "table2" containing the values from the first and third textbox values.
So my problem is that when the macro is run it either creates one line of the table at the top of the document, another at the "table1" bookmark and one at the "table2" bookmark or the 3 column table is created then another 3 column table is created inside the first cell of the first table and so on.
I am convinced I am missing something in regards to cursor control (as the usual problem is the first table cell is created at the first line of the document then the cursor seems to move to the "table1" bookmark and the macro continues from there).
If someone could give me some pointers on this I would greatly appreciated as I feel I am almost there but that I'm just overlooking something.
Here is the code I have so far, apologies in advance for any unused variables this is back of a napkin coding.
Private Sub CommandButton14_Click() 'Create WO Letter
'Open WO letter and copy paste data
Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim riskCombo As Control
Dim theTextBox802 As Control
Dim theTextBox803 As Control
Dim theTextBox804 As Control
Dim b As Integer
Dim c As Integer
Dim intNoOfColumns
Dim wdDoc
Dim objRange1
Dim objRange2
Dim objTable1
Dim objTable2
b = iRiskCount
c = 1
If Me.WOLetter1.Value = False And Me.WOLetter2.Value = False And Me.WOLetter3.Value = False And Me.WOLetter4.Value = False Then
MsgBox "You Must Choose a Letter Type"
Exit Sub
End If
If UserForm1.MultiPage1.Pages(2).Frame15.Controls("Risk" & c).Value = "Risk" Then
MsgBox "Select Risk Level for line " & c
Exit Sub
End If
If Me.WOLetter1.Value = True Then
Set objWord = New Word.Application
objWord.DisplayAlerts = False
objWord.Visible = True
objWord.Activate
Set wdDoc = objWord.Documents.Add(ActiveWorkbook.Path & "\WOTest.docx")
ElseIf Me.WOLetter2.Value = True Then
Set objWord = New Word.Application
objWord.DisplayAlerts = False
objWord.Visible = True
'objWord.Documents.Open ActiveWorkbook.Path & "\WOTest.docx", ReadOnly:=True '"\test.dotx", ReadOnly:=True'"\Doc.dotm", ReadOnly:=True '"\test.dotx", ReadOnly:=True
objWord.Activate
ElseIf Me.WOLetter3.Value = True Then
Set objWord = New Word.Application
objWord.DisplayAlerts = False
objWord.Visible = True
'objWord.Documents.Open ActiveWorkbook.Path & "\WOTest.docx", ReadOnly:=True '"\test.dotx", ReadOnly:=True'"\Doc.dotm", ReadOnly:=True '"\test.dotx", ReadOnly:=True
objWord.Activate
ElseIf Me.WOLetter4.Value = True Then
Set objWord = New Word.Application
objWord.DisplayAlerts = False
objWord.Visible = True
'objWord.Documents.Open ActiveWorkbook.Path & "\WOTest.docx", ReadOnly:=True '"\test.dotx", ReadOnly:=True'"\Doc.dotm", ReadOnly:=True '"\test.dotx", ReadOnly:=True
objWord.Activate
End If
For Each riskCombo In UserForm1.MultiPage1.Pages(2).Frame15.Controls
If b > 0 Then
Set objRange1 = objWord.Selection.Range
Set objRange2 = objWord.Selection.Range
'Table1
objWord.Selection.GoTo What:=wdGoToBookmark, Name:="table1"
objWord.Selection.HomeKey Unit:=wdLine, Extend:=wdMove
objWord.Selection.MoveUp
wdDoc.Tables.Add objRange1, 1, 3
objWord.Selection.GoTo What:=wdGoToBookmark, Name:="table1"
objWord.Selection.HomeKey Unit:=wdLine, Extend:=wdMove
objWord.Selection.MoveUp
Set objTable1 = wdDoc.Tables(1)
'Table 2
objWord.Selection.GoTo What:=wdGoToBookmark, Name:="table2"
objWord.Selection.HomeKey Unit:=wdLine, Extend:=wdMove
objWord.Selection.MoveUp
wdDoc.Tables.Add objRange2, 1, 2
objWord.Selection.GoTo What:=wdGoToBookmark, Name:="table2"
objWord.Selection.HomeKey Unit:=wdLine, Extend:=wdMove
objWord.Selection.MoveUp
Set objTable2 = wdDoc.Tables(1)
With objTable1
.Cell(0, 1).Range.Text = UserForm1.MultiPage1.Pages(2).Frame15.Controls("Text5" & c).Value
.Cell(0, 2).Range.Text = UserForm1.MultiPage1.Pages(2).Frame15.Controls("Text6" & c).Value
.Cell(0, 3).Range.Text = UserForm1.MultiPage1.Pages(2).Frame15.Controls("Risk" & c).Value
End With
With objTable2
.Cell(0, 1).Range.Text = UserForm1.MultiPage1.Pages(2).Frame15.Controls("Text5" & c).Value
.Cell(0, 2).Range.Text = UserForm1.MultiPage1.Pages(2).Frame15.Controls("Text7" & c).Value
End With
c = c + 1
b = b - 1
End If
Next riskCombo
objTable1.Columns(1).SetWidth ColumnWidth:=30, RulerStyle:= _
wdAdjustNone
objTable1.Columns(2).SetWidth ColumnWidth:=350, RulerStyle:= _
wdAdjustNone
objTable1.Columns(3).SetWidth ColumnWidth:=75, RulerStyle:= _
wdAdjustNone
objTable2.Columns(1).SetWidth ColumnWidth:=30, RulerStyle:= _
wdAdjustNone
objTable2.Columns(2).SetWidth ColumnWidth:=425, RulerStyle:= _
wdAdjustNone
End Sub 'end of test
Even some reassurance that I am on the right track with this would be appreciated as I have been beating my head against this for the past week.
You are setting both table one and table two to be the same.
"Set objTable1 = wdDoc.Tables(1)"
"Set objTable2 = wdDoc.Tables(1)"

Finding heading of chapters in word file and copying individual paragraphs to new word file with VBA

Since nobody was able to help me with the problem I posted here before (link is below), I am now trying to solve the task through VBA.
Finding a heading in word file and copying entire paragraph thereafter to new word file with python
To briefly recap, I have a large amount of word files, which I would like to reduce to a more readable size each. In each file, there is one heading that appears several times, always formated as a 'Heading 2'. I look for this specific heading which occurs several times in a document and I want to copy all the text parts in just these chapters with the respective heading to a new word document.
I decided to create an excel file in which I list the files and the respective heading of the chapters that I want to copy (see picture below).
To do this now I have written the following code:
Sub SelectData()
Application.ScreenUpdating = False
Dim WdApp As Word.Application
Set WdApp = CreateObject("Word.Application")
Dim Doc As Word.Document
Dim NewDoc As Word.Document
Dim HeadingToFind As String
Dim ChapterToFind As String
Dim StartRange As Long
Dim EndRange As Long
Dim WkSht As Worksheet
Dim LRow As Long
Dim i As Long
Set WkSht = ThisWorkbook.Sheets("Sheet1")
LRow = WkSht.UsedRange.Cells.SpecialCells(xlCellTypeLastCell).Row
With WkSht
For i = 1 To LRow
If Dir(.Cells(i, 1).Text, vbNormal) = "" Then
.Cells(i, 3).Value = "Please check File Location"
Else
Set Doc = WdApp.Documents.Open(Filename:=.Cells(i, 1).Text, _
AddToRecentFiles:=False, Visible:=False, ReadOnly:=False)
Set NewDoc = Documents.Add
ChapterToFind = LCase(.Cells(i, 2).Text)
With Doc
Selection.HomeKey Unit:=wdStory
With Selection
With .Find
.ClearFormatting
.Text = ChapterToFind
.MatchWildcards = False
.MatchCase = True
.Execute
End With
If .Find.Found Then
.Collapse wdCollapseStart
With .Find
.Text = ""
.Style = "Heading 2"
.Forward = False
.Execute
End With
.MoveDown Count:=1
.HomeKey Unit:=wdLine
StartRange = .Start
.Find.Forward = True
.Find.Execute
.Collapse wdCollapseStart
.MoveUp Count:=1
.EndKey Unit:=wdLine
EndRange = .End
Doc.Range(StartRange, EndRange).Copy
NewDoc.Content.Paste
NewDoc.SaveAs2 Doc.Path & "Clean" & ".docx", wdFormatFlatXML
Else
WkSht.Cells(i, 4).Value = "Error Chapter Not Found"
End If
End With
End With
WdApp.Quit
Set Doc = Nothing: Set NewDoc = Nothing: Set WdApp = Nothing: Set WkSht = Nothing
Application.ScreenUpdating = True
End If
Next
End With
End Sub
However I am really struggling. It seems to not work as I constantly get an error with the command (RunTimeError 438):
Selection.HomeKey Unit:=wdStory
I am aware that I have to activate the Microsoft Word 15.0 Object Library in the references to be able to get word commands. Nevertheless it is not working.
I would greatly appreciate any help, I am also open to other suggestions of course.
The word files look something like in the picture below, however the chapter that I want to extract can occur several times within one word document. As a result my code would probably need a loop or something, I was not able to get this done.
Also I have considered the following links to the topic:
Use VBA with Powerpoint to Search titles in a Word Doc and Copy Text into another Word Document
VBA: open word from excel
word vba: select text between headings
Did I understand this correctly? The following code is the core of what I think you're trying to do. It finds the first Heading 2, then finds all the paragraphs after it until it finds another header of any type or the end of the document. startCopyRange and endCopyRange is the range of those paragraphs. You'll have to piece this into your Excel routine.
A few notes. Always save the active document to a variable and work from that; the user is then free to change active documents while this routine is running. Never use Selection, always use ranges. Never use relative movements like Move, always use API calls.
Sub SelectData()
Dim Doc As Word.Document
Set Doc = ActiveDocument
Dim findRange As Range
Set findRange = Doc.Range
ChapterToFind = "My Chapter"
findRange.Find.Text = ChapterToFind
findRange.Find.Style = "Heading 2"
findRange.Find.MatchCase = True
Dim startCopyRange As Long
Dim endCopyRange As Long
Do While findRange.Find.Execute() = True
startCopyRange = findRange.End + 1
endCopyRange = -1
'findRange.Select
Dim myParagraph As Paragraph
Set myParagraph = findRange.Paragraphs(1).Next
Do While Not myParagraph Is Nothing
myParagraph.Range.Select 'Debug only
If InStr(myParagraph.Style, "Heading") > 0 Then
endCopyRange = myParagraph.Range.Start - 0
End If
If myParagraph.Next Is Nothing Then
endCopyRange = myParagraph.Range.End - 0
End If
If endCopyRange <> -1 Then
Doc.Range(startCopyRange, endCopyRange).Select 'Debug only
DoEvents
Exit Do
End If
Set myParagraph = myParagraph.Next
DoEvents
Loop
Loop
End Sub

Resources