Screen alerts in Acrobat stopping VBA code - excel

I have this VBA code that searches through PDF files on my computer. Here is the code:
Option Explicit
Sub FindTextInPDF()
Dim TextToFind As String
Dim PDFPath As String
Dim App As Object
Dim AVDoc As Object
Dim DS As Worksheet
Dim SS As Worksheet
Set DS = Sheets("Report")
Set SS = Sheets("Search Index")
Dim sslastrow As Long
Dim dslastrow As Long
Dim b As Integer
Dim J As Integer
With SS
sslastrow = .Cells(.Rows.Count, "B").End(xlUp).Row
End With
With DS
dslastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
For b = 2 To dslastrow
PDFPath = "C:\Users\desposito\Documents\Temp\" &
Sheets("Report").Range("E" & b).Value & Sheets("Report").Range("B" &
b).Value & ".pdf"
If Dir(PDFPath) = "" Then
GoTo nextb
End If
If LCase(Right(PDFPath, 3)) <> "pdf" Then
GoTo nextb
End If
On Error Resume Next
Set App = CreateObject("AcroExch.App")
If Err.Number <> 0 Then
Set App = Nothing
GoTo nextb
End If
Set AVDoc = CreateObject("AcroExch.AVDoc")
If Err.Number <> 0 Then
Set AVDoc = Nothing
Set App = Nothing
GoTo nextb
End If
On Error GoTo 0
If AVDoc.Open(PDFPath, "") = True Then
AVDoc.BringToFront
Else
App.Exit
Set AVDoc = Nothing
Set App = Nothing
GoTo nextb
End If
For J = 2 To sslastrow
TextToFind = SS.Range("B" & J).Value
If AVDoc.FindText(TextToFind, False, False, True) = False Then
GoTo NextJ
Else:
DS.Range("Q" & b).Value = DS.Range("Q" & b).Value & TextToFind & ";"
& " "
End If
NextJ:
Next
AVDoc.Close True
App.Exit
Set AVDoc = Nothing
Set App = Nothing
nextb:
Next
End Sub
However, every 100ish files, I will get this notification:
"Reader has finished searching the document. No matches were found."
All I have to do is hit enter and then the code runs for another 10-30 minutes before I get the notification again. It seems to be randomly happening in the middle of searching through the document which is this part of the code:
For J = 2 To sslastrow
TextToFind = SS.Range("B" & J).Value
If AVDoc.FindText(TextToFind, False, False, True) = False Then
GoTo NextJ
Else:
DS.Range("Q" & b).Value = DS.Range("Q" & b).Value & TextToFind & ";"
& " "
End If
NextJ:
I looked into disabling screen alerts in acrobat, but it doesn't look like I can do that.

Related

Updated Page number in MS Word footer table from Excel

