VBA CountIf external document - excel

Following from my previous question, I am now struggling to create a working Application.CountIf function. I am using the following code to access the file as "xl0":
'DATABASE ACCESS
Dim xl0 As New Excel.Application
Dim xlw As New Excel.Workbook
Dim db_directory As String
db_directory = "R:\New Quality Management System\xls\Supplier\Non-Conformance\Supplier Non-Conformance Database.xlsm"
Set xlw = xl0.Workbooks.Open(db_directory)
I can create a function to search the same open document no problem...
Test = Application.CountIf(Range("B:B"), Report_ID)
MsgBox (Test)
...but neither of the methods I've tried for searching in the document open in the background have worked...
Test = Application.CountIf(xlw.Sheets("SNCR Log").Range("B:B"), Report_ID)
...and...
Test = xlw.Sheets("SNCR Log").Application.CountIf(Range("B:B"), Report_ID)
What am I missing?

Just a guess, did not test it, but as Application refers to your current open application and you want to search within the xl0 application try
xl0.CountIf(...)
and see if that helps.

Related

VBA Sharepoint Check In File after Upload

Been using this code for a while to upload a file to SharePoint and noticed the other day that when the file is uploaded, it is checked out to myself automatically and have to go into SharePoint and manually check-in so that others can view the file. Any way to modify or add to my code below to auto check-in a file after it uploads? Totally stumped and any help would be greatly appreciated.
Sub SharePointUpload()
Dim WSN
Dim spAdd
Set WSN = CreateObject("WScript.Network")
spAdd = "https://mysharepoint/test"
WSN.mapnetworkdrive "N:", spAdd
ActiveWorkbook.Save
Dim SharepointAddress As String
Dim LocalAddress As String
Dim objNet As Object
Dim FS As Object
' Where you will enter Sharepoint location path
SharepointAddress = "\\mysharepoint\test"
' Where you will enter the local file path
LocalAddress = "C:\data\sample_file.xlsm"
Set objNet = CreateObject("WScript.Network")
Set FS = CreateObject("Scripting.FileSystemObject")
If FS.FileExists(LocalAddress) Then
FS.CopyFile LocalAddress, SharepointAddress
Else: MsgBox "File does not exist!"
End If
Set objNet = Nothing
Set FS = Nothing
WSN.removenetworkdrive "N:"
End Sub
I had the same issue with our SharePoint Document library even with option Require Check Out switched off - some documents (Word, Excel) were uploaded as Checked Out automatically (green arrow mark) whereas others were in fact Checked In from upload. Moreover when I wanted to Check In manually such documents, I got error message about missing mandatory field, although there was no field in the library set as mandatory.
The reason in my case was the field Title. This field is presented in library as default text field and it is also presented in Office files. I noticed that for Office files with empty field Title the document is automatically Checked Out during upload. When uploaded document contained some text in Title property, no Check Out was set. I also tried to use workflow to change the Title and then Check Out file (after creation), but it was not working - initial Check In was required to start the workflow. Manual change of document properties works but that's too tedious...
The first programmatic option to handle this issue could be to fill Title property of uploaded document when empty (i.e. add file name or any other text). Should work well with many languages. Example with Python:
from docx import Document
path = 'D:/myfile.docx'
document = Document(path)
document_property = document.core_properties
if not document_property.title:
document_property.title = 'Everything is Awesome'
document.save(path)
In VBA Title should accessible via Wb.BuiltinDocumentProperties("Title").
Another successful option for me was to find column Title in Columns of Document library and rename it to something else (I used Titlex as new name), then try to upload new document with empty Title - this time there was no automatic Check Out for the uploaded document. Then I could set the Titlex back to Title without returning the issue.

Automate the pcomm login from Excel vba

I am trying to automate my PCOMM login process. When I execute the below piece of code, I am getting an error.
Dim Num as Long
Set autECLConnList=
CreateObject("PCOMM.autECLConnList")
autECLConnList.Refresh
The error I got is "ActiveX component can't create object." in line 2. I even checked whether the dll was missing, but it looks fine.
According to the documentation, it appears you need to create the object before initializing it:
Dim autECLConnList as Object
See here: https://www.ibm.com/support/knowledgecenter/en/SSEQ5Y_13.0.0/com.ibm.pcomm.doc/books/html/host_access08.htm
Dim autECLConnList as Object
Dim Num as Long
Set autECLConnList = CreateObject("PCOMM.autECLConnList")
autECLConnList.Refresh
Num = autECLConnList.Count
Num is the number of connections present in the autECLConnList collection for the last call to the Refresh method and The Count property is a Long data type and is read-only.

