Results 1 to 3 of 3

Trouble looping through content control blocks in MS Word to then export to Excel

Threaded View

mascon Trouble looping through... 04-08-2020, 10:36 PM
Pepe Le Mokko Re: Trouble looping through... 04-09-2020, 02:24 AM
mascon Re: Trouble looping through... 04-09-2020, 11:30 AM
  1. #1
    Registered User
    Join Date
    04-08-2020
    Location
    San Diego, California
    MS-Off Ver
    MS 365 on a Mac
    Posts
    2

    Trouble looping through content control blocks in MS Word to then export to Excel

    Hello all,

    I am attempting to write a macro that will allow me to type data in MS Word using content control blocks (ccblocks) in groups of 25. I then want to extract that data and import in into Excel in the next available or empty row. I then want to move to the next 25 cc blocks in the word document, etc.

    I have partial success and I can accomplish my objective but the loop is not efficient and will not allow me automatically loop through groups of 25 blocks. I have to manually type in the number of blocks I want to loop through (using For i = 1 to 25, etc.) instead of just detecting the next 25 blocks. (these will always be in groups of 25 cc blocks). I at times might have hundreds or a thousand rows filled in the excel document so manually writing each set of blocks to loop through isn't efficient.

    What I currently have, I map to and open a word document from excel, this is necessary when working off my excel template.

    Sub Trying_To_Loop()
    
    Dim appWD As Object
    Dim wddoc As Object
    Dim i As Integer
    Dim r As Integer
    Dim rr As Integer
    
    
    On Error Resume Next
    Set appWD = GetObject(, "Word.application") 
    If Err = 429 Then
    Set appWD = CreateObject("Word.application") 
    Err.Clear
    End If
    
    'This section I map to a preexisting location where the word doc is sitting.
    
    Set wddoc = appWD.Documents.Add(Template:="/Users/########/Library/Group Containers/UBF8T346G9.Office/MyExcelFolder/Product ID.docx", NewTemplate:=False, DocumentType:=0)
    appWD.Visible = True
    wddoc.Visible = True
    appWD.DisplayAlerts = True
    
    
    'This portion begins where I attempt to loop. As you can see in the For i = portion, I have to write 1 to 25, 26 to 50, etc. I want it to just find the next group of 25 cc blocks.
    
    r = 1
    rr = Sheets("Sheet1").Cells(Rows.Count, r).End(xlUp).Offset(1, 0).row
    For i = 1 To 25
    Sheets("Sheet1").Cells(rr, r).Value = wddoc.ContentControls(i).Range.Text
    r = r + 1
    Next i
    
    r = 1
    rr = Sheets("Sheet1").Cells(Rows.Count, r).End(xlUp).Offset(1, 0).row
    For i = 26 To 50
    Sheets("Sheet1").Cells(rr, r).Value = wddoc.ContentControls(i).Range.Text
    r = r + 1
    Next I
    
    
        wddoc.Close SaveChanges:=wdDoNotSaveChanges
        wddoc.Quit
    
    
    End Sub
    Any assistance would be greatly appreciated. P.S. Im working with Office 365 on a MacBook Pro.

    Thank you

    /r
    mascon
    Last edited by mascon; 04-09-2020 at 11:28 AM. Reason: Missing code tags, clarity.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Count checked fieldform and content control boxes from word using excel
    By morganhughes360 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-04-2015, 06:39 AM
  2. Excel Populating Microsoft Word Content Control DropDownList
    By Shanewjj in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-23-2014, 03:31 PM
  3. [SOLVED] Pull data to Word control content from Excel spreadsheet
    By coopman64 in forum Excel General
    Replies: 2
    Last Post: 06-30-2014, 10:05 PM
  4. Replies: 0
    Last Post: 07-02-2013, 08:40 AM
  5. VBA/Word picture content control causing unreadable content
    By atycks in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-15-2013, 04:51 PM
  6. Control word from vba/excel - export multiple oleobjects to word
    By mathengineer in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-17-2012, 11:09 AM
  7. Replies: 4
    Last Post: 12-17-2007, 08:11 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1