I am using Excel VBA code to updated Word document footer table information from excel. Its work fine only problem. I am unable to update page number in word. Kindly refer the below code I am using. Below is also an image of the footer table I have in word.
Use of this code. This code will help me to update some information from excel to MS word footer table. It works perfectly but page number i need your help to make dynamic.
Sub Update_Informe_word_2003()
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim wdRng As Word.Range
Dim j As Integer
Dim datos(0 To 1, 0 To 30) As String '(columna,fila)
Dim ruta As String
Dim rngFooter As Word.Range
Dim tbl As Word.Table
Dim rngCell As Word.Range
Dim FileName As String
Set wdApp = CreateObject("Word.Application")
wdApp.Visible = True
For i = 2 To Application.WorksheetFunction.CountA(Range("A:A"))
On Error GoTo nx:
If Range("C" & i).Value = "Form (FORM)" Then
logo = Range("s2").Value
ruta = Range("s4").Value & "\Form\Word\" & Range("B" & i).Value & ".doc"
FileName = VBA.FileSystem.Dir(ruta)
If FileName = VBA.Constants.vbNullString Then GoTo nx
Set wdDoc = wdApp.Documents.Open(ruta)
Set rngFooter = wdDoc.Sections(1).Footers(wdHeaderFooterPrimary).Range
rngFooter.Delete
With rngFooter
Set tbl = rngFooter.Tables.Add(rngFooter, 1, 3)
' tbl.Select
With tbl.Borders
.OutsideLineStyle = wdLineStyleSingle
End With
Set rngCell = tbl.Cell(1, 3).Range
rngCell.Text = "Doc #: " & Range("e" & i).Value & Chr(10) & "Rev. #: " & Range("H" & i).Value
rngCell.Font.Size = 7
rngCell.Font.Name = "Arial"
rngCell.Paragraphs.Alignment = wdAlignParagraphRight
Set rngCell = tbl.Cell(1, 1).Range
rngCell.Text = "Uncontrolled When Printed" & Chr(10) & "Page 1 of 3"
rngCell.Font.Size = 7
rngCell.Font.Name = "Arial"
Set rngCell = tbl.Cell(1, 2).Range
rngCell.Text = "VECTRUS COMPANY PROPRIETARY" & Chr(10) & "If Client Proprietary, Leave this Blank"
rngCell.Font.Size = 7
rngCell.Font.Name = "Arial"
rngCell.Font.Bold = True
End With
'Set rngheader = wdDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range
'rngheader.Delete
'Set tbl = rngheader.Tables.Add(rngheader, 1, 3)
'Set rngCell = tbl.Cell(1, 1).Range
'With rngCell
'.InlineShapes.AddPicture FileName:=logo, LinkToFile:=False, SaveWithDocument:=True
'End With
Dim FindWord As String
Dim result As String
rngFooter.Find.Execute FindText:="Doc #:", Forward:=True
If rngFooter.Find.Found = True Then rngFooter.Bold = True
Set rngFooter = wdDoc.Sections(1).Footers(wdHeaderFooterPrimary).Range
rngFooter.Find.Execute FindText:="Rev. #: ", Forward:=True
If rngFooter.Find.Found = True Then rngFooter.Bold = True
Set rngFooter = wdDoc.Sections(1).Footers(wdHeaderFooterPrimary).Range
rngFooter.Find.Execute FindText:="Uncontrolled When Printed", Forward:=True
If rngFooter.Find.Found = True Then rngFooter.Bold = True
Range("M" & i).Value = "Updated"
wdDoc.Save
wdDoc.Close
End If
nx:
Next
Call Update_Informe_Excel_2003
MsgBox ("Files updated")
End Sub
Since you print a string "Page 1 of 3" to the footer, the page number will naturally not be updated.
The current page number and total page number are stored in document fields, which you can insert with the following code:
Fields.Add oRange, wdFieldEmpty, "PAGE \* Arabic", True
Fields.Add oRange, wdFieldEmpty, "NUMPAGES ", True
In your case, replace
Set rngCell = tbl.Cell(1, 1).Range
rngCell.Text = "Uncontrolled When Printed" & Chr(10) & "Page 1 of 3"
with
Set rngCell = tbl.Cell(1, 1).Range
rngCell.Collapse
rngCell.InsertAfter = "Uncontrolled When Printed" & Chr(10)
rngCell.Collapse 0
wdDoc.Fields.Add rngCell, wdFieldEmpty, "PAGE \* Arabic", True
rngCell.InsertAfter " of "
rngCell.Collapse 0
wdDoc.Fields.Add rngCell, wdFieldEmpty, "NUMPAGES ", True
To update fields, use Ctrl+A and Shift+F9 or use the following VBA:
Dim oStory
For Each oStory In wdDoc.StoryRanges
oStory.Fields.Update
Next oStory
This final Answer
Set rngCell = tbl.Cell(1, 1).Range
rngCell.Collapse
wdDoc.Fields.Add rngCell, wdFieldEmpty, "NUMPAGES ", False
rngCell.Collapse
rngCell.InsertBefore " of "
rngCell.Collapse
wdDoc.Fields.Add rngCell, wdFieldEmpty, "PAGE \* Arabic", True
rngCell.Text = "Uncontrolled When Printed" & Chr(10) & "Page "

Insert adjustable table in Word document from Excel