Using Print Dialog with Excel Object, Will Only Print to Default Printer Despite Dialog Selection

I have made a quick app for a dept at work that takes users input text fields and writes them to an excel object. The challenge of the project was that it needed to create the worksheet, write to it, print it, clear out certain confidential information, then save it, (has to print before being saved). Everything works great except that when I use the print dialog to select which printer to send it to, it will only print to the user's default printer or last printer used. I don't want to change their default printers as that will confuse them and cause chaos with some other software we have that is really particular to it's printing locations. I've searched tirelessly online and only found solutions for if I am printing a word document and not a worksheet object.
I believe this is all the relevant code:
Imports Excel = Microsoft.Office.Interop.Excel
Dim objExcel As New Excel.Application
Dim objWorkbook As Excel.Workbook
Dim objWorksheet As Excel.Worksheet
Dim PrintDialog1 As New PrintDialog()
Dim result As DialogResult = PrintDialog1.ShowDialog()
If result = DialogResult.OK Then
PrintDialog1.PrinterSettings = PrintDocument1.PrinterSettings
objWorksheet.PrintOutEx()
MessageBox.Show(PrintDialog1.PrinterSettings.PrinterName)
MsgBox("Order Printing Completed")
ElseIf result = DialogResult.Cancel Then
objExcel.DisplayAlerts = False
objWorkbook.Saved = True
objWorkbook.Close(False)
objExcel.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(objWorksheet)
System.Runtime.InteropServices.Marshal.ReleaseComObject(objWorkbook)
System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcel)
ProgressBar1.Value = 200
ProgressBar1.Visible = False
Exit Sub
End If
Also first question on Exchange, so I don't have a lot of privileges, (aka won't be able to respond to comments right away until allowed), also new to coding in general, and this websites' stricter than MLA question and answer protocol, so be kind.
Open the Printers & Scanners settings in Windows.
At the bottom of your list of printers & scanners make sure that "Allow Windows to manage my default printer" is selected/checked/ticked.
Should fix your problem.

Macro wont run for all users

The error occurs on
Set report = app.OpenReport("S:\Supply Chain\Scheduling\HRE\HRE.imr")
It works for some users and not others.
Any alternative methods or maybe some settings that need to be changed?
Its an objected defined error.
`Sub Update()
Dim app As Object
Dim report As Object
Dim catalog As Object
'open cognos'
Set app = CreateObject("CognosImpromptu.Application")
app.Visible True
app.Activate
'open catalog'
app.OpenCatalog "R:CognosUsers/Cognos Catalogs/SUPPLY CHAIN.cat"
app.Visible True
app.Activate
'open report'
Set report = app.OpenReport("S:\Supply Chain\Scheduling\HRE\HRE.imr")
report.RetrieveAll
'save report path'
report.Export "S:\Supply Chain\Scheduling\HRE\Raw", "X_ascii.flt"
'close cognos'
report.CloseReport
app.Quit`
You might have already looked at this, but the first thing I would check is (1) if the R:\ and S:\ are properly mapped (if you click on the R:\ and S:\ in the file explorer, does it take you where you would expect?); (2) that the user has access to the various folders/files/applications in the code.

How do I delete a signature from an Excel document using Office automation

I have a signed Excel workbook and I want to delete the signature from it. The problem is that when I try to delete the signature there's a prompt for confirming the deletion and I want the process to be fully automatic.
Here's a code sample, the prompt appears when the last line executes:
Dim source As String = "c:\temp\signed.xlsx"
Dim app As New Application()
app.Visible = True
Dim book As Workbook = app.Workbooks.Open(source, UpdateLinks:=0)
app.ShowToolTips = False
Dim sig As Microsoft.Office.Core.Signature = book.Signatures.Item(1)
sig.Delete()
Thanks
using Aspose.Cells 5.2.0.0 can add and remove digital signature in excel 2007
I ended up using the SaveAs function of the Workbook object. Saving a copy of the document removes any signatures.
I'd still like to hear if there's a more elegant solution though.

Resources