Selecting the 4th line in a Word document which has been opened in Excel.

RogerP

New Member
Joined
Aug 20, 2011
Messages
12
Office Version
  1. 365
Platform
  1. Windows
I have written an EXCEL VBA macro which opens a Word document and I need to select the 4th line in the Word Document FROM the Excel VBA macro. I've been reading up on this for hours and cannot figure out how to do it.

The cursor is already positioned at "National Maritime Center" (see below) and the 4th line begins with "D032DG".

Tried were:
Selection.MoveDown Unit:=wdLine, Count:=4 (with and without WordApp. as the word object).

Selection.Goto What:=wdGoToLine, Which:=wdGoToAbsolute, Count:=4 (with and without WordApp. as the word object).

Application.VBE.ActiveCodePane.SetSelection 4, 1, 4, 1

Nothing works. Please advise.

National Maritime Center
Keep ’em Safe, Keep ’em Sailing
________________________________________
D032DG

Thanks,
Roger
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Hi RogerP. Word is mostly difficult when it comes to these things. When you say that you want to select line 4 it may mean that you want to select paragraph 4? I guess the obvious elephant in the room is why do you want to select it? Do you always want to always find "line"4 or maybe always want to find the number? What do you want to do with the selection? Anyways, this seems like it should select paragraph 4 of the active document. HTH. Dave
Code:
Dim Wdapp As Object
'open Word application
On Error Resume Next
Set Wdapp = GetObject(, "word.application")
If Err.Number <> 0 Then
On Error GoTo 0
Set Wdapp = CreateObject("Word.Application")
End If

Wdapp.Visible = True
Wdapp.Documents.Open Filename:="C:\Foldername\DocName.docx"
Wdapp.ActiveDocument.Paragraphs(4).Select
 
Upvote 0
The 4th line (paragraph) is the name of the Illustration which I am trying create as a .pdf or .docx file. The Word document which is already open and visible and the ActiveDocument so that I only need your third instruction. When executing it as: WordApp.ActiveDocument.Paragraphs(4).Select give me an error. When IN the Word Document and I try to record a macro, I am able to do a FIND for the 4th line and the cursor does move so I can select that "word" and use it as the name of the Illustration I am creating. Since my Word file may have multiple Illustrations, I perform a loop to attempt to find the Illustration name, save it as a variable so I can put the file out with that 4th line as the Filename. Thanks for looking into this.

1703515661301.png
 
Upvote 0
Hi again RogerP and Merry Xmas. I'm really not surprised that Word doesn't do what I thought it would. I'm guessing the paragraph has to be changed into a range in order to select it. This again seems like it should work. Dave
Code:
Dim Myrange As Variant
Set Myrange = WdApp.ActiveDocument.paragraphs(4).Range
Myrange.Select
 
Upvote 0
Dave, thank you VERY much for your assistance. It worked beautifully. I'm not that experienced with Word VBA and your help gave me exactly what I needed. Thanks again.

Merry Christmas and have a very Happy New Year.
Roger
 
Upvote 0
Forgive me Dave for taking advantage of you. I'm hoping you have a simple answer to this question. I have extracted/copied a single page from a .docm file and I create/pasted it to a new document; but it spans two pages. Not a big deal but there is no text on the second page, just a number of Paragraph marks. I've been trying to select and delete the second page (see below) and can't get it to work.

I use a tool called: ABLE2EXTRACT to take the input .pdf file and generate both a Word and Excel file from the same .pdf file. I start my macro from the Excel file (to format certain data and clean up the .xlsm file) and then I open the Word document (from within the Excel macro) to create the illustration .docx file reflected by the following screen shot. While within the .xlsm macro, I want to use VBA code to delete the 2nd page and can't figure it out. Any ideas? Thanks, Roger

1703736895863.png

I
 
Upvote 0
Hi again Roger. You will have to create your WdApp and adjust the file path to suit before trialing this code which seems like it should work. Dave
Code:
Sub test()
Dim PagFlag As Boolean, WdApp As Object, WdDoc As Object, MyRange As Variant
Dim LastPara As Integer, PageCnt As Integer, Cnt As Integer

PagFlag = False
'turn on pagination
If WdApp.Options.Pagination = False Then
WdApp.Options.Pagination = True
PagFlag = True
End If

Set WdDoc = WdApp.Documents.Open(YourdocFilePath)
'find last paragraph
LastPara = WdDoc.Content.Paragraphs.Count
PageCnt = 1
'loop paragraphs
For Cnt = LastPara To 1 Step -1
If WdDoc.Paragraphs(Cnt).Range.Information(3) > PageCnt Then
Set MyRange = WdDoc.Paragraphs(Cnt).Range
MyRange.Delete
'????myrange.Range.Delete
End If
Next Cnt

'return Word to orig setting
If PagFlag Then
WdApp.Options.Pagination = False
End If
End Sub
 
Upvote 0
Dave, it's taken me days to incorporate your logic with all the other logic changes I wanted to make. Finally, I am about 99% there. I have one minor issue which I will work to resolve on my own. I can't thank you enough for all your help. The whole purpose of the code I am writing is to try and assist my twin brother with some code he is writing for Mariners around the world to upgrade their license (first mate, second mate, etc.). You've given me invaluable information. THANKS!!!
 
Upvote 0
You are welcome. I hope that Mariners around the World will benefit from your efforts. Thanks for posting your outcome and good luck with the remaining 1%. Dave
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,164
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top