I will have two situations either:
Otherwise the first cell, will contain more values separated by ";" as follows:
These situations should result in different tables which should be inserted in a pre-existing Word document I open with the VBA from Excel.
The resulting tables are shown below:
I just inserted a "fixed" table in the Word document and replace the inside values, this isn't sufficient anymore.
This is the code I use to open a Word document and replace certain words and save the newly made Word documents as a new file in both docx and pdf format:
Sub Sample()
Const wdFindContinue As Long = 1
Const wdReplaceAll As Long = 2
Const StrNoChr As String = """*./\:?|"
Dim oWordApp As Object, oWordDoc As Object, rngStory As Object
Dim sFolder As String, strFilePattern As String
Dim strFileName As String, sFileName As String
Dim cant As Integer
Dim tex As String
Dim max As Integer
Dim total As Integer
Dim final As Integer
sFolder = "C:\Users\name\folder\"
On Error Resume Next
Set oWordApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set oWordApp = CreateObject("Word.Application")
End If
Err.Clear
On Error GoTo 0
oWordApp.Visible = False
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Data")
last_row = Application.WorksheetFunction.CountA(sh.Range("A:A"))
For i = 2 To last_row
sFileName = sFolder & "wordfile.docx"
Set oWordDoc = oWordApp.Documents.Open(sFileName)
For Each rngStory In oWordDoc.StoryRanges
With rngStory.Find
If sh.Range("C" & i).Value <> "" Then
.Text = "_Name1"
.Replacement.Text = sh.Range("C" & i).Value
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End If
If sh.Range("D" & i).Value <> "" Then
.Text = "_Name2"
.Replacement.Text = sh.Range("D" & i).Value
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End If
End With
Next
StrName = Sheets(1).Cells(i, 2)
For j = 1 To Len(StrNoChr)
StrName = Replace(StrName, Mid(StrNoChr, j, 1), "_")
Next j
StrName = Trim(StrName)
With oWordDoc
.SaveAs Filename:=sFolder & StrName & ".docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
'.SaveAs Filename:=sFolder & StrName & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False
.ExportAsFixedFormat sFolder & StrName & ".pdf", 17
.Close SaveChanges:=False
End With
Next i
oWordApp.Quit
Set oWordDoc = Nothing
Set oWordApp = Nothing
MsgBox "Succes"
End Sub
The code isn't relevant for the specific problem, but may give some inspiration or other ideas.
EDIT:
I tried with this:
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=2, NumColumns:= 4
As suggested by MacroPod, but it doesn't work.
For example, assuming the basic tables are already there and you have code to populate the rows with the pre-processed data:
Sub Demo()
Dim oWdApp As Object, oWdDoc As Object, oWdRng As Object, oWdTbl As Object
Dim sFolder As String, sFileName As String, StrTxt As String
Dim last_row As Long, r As Long, c As Long, i As Long, j As Long
Const wdFindContinue As Long = 1: Const wdReplaceAll As Long = 2
Const wdFormatXMLDocument As Long = 12: Const wdFormatPDF As Long = 17
Const StrNoChr As String = """*./\:?|"
sFolder = "C:\Users\name\folder\"
Dim sh As Worksheet: Set sh = ThisWorkbook.Sheets("Data")
last_row = Application.WorksheetFunction.CountA(sh.Range("A:A"))
On Error Resume Next
Set oWdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set oWdApp = CreateObject("Word.Application")
End If
Err.Clear
On Error GoTo 0
oWdApp.Visible = False
For i = 2 To last_row
sFileName = sFolder & "wordfile.docx"
Set oWdDoc = oWdApp.Documents.Add(sFileName)
With oWdDoc
For Each oWdRng In .StoryRanges
With oWdRng.Find
If sh.Range("C" & i).Value <> "" Then
.Text = "_Name1"
.Replacement.Text = sh.Range("C" & i).Value
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End If
If sh.Range("D" & i).Value <> "" Then
.Text = "_Name2"
.Replacement.Text = sh.Range("D" & i).Value
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End If
End With
Next
For Each oWdTbl In .Tables
With oWdTbl
For r = .Rows.Count To 2 Step -1
For c = 1 To .Rows(r).Cells.Count Step 2
StrTxt = Split(.Cell(r, c).Range.Text, vbCr)(0)
If InStr(StrTxt, ";") > 0 Then
For j = 1 To UBound(Split(StrTxt, ";"))
If r = .Rows.Count Then
.Rows.Add
Else
.Rows.Add .Rows(r + 1)
End If
.Cell(r + j, c).Range.Text = Split(Trim(Split(StrTxt, ";")(j)), " ")(0)
.Cell(r + j, c + 1).Range.Text = Replace(Replace(Split(Trim(Split(StrTxt, ";")(j)), " ")(1), ")", ""), "(", "")
Next
End If
If InStr(StrTxt, " ") > 0 Then
.Cell(r, c).Range.Text = Split(Trim(Split(StrTxt, ";")(0)), " ")(0)
.Cell(r, c + 1).Range.Text = Replace(Replace(Split(Trim(Split(StrTxt, ";")(0)), " ")(1), ")", ""), "(", "")
End If
Next
Next
End With
Next
StrName = Sheets(1).Cells(i, 2).Text
For j = 1 To Len(StrNoChr)
StrName = Replace(StrName, Mid(StrNoChr, j, 1), "_")
Next j
StrName = Trim(StrName)
.SaveAs Filename:=sFolder & StrName & ".docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
.SaveAs Filename:=sFolder & StrName & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False
.Close SaveChanges:=False
End With
Next i
oWdApp.Quit
Set oWordDoc = Nothing: Set oWdApp = Nothing: Set oWdRng = Nothing: Set oWdTbl = Nothing: Set sh = Nothing
MsgBox "Succes"
End Sub

