Load contents of .rtf file into RichTextBox Control - excel

I am trying to build an Excel application with UserForms. In a UserForm, I've successfully added RichTextBoxes. I have also successfully coded a button to save all RichTextBox contents in an .rtf file.
This is where I'm stuck:
There does not seem to be a way to load the contents of an rtf file into a RichTextBox.
I've flipped through 5 pages of various google searches, but none gave any clues towards Excel VBA... all were on C#...
Speaking of C#, when I try something similar such as .rtfOne.LoadFile(filePath) I get an error:
Run-Time error '75':
The specified Path/file name cannot be accessed or is invalid.
Code I tried:
Dim filePath As String
filePath = "C:\Users\Donkey\Desktop\Test One\" & Cells(1,1)
UserForm.richtextbox1.LoadFile(filePath & "richtextfile.rtf")
I have included a MsgBox in my code to see how the compiler reads this line, it is read correctly as:
C:\Users\Donkey\Desktop\Test One\2019AA-10000V1richtextfile.rtf

Related

Saving Excel Files to Sharepoint using VBA

I am attempting to run a query in Excel and update the data to Sharepoint (accessible via Teams) on a set schedule.
Desired folder destination:
https://rxsafeway.sharepoint.com/:f:/r/sites/EXELiveProductionTool/Shared%20Documents/General/Portland/Individual%20Performance%20Update?csf=1&web=1&e=oFwj6i
VBA code used:
ThisWorkbook.SaveAs Filename:="https://rxsafeway.sharepoint.com/sites/EXELiveProductionTool/Shared%20Documents/General/Portland/Individual%20Performance%20Update/" & ".xlsm"
Details:
-The above VBA code has been used successfully while connected to VPN and on the company network.
-It works inconsistently... Potentially an upload limit?
-Notice that the "/:f:/r/" has been removed, a "/" has been added after "Update", and everything passed "Update" has been removed in the working code
Question:
Sometimes the code works and sometimes I get a Run-time error '1004': Method 'SaveAs' of object '_Workbook' failed
Is there a syntax error, or a smarter way to save the file?
You probably getting this error due to the MAX_PATH 256 limitation.
Your filename is too long and therefore will have a longer Windows path as well.
I suggest you to view your SharePoint files in File Explorer (see the documentation here). By doing so, every file/folder in your sharepoint will have a normal path and you'll not have anymore to use your file's url.
After that, you can use this formula to save your xlsm in some location in sharepoint :
Path="C:\Users\AKow\Sharepoint_Name\Path_to_your_file"
Filename = "Individual Performance Update"
ThisWorkbook.SaveAs Filename:= Path & Filename, FileFormat:=xlOpenXMLWorkbookMacroEnabled

How do I upload a file to OneDrive Business (Sharepoint) with VBA

I'm trying to find a method to upload a file to Onedrive from VBA in Excel.
I've done a lot of searching for acceptable methods, but most methods will not work for my scenario or the proposed solution will give an error.
I can upload files just fine if I'm using UNC paths or OneDrive locations that are synced locally (e.g. "C:\Users(username)\OneDrive\File Share") but I need a method that lets me push(upload) files to a shared URL location (e.g. "https://my.sharepoint.com/:f:/r/personal/(email_address)/Documents/SharedFiles?csf=1&e=6WmUIO"). All the users that will need to use the tool will have access to this shared location in OneDrive.
I tried the normal "SaveAs" method in VBA but that won't work.
Set Excelwb = ThisWorkbook
Excelwb.SaveAs fileName:="https://my.sharepoint.com/:f:/r/personal/(email_address)/Documents/SharedFiles?csf=1&e=6WmUIO" _
, FileFormat:=xlOpenXMLWorkbook, ConflictResolution:=xlLocalSessionChanges
Excelwb.Saved = True
Excelwb.Close SaveChanges:=False
Application.DisplayAlerts = True
I expected this to save the file, but I understand that since I'm saving to a URL and not a local file that another method is probably required, but I can't find one that will work.
The error I'm getting is:
Run-time error '1004'
Method 'SaveAs' of object '_Workbook' failed
I just tried this and it worked fine:
Activeworkbook.SaveAs "https://myComany.sharepoint.com/Departments/dept1/Documents/FGH/Text.xlsx"
To get the required path, browse to the destination in IE, then use Library tab >> Open with Explorer to open the destination in Windows Explorer: you can copy the path from the address bar there.
Maybe this will help someone pulling their hair out. Make sure and spaces in your SharePoint URL are replaced by %20. Example "New Folder" should be "New%20Folder, or remove the space from the folder name.

How to define a specific application to open a pdf from excel

I would like to automate the opening of a pdf which is defined using a dynamic hyperlink, but using the DEFAULT pdf editor of the user, but cannot fathom how to do this beyond the code below which opens the file in
I have other code to save the pdf which automatically saves it and opens a file in the default program for each user. But I don't know how to do the reverse and have a file which is searched for and found via the dynamic hyperlink - which will then allow them to update.
This is to shortcut sometone opening various folders, manually seatching, then opening up a file. ideally Id want to automate.
Here is the code I have so far which works but will only open in Adobe and not the default program - is this perhaps a setting thing on my PC or is it code which is missing?
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("TNR Report")
Dim r As Range: Set r = ws.Range("A82") ' this defines the actual path and filename to be searched for
Dim strHyperlink As String
On Error GoTo CannotFindFile
strHyperlink = r.Value
ThisWorkbook.FollowHyperlink strHyperlink
File needs to open with nuance to enable the user to edit and update the "record", but opens only in reader which is no use and kind of defeats the purpose of the code.
2 possibilities:
Make Nuance your default application in Windows to open PDF files.
Use a shell command to start nuance with a filename as parameter
Shell """C:\Path To Nuance\Nuance.exe"" ""C:\Path To PDF\pdffile.pdf"""