How can i change color in my sheet if browser profile url checkpoint with VBA

I want to change color text in my Sheet in Excel if anyprofile browser checkpoint but it alway error when i use oExec in my if:
Private Sub CommandButton1_Click()
Dim chromePro As String
Dim wshell As Object
Dim oExec As Object
chromePath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe --profile-directory=""Profile "
myUrl = "https://www.facebook.com"
Dim LastRow As Long, intRow As Long
With Worksheets("Data")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
For intRow = 1 To LastRow
myPr = Range("A" & intRow).Value
chromePro = (chromePath & "" & myPr & """ " & myUrl)
Set wshell = CreateObject("Wscript.Shell")
Set oExec = wshell.exec(chromePro)
cUrl = "https://www.facebook.com/checkpoint/"
nchromePro = (chromePath & "" & myPr & """ " & cUrl)
***'This block i want to record checkpoint but it error when i use oExec in if***
If oExec = nchromePro Then
.Range("A" & intRow).Font.Color = vbRed
Else
.Range("A" & intRow).Font.Color = vbBlack
End If
Next intRow
End With
End Sub

Choose to Insert Image as an Image or as a Comment

I have code which inserts images from the given path using specific set of numbers against which I already have an image database.
Sub InsertPics()
Dim fPath As String, fName As String
Dim r As Range, rng As Range
Dim shpPic As Shape
Application.ScreenUpdating = False
fPath = "C:\Users\DELL\Documents\FY18-19\Images\"
Set rng = Range("D2:D" & Cells(Rows.Count, 4).End(xlUp).Row)
For Each r In rng
On Error GoTo errHandler
If r.Value <> "" Then
Set shpPic = ActiveSheet.Shapes.AddPicture(Filename:=fPath & r.Value & ".jpg", linktofile:=msoFalse, _
savewithdocument:=msoTrue, Left:=Cells(r.Row, 2).Left, Top:=Cells(r.Row, 2).Top, Width:=-1, Height:=-1)
With shpPic
.LockAspectRatio = msoTrue
If .Width > Columns(2).Width Then .Width = Columns(2).Width
Rows(r.Row).RowHeight = .Height
End With
End If
errHandler:
If Err.Number <> 0 Then
Debug.Print Err.Number & ", " & Err.Description & ", " & r.Value
On Error GoTo -1
End If
Next r
Application.ScreenUpdating = True
End Sub
I need to do the below as well:
ask the file path
ask whether to insert the image as image or as a comment against those set of numbers and run accordingly
If the code can be converted into a select mode run, i.e. on a set of numbers I can run the code for (instead of the entire 'D'-Column I've embedded currently).
May try this code and modify to your requirement.
Sub InsertPics()
Dim fPath As String, fName As String
Dim r As Range, rng As Range
Dim shpPic As Shape, IsCmnt As VbMsgBoxResult
'Application.ScreenUpdating = False
Set rng = ThisWorkbook.ActiveSheet.Range("D2:D" & Cells(Rows.Count, 4).End(xlUp).Row)
On Error GoTo Xexit
Set rng = Application.InputBox("Select the range to import Images", "Import Image", rng.Address, , , , , 8)
On Error GoTo 0
If rng Is Nothing Then Exit Sub
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = " Select Folder to Upload Images"
.AllowMultiSelect = False
.InitialFileName = "C:\Users\user\DeskTop\"
If .Show <> -1 Then Exit Sub
fPath = .SelectedItems(1)
End With
fPath = fPath & "\"
'Avoided further asking wheather all Images are to be uploaded as Comment
'instead used bold font of the file names to do the same
'try Next statement, if want all the images as comment
'IsCmnt = MsgBox("Is the images to be uploaded as comments", vbYesNo)
For Each r In rng
If r.Value <> "" Then
If Dir(fPath & r.Value & ".jpg") <> "" Then
'If IsCmnt = vbYes Then 'try this branch if want all the images as comment
If r.Font.Bold Then ' instead of asking multiple times
r.ClearComments
r.AddComment ""
r.Comment.Shape.Fill.UserPicture fPath & r.Value & ".jpg"
Else
Set shpPic = ActiveSheet.Shapes.AddPicture(Filename:=fPath & r.Value & ".jpg", linktofile:=msoFalse, _
savewithdocument:=msoTrue, Left:=Cells(r.Row, 2).Left, Top:=Cells(r.Row, 2).Top, Width:=-1, Height:=-1)
With shpPic
.LockAspectRatio = msoTrue
If .Width > Columns(2).Width Then .Width = Columns(2).Width
Rows(r.Row).RowHeight = .Height
End With
End If
Else
Debug.Print fPath & r.Value & ".jpg not found"
End If
End If
Next r
Xexit:
'Application.ScreenUpdating = True
End Sub
Code is tested with makeshift images. May disable ScreenUpdatingas per actual condition.

No data transfer to Access with ADODB recordset

I have created an excel linked access database with VBA that works when I use a centrally saved version but not when I save a local copy.
I have used the Debug tool and the code skips my For loop in the locally saved copy.
For x = 2 To nextrow
DatabaseData.AddNew
For i = 1 To 35
DatabaseData(Cells(1, i).Value) = Sheet18.Cells(x, i).Value
Next i
DatabaseData.Update
Next x
I think that this is because the Recordset (DatabaseData) is not being recognized (not sure if that is the correct term).
The code is below
Sub CopyDatatoAccess()
Dim DatabaseConn As ADODB.Connection
Dim DatabaseData As ADODB.Recordset
Dim Pathway
Dim x As Long, i As Long
Dim nextrow As Long
On Error GoTo errorhandler:
Pathway = Sheet18.Range("AQ2").Value
nextrow = Sheet18.Range("AR2")
Set DatabaseConn = New ADODB.Connection
If Sheet18.Range("A2").Value = "" Then
MsgBox "ARF form is not present for Upload"
Exit Sub
End If
DatabaseConn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Pathway
Set DatabaseData = New ADODB.Recordset
DatabaseData.Open Source:="ARFs", _
ActiveConnection:=DatabaseConn, _
CursorType:=adOpenDynamic, _
LockType:=adLockOptimistic, _
Options:=adCmdTable
For x = 2 To nextrow
DatabaseData.AddNew
For i = 1 To 35
DatabaseData(Cells(1, i).Value) = Sheet18.Cells(x, i).Value
Next i
DatabaseData.Update
Next x
DatabaseData.Close
DatabaseConn.Close
Set DatabaseData = Nothing
Set DatabaseConn = Nothing
MsgBox "The ARF is now uploaded"
Application.ScreenUpdating = True
Sheet18.Cells.Range("AK2").Value = Sheet18.Cells.Range("AK4").Value + 1
On Error GoTo 0
Exit Sub
errorhandler:
Set DatabaseData = Nothing
Set DatabaseConn = Nothing
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Export_Data"
End Sub

Resources