AppleScript/Automator Folder Action to Convert Excel to CSV

I'm running into a "Grant Access" problem with Office 2016 where I have to manually click "Grant Access" if I'm opening a new file with Automator. I found this answer for how to get around it (by using a file object or an alias object instead of a string):
tell application "Microsoft Excel"
activate
open file "Macintosh HD:Users:path:to:file"
end tell
But since I'm using an Automator folder action, I'm not sure how to get that file path where it needs to be. Most of the examples I found has the AppleScript use choose folder with prompt but since the whole point of this is to be fully automated, that's not going to work.
The idea is:
Excel file gets downloaded into "ForSQL" folder
Folder action prompts xls file to convert into csv
csv then opens in TextWrangle to ensure it stays in UTF-8
Then moves it to official "SQL" folder
Closes all the applications it opened and deletes whatever it moved from the "ForSQL" folder
But I'm open to better suggestions that get to the same end result.
This is my Automator workflow so far -- but it looks like I need to replace the 'Convert Format of Excel Files' step with AppleScript to get the "Grant Access" pop-up to go away. It's a folder action that starts when something hits the "ForSQL" folder:
I am not sure to understand what you want to do with textWrangle, but the script bellow does all steps before and after, only using Applescript (no need for Automator actions) :
--this choose file must be replaced by your "input" of automator folder items
set Fxl to choose file --the Excel file to be processed
-- define here your destination SQL folder
-- for my tests, I used a folder mySQL on my Desktop
set SQLFolder to ((path to desktop folder) as string) & "mySQL"
tell application "Finder" to set ForSQL to (container of Fxl) as string
--define new name by replacing current extension (xls, xlsx, xlsm, xml) by "csv"
tell application "Finder"
set N to name of Fxl
set NbExt to length of ((name extension of Fxl) as string)
set newname to (text 1 thru -(NbExt + 1) of N) & "csv"
end tell
--convert to CSV and close
tell application "Microsoft Excel"
open Fxl
tell workbook 1
tell sheet 1 to save in (ForSQL & newname) as CSV file format
close without saving
end tell
end tell
-- add eventually your TextWrangle step here (?)
-- delete source file and move csv to proper folder
tell application "Finder"
delete Fxl
move file (ForSQL & newname) to folder SQLFolder
end tell
In practice you can run all this in full applescript folder action.
Replace the "set Fxl to choose..." by "on adding folder items...".
In this case you must insert the script above in a repeat loop to process all files drops in the folder.
However, you may have some issues using folder action script, because system will trigger your script again at the time you are creating the new CSV file (again in the same folder !).
The work around I suggest is to use folder action script to get the file dropped in ForSQL, move it to an other "temp_ForSQL" and run this script using the moved file. Doing so, the cvs will be added to temp_forSLQ, without triggering new folder action script.
I hope it is clear enough...if not, just let me know.

Problems came up in the following areas during load: Table

I have generated an excel file from xml. But i can not open it with Excel. Excel gives the following error opening it:
Problems came up in the following areas during load:
Table
Then it shows a message that the log file corresponding the error can be found at : C:/Documents and Setting/myUserName/Local Settings/Temporary Internet Files/Content.MSO/xxxxx.log
But i can not find Content.MSO folder in my windows. I checked folder settings and made all folders visible but i still can not access this folder. So that i can not analise the log file.
how could i find the generated log file?
I found the problem without analising the log file. i stil can not access the log file in temporary internet files. But i realised that i put a string(non-number) characters on a number-styled cell in Excel xml. So if you having the similar issues about your Excel file generated from xml, then have a look at if your cell values are appopriate with your cell data type.
If you type or paste the path of the log file into Explorer or your text editor of choice, you may find that the folder does exist, despite being invisible.
In my case it was a <Row> with an incorrect ss:Index
I was using a template and the last row had a fixed Index=100. If the number of rows I added exceeded 100, this last row had a wrong index and excel threw the error without any other message or log (MacOSX, Excel 15.25.1). I wish they printed more informative error messages, what a waste of our time.
Excel 2016. My error message was "Worksheet Settings". Path was pointing to non-existing file.
My cause of the problem was ExpandedRowCount not big enough for number of rows in Worksheet. If you add rows in XML directly (i.e. on a machine where Excel is not installed), make sure to increment number of rows in ExpandedRowCount.
yes.Even i too faced the same problem and problem was with the data type of cells ofexcel generated using xslt
In addition to checking the data being used vs "Type" assigned, make sure that the list of characters that need to be encoded for XML are indeed encoded.
I had a system that appeared to be working, but then some user data including & and < was throwing this error.
If you're not sure what's going on with your file, try http://www.xmlvalidation.com/ - that helped be spot the issue in a large file immediately.
I used this function to fix it, modified from this post:
function xmlsafe($s) {
return str_replace(array('&','>','<','"'), array('&','>','<','"'), $s);
}
and then run echo xmlsafe($myvalue) where you were just echoing $myvalue in your script.
This seems to be more appropriate for XML than htmlentities() or other options built into PHP.
I had the same issue, and the answer was - type of Cell was Number and some values doesn't converts to this type on my backend.
I had the SAME problem,
and its because de file is TOO BIG.
I try an extract from SAP, more little than the one with that make the error) and save it in XML file. and it WORK, no more error.
so maybe if you can save in 2 Excel files XML instead of 1 it will be good ;)
ALicia

Resources